紫书第四章训练 UVA1589 Xiangqi by 15 周泽玺

Xiangqi is one of the most popular two-player board games in China. The game represents a battle between two armies with the goal of capturing the enemy’s “general” piece. In this problem, you are given a situation of later stage in the game. Besides, the red side has already “delivered a check”. Your work is to check whether the situation is “checkmate”.

Now we introduce some basic rules of Xiangqi. Xiangqi is played on a 10×9 board and the pieces are placed on the intersections (points). The top left point is (1,1) and the bottom right point is (10,9). There are two groups of pieces marked by black or red Chinese characters, belonging to the two players separately. During the game, each player in turn moves one piece from the point it occupies to another point. No two pieces can occupy the same point at the same time. A piece can be moved onto a point occupied by an enemy piece, in which case the enemy piece is "captured" and removed from the board. When the general is in danger of being captured by the enemy player on the enemy player’s next move, the enemy player is said to have"delivered a check". If the general‘s player can make no move to prevent the general‘s capture by next enemy move, the situation is called “checkmate”. 

We only use 4 kinds of pieces introducing as follows: 
General: the generals can move and capture one point either vertically or horizontally and cannot leave the “ palace” unless the situation called “ flying general” (see the figure above). “Flying general” means that one general can “fly” across the board to capture the enemy general if they stand on the same line without intervening pieces. 
Chariot: the chariots can move and capture vertically and horizontally by any distance, but may not jump over intervening pieces 
Cannon: the cannons move like the chariots, horizontally and vertically, but capture by jumping exactly one piece (whether it is friendly or enemy) over to its target. 
Horse: the horses have 8 kinds of jumps to move and capture shown in the left figure. However, if there is any pieces lying on a point away from the horse horizontally or vertically it cannot move or capture in that direction (see the figure below), which is called “ hobbling the horse’s leg”.

Now you are given a situation only containing a black general, a red general and several red chariots, cannons and horses, and the red side has delivered a check. Now it turns to black side’s move. Your job is to determine that whether this situation is “checkmate”.

InputThe input contains no more than 40 test cases. For each test case, the first line contains three integers representing the number of red pieces N (2<=N<=7) and the position of the black general. The following n lines contain details of N red pieces. For each line, there are a char and two integers representing the type and position of the piece (type char ‘G’ for general, ‘R’ for chariot, ‘H’ for horse and ‘C’ for cannon). We guarantee that the situation is legal and the red side has delivered the check. 
There is a blank line between two test cases. The input ends by 0 0 0.OutputFor each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘NO’. 
Sample Input

2 1 4
G 10 5
R 6 4

3 1 5
H 4 5
G 10 5
C 7 5

0 0 0

Sample Output

YES
NO

Hint

In the first situation, the black general is checked by chariot and “flying general”.
In the second situation, the black general can move to (1, 4) or (1, 6) to stop check.
See the figure above.

题意:

就是给出一个象棋残局,问是否绝杀,并且黑方只有一个帅(其他规则与象棋走法一致)

分析:

开始存入将能走到的四个点,判断这四个点是否有未被其他棋子将军到的。

一道细节题,不同的人有不同的错误,一些方面没有考虑,或者细节上就是有问题,当然,也有可能是逻辑上的问题。

这份代码是我WA多次后的,做的心烦,导致自己无法静心。下面错误数据就是导致我错N次的原因。
4 1 4
C 3 4
R 5 4
C 7 4
G 10 5

