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

Posted 台州学院ACM集训队

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了紫书第四章训练 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 */

 

 

























以上是关于紫书第四章训练 UVA1589 Xiangqi by 15 周泽玺的主要内容,如果未能解决你的问题,请参考以下文章

UVA 1589 Xiangqi

Xiangqi UVa1589(多次WA)

UVA1589 Xiangqi(挖坑待填)

UVa1589 Xiangqi 坑点总结+参考代码

习题4-1 Xiangqi UVA - 1589

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