Xiangqi
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3614 Accepted Submission(s): 877
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”.
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.
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
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.
Hint
这条题直接模拟 ~ 黑色将上下左右移动一次 , 看看有没被将死 。
注意一下将军可以把红旗吃了一只获得到自己的安全
代码写的比较恶心~
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int N = 15; const int M = 55 ; int dx[] = {0 , 0 , 1 , -1 }; int dy[] = {1 , -1 , 0 , 0 }; int movex[] = { 1 , -1 , 1 , -1 , 2 , 2, -2 , -2 }; int movey[] = { 2 , 2 , -2, -2 , 1 , -1, 1, -1 }; struct node { char w; int x , y ; } e[N]; int n ; int danger[N][N]; char mp[N][N]; void test() { for( int i = 1 ; i <= 10 ; ++i ){ for( int j = 1 ; j <= 9 ; ++j ){ cout << danger[i][j] << ' '; } cout << endl; } } bool inside( int x , int y ) { if( x < 1 || x > 10 || y < 1 || y > 9 ) return false; return true; } bool inside2( int x , int y ) { if( x > 3 || x < 1 || y < 4 || y > 6 )return false ; return true; } bool check( int x , int y ) { // cout << x <<' ' << y << endl; if( danger[x][y] == 3 ) return false ; else { for( int j = x - 1 ; j > 0 ; --j ){ if( mp[j][y] == 'R' || mp[j][y] == 'G' ) return false; if( danger[j][y] == 1 ) break ; } for( int j = x + 1 ; j < N ; ++j ){ if( mp[j][y] == 'R' || mp[j][y] == 'G' ) return false; if( danger[j][y] == 1 ) break ; } for( int j = y - 1 ; j > 0 ; --j ){ if( mp[x][j] == 'R' || mp[x][j] == 'G' ) return false; if( danger[x][j] == 1 ) break ; } for( int j = y + 1 ; j < N ; ++j ){ if( mp[x][j] == 'R' || mp[x][j] == 'G' ) return false; if( danger[x][j] == 1 ) break ; } // cout << "pass1" << endl; int xx , yy , tag = 0 ; for( int j = x - 1 ; j > 0 ; --j ){ if( danger[j][y] == 1 ) { tag = 1 ; xx = j ; break ; } } if( tag ){ for( int j = xx - 1 ; j > 0 ; --j ){ if( mp[j][y] == 'C') return false; if( danger[j][y] == 1 ) break ; } } tag = 0 ; for( int j = x + 1 ; j < N ; ++j ){ if( danger[j][y] == 1 ) { tag = 1 ; xx = j ; break ; } } if( tag ){ for( int j = xx + 1 ; j < N ; ++j ){ if( mp[j][y] == 'C') return false; if( danger[j][y] == 1 ) break ; } } // ======================================== tag = 0 ; for( int j = y - 1 ; j >= 0 ; --j ){ if( danger[x][j] == 1 ) { tag = 1 ; yy = j ; break ; } } if( tag ){ for( int j = yy - 1 ; j > 0 ; --j ){ if( mp[x][j] == 'C') return false; if( danger[x][j] == 1 ) break ; } } tag = 0 ; for( int j = y + 1 ; j < N ; ++j ){ if( danger[x][j] == 1 ) { tag = 1 ; yy = j ; break ; } } if( tag ){ for( int j = yy + 1 ; j < N ; ++j ){ if( mp[x][j] == 'C') return false; if( danger[x][j] == 1 ) break ; } } } // cout << "pass2" << endl; return true ; } void run() { memset ( danger , 0, sizeof danger ); memset ( mp , 0, sizeof mp ); for( int i = 1 ; i <= n ;++i ){ cin >> e[i].w >> e[i].x >> e[i].y ; mp[ e[i].x ][ e[i].y ] = e[i].w; danger[ e[i].x ][ e[i].y ] = 1 ; } for( int i = 1; i <=n ; ++i ){ if( e[i].w == 'H' ){ for( int j = 0 ; j < 4 ; ++j ){ int x = e[i].x + dx[j] , y = e[i].y + dy[j] ; if( !inside(x,y) || danger[x][y] == 1 ) continue ; danger[ e[i].x + movex[ 2*j ] ][ e[i].y + movey[ 2*j ] ] = 3 ; danger[ e[i].x + movex[ 2*j + 1 ] ][ e[i].y + movey[ 2*j + 1 ] ] = 3 ; } } } // test(); for( int i = 0 ; i < 4 ; ++i ){ int x = e[0].x + dx[i] , y = e[0].y + dy[i] ; if( !inside2(x,y) ) continue ; if( check(x,y) ) { cout << "NO" << endl; return ; } } cout << "YES" << endl ; } int main() { #ifdef LOCAL freopen("in","r",stdin); #endif ios::sync_with_stdio(0); while( cin >> n >> e[0].x >> e[0].y ){ if( !n && !e[0].x && !e[0].y ) break ; run(); } return 0; }