4 1 4
C 3 4
G 10 4
R 10 5
R 7 5
细节决定成败,一个逻辑错误,导致一晚上的心烦。
HDU上数据与本题并不一致,一开始就飞将,本题要输出NO,而HDU上的4121却不用
(这个情况题目没说明,但根据象棋的走法,应该是输出NO的)

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <time.h>
  6 #include <iostream>
  7 #include <algorithm>
  8 #include <string>
  9 #include <vector>
 10 #include <map>
 11 #include <stack>
 12 #include <queue>
 13 #include <deque>
 14 #include <set>
 15 #define ll long long
 16 #define eps 1e-6
 17 #define pi (acos(-1))
 18 #define e exp(1.0)
 19 #define cei(n) ((int)ceil((n)))
 20 #define rou(n) ((int)round((n)))
 21 #define qclear(q) while(!q.empty())q.pop();
 22
 23 template<class T> T gcd(T a, T b) { return b ? GCD(b, a%b) : a; }
 24 template<class T> T lcm(T a, T b) { return a / GCD(a,b) * b;    }
 25 using namespace std;
 26
 27 char a[22][22];
 28 bool panduan(int x,int y)//判断是否超过中间3*3的格子
 29 {
 30     if(x<1||x>3||y<4||y>6)return false;
 31     return true;
 32 }
 33 int fx[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};//马的路线
 34 int matui[8][2]={1,1,1,-1,-1,1,-1,-1,1,1,1,-1,-1,1,-1,-1};//马脚的位置
 35 bool ma(int x,int y)//判断是否能打到这个点
 36 {
 37     for(int i=0;i<8;i++)
 38     {
 39         if(x+fx[i][0]>=1&&y+fx[i][1]>=1&&x+fx[i][0]<=10&&y+fx[i][1]<=10)
 40         {
 41             if(a[x+fx[i][0]][y+fx[i][1]]==‘H‘&&a[x+matui[i][0]][y+matui[i][1]]==‘.‘)return true;
 42         }
 43     }
 44     return false;
 45 }
 46 bool kk(int x,int y)//判断车、将、跑是否能打到这个点
 47 {
 48     int i=x-1,k=0;
 49     for(i=x-1;i>=1;i--)
 50     {
 51         if(((a[i][y]==‘R‘||a[i][y]==‘G‘)&&k==0)||(a[i][y]==‘C‘&&k==1))
 52         {
 53             return true;
 54         }
 55         else if(a[i][y]!=‘.‘)
 56         {
 57             k++;
 58             if(k>=2)
 59                 break;
 60         }
 61     }
 62     i=x+1,k=0;
 63     for(i=x+1;i<=10;i++)
 64     {
 65         if(((a[i][y]==‘R‘||a[i][y]==‘G‘)&&k==0)||(a[i][y]==‘C‘&&k==1))
 66         {
 67             return true;
 68         }
 69         else if(a[i][y]!=‘.‘)
 70         {
 71             k++;
 72             if(k>=2)
 73                 break;
 74         }
 75     }
 76     i=y+1,k=0;
 77     for(i=y+1;i<=10;i++)
 78     {
 79         if(((a[x][i]==‘R‘||a[x][i]==‘G‘)&&k==0)||(a[x][i]==‘C‘&&k==1))
 80         {
 81             return true;
 82         }
 83         else if(a[x][i]!=‘.‘)
 84         {
 85             k++;
 86             if(k>=2)
 87                 break;
 88         }
 89     }
 90     i=y-1,k=0;
 91     for(i=y-1;i>=1;i--)
 92     {
 93         if(((a[x][i]==‘R‘||a[x][i]==‘G‘)&&k==0)||(a[x][i]==‘C‘&&k==1))
 94         {
 95             return true;
 96         }
 97         else if(a[x][i]!=‘.‘)
 98         {
 99             k++;
100             if(k>=2)break;
101         }
102     }
103     return false;
104 }
105 bool fj(int x,int y)//一开始将是否在面对面
106 {
107     for(int i=x+1;i<=10;i++)
108     {
109         if(a[i][y]==‘G‘)return 1;
110         if(a[i][y]!=‘.‘)return 0;
111     }
112 }
113 int main()
114 {
115     #ifndef ONLINE_JUDGE
116         freopen("in.txt","r",stdin);
117         freopen("out.txt","w",stdout);
118         ll _btime=clock();
119     #endif
120     char ch[2];
121     int T,x,y,x1,y1;
122     queue< pair <int,int> >q; pair <int,int> t;
123     while(~scanf("%d%d%d",&T,&x,&y),T||x||y)
124     {
125         //初始化
126         while(!q.empty())q.pop();
127         x1=x;
128         y1=y;
129         memset(a,‘.‘,sizeof(a));
130         a[x][y]=‘#‘;
131         //判断将旁边4个位置
132         if(panduan(x-1,y))
133         {
134             q.push(make_pair(x-1,y));
135         }
136         if(panduan(x+1,y))
137         {
138             q.push(make_pair(x+1,y));
139         }
140         if(panduan(x,y+1))
141         {
142             q.push(make_pair(x,y+1));
143         }
144         if(panduan(x,y-1))
145         {
146             q.push(make_pair(x,y-1));
147         }
148         #ifndef ONLINE_JUDGE
149             for(int i=0;i<kyz.size();i++)
150             {
151             cout<<kyz[i].first<<"*"<<kyz[i].second<<endl;
152             }
153             xintengzhiji,c-free moumingdeguangbi;
154         #endif
155         while(T--)
156         {
157             scanf("%s%d%d",ch,&x,&y);
158             a[x][y]=ch[0];
159         }
160         #ifndef ONLINE_JUDGE
161             for(int i=1;i<=10;i++)
162             {
163                 for(int j=1;j<=10;j++)
164                 {
165                     cout<<a[i][j];
166                 }
167                 puts("");
168             }
169         #endif
170         if(fj(x1,y1))//一开始飞将的情况
171         {
172             puts("NO");
173             continue;
174         }
175         int llll=0;
176         while(!q.empty())
177         {
178             t=q.front();
179             int xx=t.first;
180             int yy=t.second;
181             if(kk(xx,yy)||ma(xx,yy))//判断这个点是否能走
182             {
183                 q.pop();
184             }
185             else{llll=1;
186             break;
187             }
188
189         }
190         //本来不想有llll的,一直错,就改成这样了
191         if(!llll)puts("YES");
192         else puts("NO");
193     }
194     #ifndef ONLINE_JUDGE
195         ll _etime=clock();
196         printf("time = %lld ms",_etime-_btime);
197     #endif
198     return 0;
199 }
200 /*
201 2 1 4
202 R 2 2
203 G 10 5
204
205 2 1 5
206 H 3 5
207 C 10 5
208
209 2 1 4
210 G 10 5
211 R 6 4
212
213 3 1 5
214 H 4 5
215 G 10 5
216 C 7 5
217
218 2 1 5
219 R 4 4
220 G 10 5
221
222 3 1 5
223 G 10 4
224 R 5 5
225 H 3 7
226
227 4 1 5
228 G 10 4
229 C 6 5
230 H 5 5
231 R 1 1
232
233 5 1 5
234 G 10 4
235 C 6 5
236 H 5 5
237 H 4 5
238 R 1 1
239
240 3 1 5
241 G 10 4
242 C 2 7
243 H 3 7
244
245 3 1 5
246 G 10 4
247 R 5 5
248 R 1 6
249
250 4 1 5
251 G 10 4
252 R 5 5
253 R 1 6
254 H 3 7
255
256
257 1
258 1 4
259 G 10 4
260
261 3 1 4
262 G 10 4
263 R 10 3
264 R 10 5
265
266
267 0 0 0
268
269 */
时间: 2024-12-25 08:40:37

