Uva 1589 Xiangqi (ACM/ICPC Fuzhou 2011)

hdu 4121 Xiangqi

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.

Source
2011 Asia Fuzhou Regional Contest

扫描二维码关注公众号,回复: 6241079 查看本文章
1. 用四个不定长数组分别把R,G,C,H,的坐标存储下来,然后模拟将上下左右四个方向看是否被将死
2. 开局的情况不用判断,因为题目说了刚开始红方已经将军,所以最多只要判断四种情况
3. 模拟某步时注意吃子情况,这步模拟完后要要将吃的字回撤
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define read() freopen("input.txt","r",stdin);
#define write() freopen("output.txt","w",stdout);
using namespace std;
char chess[15][15];
struct E{
	int erow,ecol;
	E(){erow=0;ecol=0;}
};
vector<E>v[6];
int row=0,col=0,_row=0,_col=0;
bool flag=false;
bool check(){
	for( int i=0; i<v[1].size(); i++ ){
		_row=v[1][i].erow;_col=v[1][i].ecol;
		if(row==_row&&col!=_col){
			flag=true;
			for( int j=min(col,_col)+1; j<max(col,_col); j++ ){
				if(chess[row][j]!='\0'){
					flag=false;break;
				}
			}
			if(flag) return flag;
		}
		else if(col==_col&&row!=_row){
			flag=true;
			for( int j=min(row,_row)+1; j<max(row,_row); j++ ){
				if(chess[j][col]!='\0'){
					flag=false;break;
				}
			}
			if(flag) return flag;
		}
	}
	_row=v[2][0].erow;_col=v[2][0].ecol;
	if(col==_col&&row!=_row){
		flag=true;
		for( int j=min(row,_row)+1; j<max(row,_row); j++ ){
			if(chess[j][col]!='\0'){
				flag=false;break;
			}
		}
		if(flag) return flag;
	}
	for( int i=0; i<v[3].size(); i++ ){
		_row=v[3][i].erow;_col=v[3][i].ecol;int cnt=0;
		if(row==_row&&col!=_col){
			for( int j=min(col,_col)+1; j<max(col,_col); j++ ){
				if(chess[row][j]!='\0') cnt++;
			}
			
			if(cnt==1) return true;
		}
		else if(col==_col&&row!=_row){
			for( int j=min(row,_row)+1; j<max(row,_row); j++ ){
				if(chess[j][col]!='\0') cnt++;
			}
			if(cnt==1) return true;
		}
	}
	for( int i=0; i<v[4].size(); i++ ){
		_row=v[4][i].erow;_col=v[4][i].ecol;
		if(abs(row-_row)==1&&abs(col-_col)==2&&chess[_row][_col+(col-_col)/2]=='\0'||abs(row-_row)==2&&abs(col-_col)==1&&chess[_row+(row-_row)/2][_col]=='\0') return true;
	}
	return false;
}
int main() {
	read();write();
	int n;
	while(scanf("%d %d %d",&n,&row,&col)==3&&n){
		memset(chess,0,sizeof(chess));
		for( int i=1; i<=4; i++ ) v[i].clear();
		E temp;
		for( int i=0; i<n; i++ ){
			char ch;
			cin>>ch>>temp.erow>>temp.ecol;
			chess[temp.erow][temp.ecol]=ch;
			if(ch=='R') v[1].push_back(temp);
			else if(ch=='G') v[2].push_back(temp);
			else if(ch=='C') v[3].push_back(temp);
			else if(ch=='H') v[4].push_back(temp);
		}
		bool ok;
		if(row-1>=1&&row-1<=3){
			char ch=chess[row-1][col];
			chess[row][col]='\0';row--;chess[row][col]='G';
			ok=check();
			if(ok==false){ cout<<"NO\n";continue;}
			chess[row][col]=ch;row++;chess[row][col]='G';
		}
		if(row+1<=3&&row+1>=1){
			char ch=chess[row+1][col];
			chess[row][col]='\0';row++;chess[row][col]='G';
			ok=check();
			if(ok==false){ cout<<"NO\n";continue;}
			chess[row][col]=ch;row--;chess[row][col]='G';
		}
		if(col-1>=4&&col-1<=6){
			char ch=chess[row][col-1];
			chess[row][col]='\0';col--;chess[row][col]='G';
			ok=check();
			if(ok==false){ cout<<"NO\n";continue;}
			chess[row][col]=ch;col++;chess[row][col]='G';
		}
		if(col+1<=6&&col+1>=4){
			char ch=chess[row][col+1];
			chess[row][col]='\0';col++;chess[row][col]='G';
			ok=check();
			if(ok==false){ cout<<"NO\n";continue;}
			chess[row][col]=ch;col--;chess[row][col]='G';
		}
		cout<<"YES\n";
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43323172/article/details/89738855
今日推荐