Xiangqi
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”.
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’.
Output
For each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘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 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
解题心得
- 题意就是给你一个残局,里面红方正在将黑方,黑方只剩下一个帅,红方可能有车,马,炮,帅。问这个残局中黑方是否被将死。
- 思维很简单,只需要计算黑方的帅还剩下几口气(上下左右一共四口气,参考围棋),如果没有气则死了,搜索模拟一下就可以了,但是象棋并不是如此简单,如果程序不够严谨就可能出现各种BUG,所以在写这个程序的时候要严格按照象棋的规则执行,不然容易陷入找特例的错误思维当中。
/*代码有点长,x代表行,y代表列*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
bool bmaps[maxn][maxn];//true为王还有的气,false为没有气
char maps[maxn][maxn];//记录每个棋子在棋盘中的位置
int dir[4][2] = {1,0,-1,0,0,1,0,-1},n;//记录上下左右四个方向
int dir_H[8][2]={-2,1, -2,-1, -1,-2, -1,2, 1,2, 1,-2, 2,1, 2,-1};//记录马可以走的位置
int dir_H_foot[8][2] = {-1,0, -1,0, 0,-1, 0,1, 0,1, 0,-1, 1,0, 1,0};//对应上面走马的方向马腿的位置
struct node//每个棋子
{
char s[3];
int x,y;
}p[maxn];
void pre_maps()
{
memset(bmaps,false,sizeof(bmaps));
for(int i=1;i<=10;i++)
for(int j=1;j<=9;j++)
maps[i][j] = '*';
}
bool check(int x,int y)//检查是否出了棋盘
{
if(x<1 || y<1 || x>10 || y>9)
return false;
return true;
}
void pre_live(int x,int y)//在一开始的时候王总共的气
{
for(int i=0;i<4;i++)
{
int x1 = x+dir[i][0];
int y1 = y+dir[i][1];
if(x1 >= 1 && x1 <= 3 && y1 >=4 && y1 <=6)
bmaps[x1][y1] = true;
}
}
void check_R(int x,int y)//车走四个方向
{
int t = 1;
while(check(x+t,y) && maps[x+t][y]=='*')
{
bmaps[x+t][y] = false;
t++;
}
bmaps[x+t][y] = false;
t = 1;
while(check(x-t,y) && maps[x-t][y]=='*')
{
bmaps[x-t][y] = false;
t++;
}
bmaps[x-t][y] = false;
t = 1;
while(check(x,y+t) && maps[x][y+t]=='*')
{
bmaps[x][y+t] = false;
t++;
}
bmaps[x][y+t] = false;
t = 1;
while(check(x,y-t) && maps[x][y-t]=='*')
{
bmaps[x][y-t] = false;
t++;
}
bmaps[x][y-t] = false;
}
void check_H(int x,int y)//马走八个方向
{
int x1,y1,x2,y2;
for(int i=0;i<8;i++)
{
x1 = x + dir_H[i][0];
y1 = y + dir_H[i][1];
x2 = x + dir_H_foot[i][0];
y2 = y + dir_H_foot[i][1];
if(check(x1,y1) && maps[x2][y2] == '*')//要注意没出界,并且没绊马腿
bmaps[x1][y1] = false;
}
}
void check_C(int x,int y)//炮打翻山,四个方向
{
bool flag = false;
int t;
t = 1;
while(check(x+t,y))
{
if(maps[x+t][y] != '*')
{
flag = true;
break;
}
t++;
}
t++;
if(flag)
{
while(check(x+t,y) && maps[x+t][y]=='*')
{
bmaps[x+t][y] = false;
t++;
}
bmaps[x+t][y] = false;
}
flag = false;
t = 1;
while(check(x-t,y))
{
if(maps[x-t][y] != '*')
{
flag = true;
break;
}
t++;
}
t++;
if(flag)
{
while(check(x-t,y) && maps[x-t][y]=='*')
{
bmaps[x-t][y] = false;
t++;
}
bmaps[x-t][y] = false;
}
flag = false;
t = 1;
while(check(x,y+t))
{
if(maps[x][y+t] != '*')
{
flag = true;
break;
}
t++;
}
t++;
if(flag)
{
while(check(x,y+t) && maps[x][y+t]=='*')
{
bmaps[x][y+t] = false;
t++;
}
bmaps[x][y+t] = false;
}
flag = false;
t = 1;
while(check(x,y-t))
{
if(maps[x][y-t] != '*')
{
flag = true;
break;
}
t++;
}
t++;
if(flag)
{
while(check(x,y-t) && maps[x][y-t]=='*')
{
bmaps[x][y-t] = false;
t++;
}
bmaps[x][y-t] = false;
}
}
void check_G(int x,int y)//这里容易出错,要注意写法
{
bool flag = false;
int t = 1;
while(check(x-t,y))
{
bmaps[x-t][y] = false;
if(maps[x-t][y] != '*')
{
flag = true;
break;
}
t++;
}
}
int check_live(int x,int y)//检查最后王还剩下几口气
{
int live = 0;
for(int i=0;i<4;i++)
{
int x1 = x+dir[i][0];
int y1 = y+dir[i][1];
if(check(x1,y1) && bmaps[x1][y1])
live++;
}
return live;
}
void get_ans(int x,int y)
{
for(int i=0;i<n;i++)
{
if(p[i].s[0] == 'R')
check_R(p[i].x,p[i].y);
if(p[i].s[0] == 'H')
check_H(p[i].x,p[i].y);
if(p[i].s[0] == 'C')
check_C(p[i].x,p[i].y);
if(p[i].s[0] == 'G')
check_G(p[i].x,p[i].y);
}
int live = check_live(x,y);
if(live == 0)//没气啦
printf("YES
");
else
printf("NO
");
}
int main()
{
int x,y;
while(scanf("%d%d%d",&n,&x,&y))
{
if(n+x+y == 0)
break;
pre_maps();
pre_live(x,y);
for(int i=0;i<n;i++)
{
scanf("%s%d%d",p[i].s,&p[i].x,&p[i].y);//用的%s,防止出现各种意外
maps[p[i].x][p[i].y] = p[i].s[0];
}
//这里是在轮到红方走的时候,已经王见王了,黑方直接胜
int t = 1;
bool flag = false;
while(check(x+t,y) && (maps[x+t][y] == '*' || maps[x+t][y] == 'G'))
{
if(maps[x+t][y] == 'G')
{
flag = true;
break;
}
t++;
}
if(flag)
{
printf("NO
");
continue;
}
get_ans(x,y);
}
return 0;
}