原题链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4121
Xiangqi
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4073 Accepted Submission(s): 981
Problem Description
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”.
![](https://i-blog.csdnimg.cn/blog_migrate/356f85442970fd0dcce79af3c173494b.jpeg)
We only use 4 kinds of pieces introducing as follows:
![](https://i-blog.csdnimg.cn/blog_migrate/7c147cb728699ffae60913be69322189.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/62e41af65a98eddae463b418ef8be5c8.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/1605249ebcc3ba023e37440aebaa7c73.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/3b46f46983c3fe57247acf058f9c3d2b.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/ad30661e9a55a221e4a613af8913bf3e.jpeg)
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’.
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 NOHintIn 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. ![]()
Source
题意: 给出此时的棋盘,已知黑方只剩将,红方有将G,以及车R,炮C,马H,又已知此时红方已对黑方将军,判断黑方是否有可行方案摆脱死局。
思路:一次判断黑将可以走的格子,再判断每一个红棋是否可以吃掉,都不能吃掉说明方案可行。
几点注意:
①输入要用cin,用scanf容易WA。
②将只能走上下左右,不要被田字格斜线迷惑。
③黑将移动时可能将对方的旗子吃掉。
④双方将不可以直接对面。
被WA哭的一道题,从前一天晚上WA到第二天,太不仔细了。。。
代码:
#include "stdio.h"
#include "string.h"
#include "iostream"
using namespace std;
char map[11][10],r;
int n,bi,bj,ri[8],rj[8],ch1[4]={1,0,0,-1},ch2[4]={0,1,-1,0},a,flag,b;
int jude_G(int ii,int jj)
{
if(jj!=b)
return 1;
for(int i=ii-1;i>a;i--)
{
if(map[i][jj]!='0')
return 1;
}
return 0;
}
int jude_R(int ii,int jj)
{
if(ii==a)
{
int xmin=min(jj,b),xmax=max(jj,b);
for(int i=xmin+1;i<xmax;i++)
{
if(map[ii][i]!='0')
return 1;
}
return 0;
}
else if(jj==b)
{
int xmin=min(ii,a),xmax=max(ii,a);
for(int i=xmin+1;i<xmax;i++)
{
if(map[i][jj]!='0')
return 1;
}
return 0;
}
else
return 1;
}
int jude_C(int ii,int jj)
{
int nmid=0;
if(ii==a)
{
int xmin=min(jj,b),xmax=max(jj,b);
for(int i=xmin+1;i<xmax;i++)
{
if(map[ii][i]!='0')
nmid++;
}
if(nmid!=1)
return 1;
return 0;
}
else if(jj==b)
{
int xmin=min(ii,a),xmax=max(ii,a);
for(int i=xmin+1;i<xmax;i++)
{
if(map[i][jj]!='0')
nmid++;
}
if(nmid!=1)
return 1;
return 0;
}
else
return 1;
}
int jude_H(int ii,int jj)
{
int ch3[8]={2,2,1,-1,-2,-2,-1,1},ch4[8]={-1,1,2,2,1,-1,-2,-2},ch5[8]={1,1,0,0,-1,-1,0,0},ch6[8]={0,0,1,1,0,0,-1,-1};
for(int i=0;i<8;i++)
{
if(a==ii+ch3[i]&&b==jj+ch4[i]&&map[ii+ch5[i]][jj+ch6[i]]=='0')
return 0;
}
return 1;
}
int jude()
{
for(int i=1;i<=n;i++)
{
if(ri[i]==a&&rj[i]==b)
continue;
if(map[ri[i]][rj[i]]=='G'&&jude_G(ri[i],rj[i])==0)
{
return 0;
}
else if(map[ri[i]][rj[i]]=='R'&&jude_R(ri[i],rj[i])==0)
{
return 0;
}
else if(map[ri[i]][rj[i]]=='H'&&jude_H(ri[i],rj[i])==0)
{
return 0;
}
else if(map[ri[i]][rj[i]]=='C'&&jude_C(ri[i],rj[i])==0)
{
return 0;
}
}
return 1;
}
int main()
{
while(cin>>n>>bi>>bj&&n)
{
flag=0;
for(int i=0;i<11;i++)
for(int j=0;j<10;j++)
map[i][j]='0' ;
for(int i=1;i<=n;i++)
{
cin>>r>>ri[i]>>rj[i];
map[ri[i]][rj[i]]=r;
}
a=bi;
b=bj;
for(int i=1;i<=n;i++)
{
if(map[ri[i]][rj[i]]=='G'&&jude_G(ri[i],rj[i])==0)
{
flag=1;
break;
}
}
if(flag==0)
{
for(int i=0;i<4;i++)
{
if(bi+ch1[i]>0&&bi+ch1[i]<4&&bj+ch2[i]>3&&bj+ch2[i]<7)
{
a=bi+ch1[i];
b=bj+ch2[i];
if(jude())
{
flag=1;
break;
}
}
}
}
if(flag==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}