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”.
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”.
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 0Sample Output
YES NOHint
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.
就是给出一个象棋残局,问是否绝杀,并且黑方只有一个帅(其他规则与象棋走法一致)
坑点:
1.将可以吃掉将军的棋子
2.马的蹩脚
3.炮和将中间有且只有一个棋子的时候才能吃掉将
4.车和将中间没有棋子时才能吃将
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int s1[20],s2[20],n;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char a[20][20];
int check(int x,int y)
{
if(x<1||x>3)
return -2;
if(y<4||y>6)
return -2;
int i,j,k,p,q,r,t;
for(i=0;i<n;i++)
{
j=s1[i];
k=s2[i];
if(x==j&&y==k)
continue;
if(a[i][0]=='R'||a[i][0]=='G')
{
if(j==x)
{
if(k<y)
{
for(r=0;r<n;r++)
{
p=s1[r];
q=s2[r];
if(p==x&&q>k&&q<y)
break;
}
if(r==n)
return 0;
}
else
if(k>y)
{
for(r=0;r<n;r++)
{
p=s1[r];
q=s2[r];
if(p==x&&q>y&&q<k)
break;
}
if(r==n)
return 0;
}
}
else
if(k==y)
{
if(j<x)
{
for(r=0;r<n;r++)
{
p=s1[r];
q=s2[r];
if(q==y&&p>j&&p<x)
break;
}
if(r==n)
return 0;
}
else
if(j>x)
{
for(r=0;r<n;r++)
{
p=s1[r];
q=s2[r];
if(q==y&&p>x&&p<j)
break;
}
if(r==n)
return 0;
}
}
}
else
if(a[i][0]=='H')
{
for(r=0;r<n;r++)
{
p=s1[r];
q=s2[r];
if(p==x&&q==y)
continue;
if(q==k&&p==j-1)
break;
}
if(r==n)
{
if(j-2==x&&k-1==y)
return 2;
if(j-2==x&&k+1==y)
return 2;
}
for(r=0;r<n;r++)
{
if(p==x&&q==y)
continue;
p=s1[r];
q=s2[r];
if(q==k&&p==j+1)
break;
}
if(r==n)
{
if(j+2==x&&k-1==y)
return 2;
if(j+2==x&&k+1==y)
return 2;
}
for(r=0;r<n;r++)
{
if(p==x&&q==y)
continue;
p=s1[r];
q=s2[r];
if(p==j&&q==k-1)
break;
}
if(r==n)
{
if(j-1==x&&k-2==y)
return 2;
if(j+1==x&&k-2==y)
return 2;
}
for(r=0;r<n;r++)
{
if(p==x&&q==y)
continue;
p=s1[r];
q=s2[r];
if(p==j&&q==k+1)
break;
}
if(r==n)
{
if(j-1==x&&k+2==y)
return 2;
if(j+1==x&&k+2==y)
return 2;
}
}
else
if(a[i][0]=='C')
{
if(j!=x&&k!=y)
continue;
if(j==x)
{
if(k<y)
{
t=0;
for(r=0;r<n;r++)
{
p=s1[r];
q=s2[r];
if(p==x&&q==y)
continue;
if(p==x&&q>k&&q<y)
t++;
}
if(t==1)
return 3;
}
else
if(k>y)
{
t=0;
for(r=0;r<n;r++)
{
p=s1[r];
q=s2[r];
if(p==x&&q==y)
continue;
if(p==x&&q>y&&q<k)
t++;
}
if(t==1)
return 3;
}
}
else
if(k==y)
{
if(j<x)
{
t=0;
for(r=0;r<n;r++)
{
p=s1[r];
q=s2[r];
if(p==x&&q==y)
continue;
if(q==y&&p>j&&p<x)
t++;
}
if(t==1)
return 3;
}
else
if(j>x)
{
t=0;
for(r=0;r<n;r++)
{
p=s1[r];
q=s2[r];
if(p==x&&q==y)
continue;
if(q==y&&p>x&&p<j)
t++;
}
if(t==1)
return 3;
}
}
}
}
return -1;
}
int main()
{
int i,j,k,x1,y1,x2,y2,t,x,y,ca=1;
while(scanf("%d%d%d",&n,&x1,&y1)!=EOF)
{
if(n==0&&x1==0&&y1==0)
break;
for(i=0;i<n;i++)
scanf("%s%d%d",a[i],&s1[i],&s2[i]);
for(i=0;i<4;i++)
{
x2=x1+dir[i][0];
y2=y1+dir[i][1];
t=check(x2,y2);
if(t==-1)
break;
}
//printf("%d:",ca++);
if(i==4)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
R 2 2
G 10 5
2 1 5
H 3 5
C 10 5
2 1 4
G 10 5
R 6 4
3 1 5
H 4 5
G 10 5
C 7 5
2 1 5
R 4 4
G 10 5
3 1 5
G 10 4
R 5 5
H 3 7
4 1 5
G 10 4
C 6 5
H 5 5
R 1 1
5 1 5
G 10 4
C 6 5
H 5 5
H 4 5
R 1 1
3 1 5
G 10 4
C 2 7
H 3 7
3 1 5
G 10 4
R 5 5
R 1 6
4 1 5
G 10 4
R 5 5
R 1 6
H 3 7
1
1 4
G 10 4
3 1 4
G 10 4
R 10 3
R 10 5