HDU 4121 Xiangqi 模擬
原題: 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): 4809 Accepted Submission(s): 1134
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”.
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’.
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
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 above.
思路:
對於給定的棋盤。黑棋僅僅有一個,紅棋有n個,如今黑將能夠走一步。假設怎麽走都活不了。就是被將死了。輸出YES。
這道題我們僅僅須要模擬每一個紅棋的攻擊範圍,最後看黑將的能走的地方是否在攻擊範圍就能夠了。
代碼:
#include <iostream>
#include"cstdio"
#include"stdlib.h"
#include"string.h"
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int mp[12][11];
int t,sx,sy;
while(scanf("%d %d %d",&t,&sx,&sy)!=EOF)
{
if(t==0&&sx==0&&sy==0) break;
memset(mp,0,sizeof(mp));
while(t--)
{
int x,y;
char s[10];
scanf("%s %d %d",s,&x,&y);
//由於帥和車的攻擊範圍一樣,所以我們這裏把帥和車看作同一種棋
if(s[0]==‘G‘)//帥
mp[x][y]=2;
else if(s[0]==‘R‘)//車
mp[x][y]=2;
else if(s[0]==‘H‘)//馬
mp[x][y]=3;
else if(s[0]==‘C‘)//炮
mp[x][y]=4;
}
//用來存儲攻擊範圍
bool flag[11][10];
memset(flag,false,sizeof(flag));
//遍歷每一個點
for(int i=1; i<=10; i++)
{
for(int j=1; j<=9; j++)
{
//枚舉每一種棋
if(mp[i][j]==2)
{
//向右
for(int k=j+1; k<=9; k++)
{
//該方向的全部點都該為被攻擊區域
flag[i][k]=1;
//除非遇到有棋子遮擋。但那個棋子的位置是能夠被攻擊的,
//後面的位置才不會被攻擊。所以這裏先標記再推斷。
//防止黑將的下一步能夠吃掉你那裏的棋子,但
//是那個棋子之前占的地方並不在你的攻擊範圍內
if(mp[i][k])
break;
}
//向左
for(int k=j-1; k>=1; k--)
{
flag[i][k]=1;
if(mp[i][k])
break;
}
//向下
for(int k=i+1; k<=10; k++)
{
flag[k][j]=1;
if(mp[k][j])
break;
}
//向上
for(int k=i-1; k>=1; k--)
{
flag[k][j]=1;
if(mp[k][j])
break;
}
}
//考慮到出界問題,所以這裏分成8種情況
//實際上把數組數組往右下角再平移一格就不用操心了
//可是那樣寫代碼有點痛苦
else if(mp[i][j]==3)
{
//向左上
if(mp[i][j-1]==0&&j>=3&&i>=2)
{
flag[i-1][j-2]=1;
}
//向左下
if(mp[i][j-1]==0&&j>=3&&i<=9)
{
flag[i+1][j-2]=1;
}
//向右上
if(mp[i][j+1]==0&&j<=8&&i>=2)
{
flag[i-1][j+2]=1;
}
//向右下
if(mp[i][j+1]==0&&j<=8&&i<=9)
{
flag[i+1][j+2]=1;
}
//向上左
if(mp[i-1][j]==0&&i>=3&&j>=2)
{
flag[i-2][j-1]=1;
}
//向上右
if(mp[i-1][j]==0&&i>=3&&j<=8)
{
flag[i-2][j+1]=1;
}
//向下左
if(mp[i+1][j]==0&&i<=7&&j>=2)
{
flag[i+2][j-1]=1;
}
//向下右
if(mp[i+1][j]==0&&i<=7&&j<=8)
{
flag[i+2][j+1]=1;
}
}
else if(mp[i][j]==4)
{
int tflag=0;
//向右
for(int k=j+1; k<=9; k++)
{
//當前沒有炮架子的時候並掃描到了有棋能夠當架子
if(tflag==0&&mp[i][k]!=0)
tflag=1;
//有架子後面的都是被攻擊範圍
else if(tflag==1)
{
flag[i][k]=1;
//一旦遇到有棋的地方。該棋是在攻擊範圍內的。
//可是後面的不在了,所以要跳出循環
if(mp[i][k]) break;
}
}
tflag=0;
//向左
for(int k=j-1; k>=1; k--)
{
if(tflag==0&&mp[i][k]!=0)
tflag=1;
else if(tflag==1)
{
flag[i][k]=1;
if(mp[i][k]) break;
}
}
tflag=0;
//向下
for(int k=i+1; k<=10; k++)
{
if(tflag==0&&mp[k][j]!=0)
tflag=1;
else if(tflag==1)
{
flag[k][j]=1;
if(mp[k][j]) break;
}
}
tflag=0;
//向上
for(int k=i-1; k>=1; k--)
{
if(tflag==0&&mp[k][j]!=0)
tflag=1;
else if(tflag==1)
{
flag[k][j]=1;
if(mp[k][j]) break;
}
}
}
}
}
//將地圖上黑方城外都標記為攻擊區域,就是說走出界也是在攻擊範圍內
for(int i=4;i<=6;i++)
{
flag[0][i]=1;
flag[4][i]=1;
}
for(int i=1;i<=3;i++)
{
flag[i][3]=1;
flag[i][7]=1;
}
// for(int i=0; i<=11; i++)
// {
// for(int j=0; j<=10; j++)
// {
// printf("%d ",flag[i][j]);
// }
// printf("\n");
// }
//當4邊都是攻擊範圍就GG了
if(flag[sx+1][sy]&&flag[sx-1][sy]&&flag[sx][sy+1]&&flag[sx][sy-1])
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
HDU 4121 Xiangqi 模擬