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”.
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”.
Input
The 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.
There is a blank line between two test cases. The input ends by 0 0 0.
Output
For each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘NO’.
Hint
In the rst situation, the black general is checked by chariot and ying general". In the second situation, the black general can move to (1, 4) or (1, 6) to stop check. See the gure on the right.
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
这题真是......我日你哥~~
#include <iostream> struct xiangqi{ char qi; int x,y; }red[8]; int N,x,y,gx,gy,rwin; char che[12][12],qi; bool chess(int xx,int yy) { return !(che[xx][yy]=='*'||che[xx][yy]=='d'); } bool inpalace(int x,int y) { return x>0&&x<4&&y>3&&y<7; } int main() { while (std::cin>>N>>gx>>gy) { if (N==0&&gx==0&&gy==0) break; for (int i=1; i<11; i++) for (int j=1; j<10; j++) che[i][j]='*'; for (int i=0; i<N; i++) { std::cin>>red[i].qi>>red[i].x>>red[i].y; che[red[i].x][red[i].y]=red[i].qi; } rwin=0; for (int j=0; j<N; j++) { int i,cnt; char t=red[j].qi; x=red[j].x; y=red[j].y; if (t=='G') { if (y==gy) { for (i=x-1; i>gx; i--) { if (chess(i, y)) { rwin=1; break; } } if (rwin==0) { rwin=-1; break; } } for (i=x-1; i>0&&!chess(i, y); i--) che[i][y]='d'; if (i>0) che[i][y]='e'; } else if (t=='R') { if (x>3&&(y<4||y>6)) continue; for (i=x-1; i>0&&!chess(i, y); i--) che[i][y]='d'; if (i>0) che[i][y]='e'; for (i=x+1; i<4&&!chess(i, y); i++) che[i][y]='d'; if (i<4) che[i][y]='e'; for (i=y-1; i>3&&!chess(x, i); i--) che[x][i]='d'; if (i>3) che[x][i]='e'; for (i=y+1; i<7&&!chess(x, i); i++) che[x][i]='d'; if (i<7) che[x][i]='e'; } else if (t=='H') { int legx[4]={x,x-1,x,x+1},legy[4]={y+1,y,y-1,y},hx[8]={x+1,x-1,x-2,x-2,x-1,x+1,x+2,x+2},hy[8]={y+2,y+2,y+1,y-1,y-2,y-2,y-1,y+1}; for (int i=0; i<8; i++) { if (inpalace(hx[i], hy[i])&&!chess(legx[i/2], legy[i/2])) { if (chess(hx[i], hy[i])) che[hx[i]][hy[i]]='e'; else che[hx[i]][hy[i]]='d'; } } } else if (t=='C') { if (x>3&&(y<4||y>6)) continue; cnt=0; for (i=x-1; i>0; i--) { if (cnt==1) { if (chess(i, y)) { che[i][y]='e'; break; } che[i][y]='d'; } if (chess(i, y)) cnt++; } cnt=0; for (i=x+1; i<4; i++) { if (cnt==1) { if (chess(i, y)) { che[i][y]='e'; break; } che[i][y]='d'; } if (chess(i, y)) cnt++; } cnt=0; for (i=y-1; i>3; i--) { if (cnt==1) { if (chess(x, i)) { che[x][i]='e'; break; } che[x][i]='d'; } if (chess(x, i)) cnt++; } cnt=0; for (i=y+1; i<7; i++) { if (cnt==1) { if (chess(x, i)) { che[x][i]='e'; break; } che[x][i]='d'; } if (chess(x, i)) cnt++; } } } if (rwin==-1) { printf("NO "); continue; } int ngx[4]={gx,gx-1,gx,gx+1},ngy[4]={gy+1,gy,gy-1,gy}; for (int i=0; i<4; i++) { if (inpalace(ngx[i], ngy[i])) { if (che[ngx[i]][ngy[i]]!='d'&&che[ngx[i]][ngy[i]]!='e') { rwin=2; break; } } } if (rwin==2) printf("NO "); else printf("YES "); } }