Xiangqi is one of the most popular two-player board games in China. The game represents a battlebetween two armies with the goal of capturing the enemy’s “general” piece. In this problem, you aregiven 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 playedon a 10 × 9 board and the pieces are placed on the intersections(points). The top left point is (1,1) and the bottom right pointis (10,9). There are two groups of pieces marked by black or redChinese characters, belonging to the two players separately. Duringthe game, each player in turn moves one piece from the point itoccupies to another point. No two pieces can occupy the same pointat the same time. A piece can be moved onto a point occupied byan enemy piece, in which case the enemy piece is“captured” andremoved from the board. When the general is in danger of beingcaptured by the enemy player on the enemy player’s next move, theenemy player is said to have “delivered a check”. If the general’splayer can make no move to prevent the general’s capture by nextenemy 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 horizontallyand 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 generalif 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 captureby 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 itcannot move or capture in that direction (see the figure below), which is called “hobbling thehorse’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. Yourjob 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 integersrepresenting the number of red pieces N (2 ≤ N ≤ 7) and the position of the black general. Thefollowing N lines contain details of N red pieces. For each line, there are a char and two integersrepresenting the type and position of the piece (type char ‘G’ for general, ‘R’ for chariot, ‘H’ for horseand ‘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 checkedby chariot and “flying general”. In the second situation,the black general can move to (1, 4) or (1, 6) tostop 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
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int f(int x,int y,int n,int chess[][2],int local[][11],char sprice[])
{
int i,j,flag=0,sum;
for(i=1;i<=n;i++)
{
if(sprice[i]=='G')//如果该点是红方的帅
{
if(chess[0][1]==chess[i][1])//如果红方帅和对面将原本就在同一条线上
{
sum=0;
for(j=chess[0][0]+1;j<chess[i][0];j++)//将和帅是否有其他子,没有就不可行
sum=sum+local[j][chess[i][1]];
if(sum==0)
return 0;
}
if(y==chess[i][1])//如果黑方将移动之后和红方帅在一条线上
{
sum=0;
for(j=x+1;j<chess[i][0];j++)
sum=sum+local[j][y];
if(sum==0)//如果其中没有子,黑方被吃
flag=1;
}
}
else if(sprice[i]=='R')//如果该点是红方的车
{
if(x==chess[i][0])//如果车在将走好后的同一行上
{
if(y>chess[i][1])//如果车在将走好之后的左侧
{
sum=0;
for(j=chess[i][1]+1;j<y;j++)
sum=sum+local[x][j];
if(sum==0)//如果其中没有其他的子
flag=1;
}
else if(y<chess[i][1])//如果车在将走好之后的右侧
{
sum=0;
for(j=y+1;j<chess[i][1];j++)
sum=sum+local[x][j];
if(sum==0)//如果其中没有其他的子
flag=1;
}
}
else if(y==chess[i][1])//如果车在将走好后的同一竖上
{
if(x>chess[i][0])//如果车在将走好之后的上侧
{
sum=0;
for(j=chess[i][0]+1;j<x;j++)
sum=sum+local[j][y];
if(sum==0)//如果其中没有其他的子
flag=1;
}
else if(x<chess[i][0])//如果车在将走好之后的下侧
{
sum=0;
for(j=x+1;j<chess[i][0];j++)
sum=sum+local[j][y];
if(sum==0)//如果其中没有其他的子
flag=1;
}
}
}
else if(sprice[i]=='C')//如果该点是红方的炮
{
if(x==chess[i][0])//如果炮在将走好后的同一行上
{
if(y>chess[i][1])//如果炮在将走好之后的左侧
{
sum=0;
for(j=chess[i][1]+1;j<y;j++)
sum=sum+local[x][j];
if(sum==1)//如果其中有一个子
flag=1;
}
else if(y<chess[i][1])//如果炮在将走好之后的右侧
{
sum=0;
for(j=y+1;j<chess[i][1];j++)
sum=sum+local[x][j];
if(sum==1)//如果其中有一个子
flag=1;
}
}
else if(y==chess[i][1])//如果炮在将走好后的同一竖上
{
if(x>chess[i][0])//如果炮在将走好之后的上侧
{
sum=0;
for(j=chess[i][0]+1;j<x;j++)
sum=sum+local[j][y];
if(sum==1)//如果其中有一个子
flag=1;
}
else if(x<chess[i][0])//如果炮在将走好之后的下侧
{
sum=0;
for(j=x+1;j<chess[i][0];j++)
sum=sum+local[j][y];
if(sum==1)//如果其中有一个子
flag=1;
}
}
}
else //如果该点是红方的马
{
if((x-2==chess[i][0]&&y-1==chess[i][1])||(x-1==chess[i][0]&&y-2==chess[i][1]))//八个方向的马脚分别判断
{
if(local[x-1][y-1]==0)
flag=1;
}
else if((x+2==chess[i][0]&&y-1==chess[i][1])||(x+1==chess[i][0]&&y-2==chess[i][1]))
{
if(local[x+1][y-1]==0)
flag=1;
}
else if((x-2==chess[i][0]&&y+1==chess[i][1])||(x-1==chess[i][0]&&y+2==chess[i][1]))
{
if(local[x-1][y+1]==0)
flag=1;
}
else if((x+2==chess[i][0]&&y+1==chess[i][1])||(x+1==chess[i][0]&&y+2==chess[i][1]))
{
if(local[x+1][y+1]==0)
flag=1;
}
}
}
return flag;
}
int main(void)
{
int local[11][11],chess[11][2];//一个是棋盘,另一个记录棋子的位子
int n,b,c,f1,f2,f3,f4,i,j;
char a,sprice[11];
while(cin>>n>>b>>c)
{
if(n==0&&b==0&&c==0)
break;
f1=0;f2=0;f3=0;f4=0;
sprice[0]='G';
chess[0][0]=b;chess[0][1]=c;//敌方将的位子
memset(local,0,sizeof(local));
for(i=1;i<=n;i++)
{
cin>>a;
sprice[i]=a;
cin>>b>>c;
chess[i][0]=b;
chess[i][1]=c;
local[b][c]=1;//棋盘内红方子在的敌方都标1
}
if(chess[0][0]-1>0)//敌方将向上走
f1=f(chess[0][0]-1,chess[0][1],n,chess,local,sprice);
else f1=1;//如果不能走标记1
if(chess[0][0]+1<4)//敌方将向下走
f2=f(chess[0][0]+1,chess[0][1],n,chess,local,sprice);
else f2=1;
if(chess[0][1]-1>3)//敌方将向左走
f3=f(chess[0][0],chess[0][1]-1,n,chess,local,sprice);
else f3=1;
if(chess[0][1]+1<7)//敌方将向右走
f4=f(chess[0][0],chess[0][1]+1,n,chess,local,sprice);
else f4=1;
if(f1==1&&f2==1&&f3==1&&f4==1)
cout<<"YES"<<endl;
else if(n!=0)
cout<<"NO"<<endl;
}
return 0;
}
//这题有许多步骤可以简化,但为了清晰,所以全列出来了
//主要还是象棋的规则