紫书第四章训练 UVA1589 Xiangqi by 15 周泽玺的相关文章

紫书第三章训练2 暴力集

A - Master-Mind Hints MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the leng

紫书第五章训练3 D - Throwing cards away I

D - Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move

紫书第五章训练2 F - Compound Words

F - Compound Words You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. Input Standard input consists of a number

紫书第五章训练 uva 10763 Foreign Exchange by crq

Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.The

紫书第五章训练 uva 10391 Compound Words by crq

You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. Input Standard input consists of a number of lowercase words

紫书第五章训练 uva 1594 Ducci Sequence by crq

Description A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers: ( a1, a2, ... , an)  (| a1 - a

紫书第4章 函数和递归

1  序 系统的整理下第四章的学习笔记.同上次一样,尽量在不依赖书本的情况下自己先把例题做出来.这次有许多道题代码量都比较大,在例题中我都用纯C语言编写,但由于习题的挑战性和复杂度,我最终还是决定在第五章开始前,就用C++来完成习题.不过所有的代码都是能在C++提交下AC的. 在习题中,我都习惯性的构造一个类来求解问题,从我个人角度讲,这会让我的思路清晰不少,希望自己的一些代码风格不会影响读者对解题思路的理解. 其实在第四章前,我就顾虑着是不是真的打算把题目全做了,这些题目代码量这么大,要耗费很

紫书第三章 数组和字符串

1  序 系统的整理下第三章的学习笔记.例题代码是在未看书本方法前自己尝试并AC的代码,不一定比书上的标程好:习题除了3-8百度了求解方法,其它均独立完成后,会适当查阅网上资料进行整理总结.希望本博文方便自己日后复习的同时,也能给他人带来点有益的帮助(建议配合紫书--<算法竞赛入门经典(第2版)>阅读本博客).有不足或错误之处,欢迎读者指出. 2  例题 2.1  UVa272--Tex Quotes #include <stdio.h> int main() { bool log

《机器学习》西瓜书第四章决策树

本章主要对决策树算法进行了讲解,主要有决策树的生成过程.决策树的划分选择.决策树的剪枝处理.连续与缺失值的处理以及多变量决策树. 4.1 基本流程 决策树是基于树的结构来进行决策的.包含一个根节点.若干内部节点和若干叶节点.叶节点对应于决策结果,其他每个结点对应于一个属性测试. 决策树学习的目的是产生一颗泛化能力强的决策树,其基本流程遵循简单的“分而治之”策略. 决策树学习的基本算法 输入:训练集D = {(x1,y1),(x2,y2),...,(xn,yn)}; 属性集 A = {a1,a2,