UVa1589

1589 Xiangqi
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 en-
emy 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 \check-
mate".
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 \ ying general" (see the gure above).
\Flying general" means that one general can \ y" 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 gure.
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 gure below), which is called \hobbling the
horse's leg".
Universidad de Valladolid OJ: 1589 { Xiangqi 2/3
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 rst 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'.
Hint: In the rst situation, the black general is checked by
chariot and \ ying general". In the second situation, the
black general can move to (1, 4) or (1, 6) to stop check. See
the gure on the right.
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
Universidad de Valladolid OJ: 1589 { Xiangqi 3/3
Sample Output
YES
NO

 

题意:

       在10x9的象棋棋盘中有一个黑将、一个红帅,还可能有红车、红炮、红马。分别给出它们的位置,下一步该黑棋走,判断黑棋是否被将死。

 

输入:

       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’.

输出:

       YES or NO

 

分析:

       模拟题。对每一种棋子的走法编写模拟函数。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int MAX_INDEX = 12;
 5 char brd[MAX_INDEX][MAX_INDEX];
 6 int dx[] = {-1,1,0,0},dy[] = {0,0,-1,1};
 7 int hx[] = {-2,-1,-2,-1,1,2,1,2};  //马将军
 8 int hy[] = {-1,-2,1,2,-2,-1,2,1};
 9 int tx[] = {-1,-1,-1,-1,1,1,1,1};  //马蹩脚
10 int ty[] = {-1,-1,1,1,-1,-1,1,1};
11 int cr[2],cc[2];  //记录炮的坐标
12 int check(int r,int c){ // 检查坐标(r,c)是否可以走
13     int i,j,k,tr,tc,cnt;
14     if(r < 1 || r > 3 || c < 4 || c > 6) return 1;
15     for (j = c - 1; j > 0; --j)   //行被车
16         if(brd[r][j])
17             if(brd[r][j] == 'R') return 1;
18             else break;
19     for (j = c + 1; j <= 9; ++j)
20         if(brd[r][j])
21             if(brd[r][j] == 'R') return 1;
22             else break;
23     for (i = r - 1; i > 0; --i)  //列被车
24         if(brd[i][c])
25             if(brd[i][c] == 'R') return 1;
26             else break;
27     for (i = r + 1; i <= 10; ++i) //列被车或将
28         if(brd[i][c])
29             if(brd[i][c] == 'R' || brd[i][c] == 'G') return 1;
30             else break;
31     for(int k = 0; k < 2; ++k){    //被炮将军
32         if(cr[k] == r){   //行有炮
33             for(j = c - 1, cnt = 0; j > cc[k]; --j) if(brd[r][j]) ++cnt;
34             if(cnt == 1) return 1;
35             for(j = c + 1, cnt = 0; j < cc[k]; ++j) if(brd[r][j]) ++cnt;
36             if(cnt == 1) return 1;
37         }
38         if(cc[k] == c){   //列有炮
39             for(i = r - 1, cnt = 0; i > cr[k]; --i) if(brd[i][c]) ++cnt;
40             if(cnt == 1) return 1;
41             for(i = r + 1, cnt = 0; i < cr[k]; ++i) if(brd[i][c]) ++cnt;
42             if(cnt == 1) return 1;
43         }
44     }
45     for(int k = 0; k < 8; ++k){  //被马将军
46         tr = r + hx[k], tc = c + hy[k];
47         if(tr < 1 || tr > 10 || tc < 1 || tc > 9) continue;
48         if(brd[tr][tc] == 'H' && (!brd[r + tx[k]][c + ty[k]]))
49             return 1;
50     }
51 
52     return 0;
53 }
54 int main(){
55     char s[5];
56     int n, r, c, x, y;
57     while(scanf("%d%d%d", &n, &r, &c), n || r || c){
58         memset(brd, 0, sizeof(brd));
59         cr[0] = cc[0] = cr[1] = cc[1] = 0;
60         while(n--){
61             scanf("%s%d%d", s, &x, &y);
62             if(s[0] == 'C'){
63                 if(cr[0]) cr[1] = x, cc[1] = y;
64                 else cr[0] = x, cc[0] = y;
65             }
66             brd[x][y] = s[0];
67         }
68         int cnt = 0;
69         for(int i = 0; i < 4; ++i)
70             cnt += check(r + dx[i],c + dy[i]);
71         if(cnt < 4) puts("NO");
72         else puts("YES");
73     }
74     return 0;
75 }
View Code

 

posted @ 2016-08-18 00:05  Yan_Bin  阅读(846)  评论(2编辑  收藏  举报