UVA1589 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 x 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 ( 2N7) 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 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 below.

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看到这么个问题确实有点头疼,但是仔细想过之后不难用模拟来解决问题,附上代码(略长);
  1 #include<stdio.h>
  2 #include<string.h>
  3 struct L
  4 {
  5     int a,b;
  6 };
  7 struct L General;
  8 struct L Chariot[2];
  9 struct L Horse[2];
 10 struct L Cannon[2];
 11 int Qipan[11][10];
 12 int n,x,y;
 13 int tempx,tempy;
 14 char type[10];
 15 int i1,i2,i3;
 16 int mainflag;
 17 int checkGenerals(int,int);
 18 int checkChariot(int,int,int,int);
 19 int checkHorse(int,int,int,int);
 20 int checkCannon(int,int,int,int);
 21 int checkPoint(int,int);
 22 int main()
 23 {
 24     while(scanf("%d%d%d",&n,&x,&y)==3&&n)
 25     //Pay attention to the point (x,y),which is the black general located.
 26     {
 27         memset(Qipan,0,sizeof(Qipan));
 28         //Create a Qipan and define all of it are 0.
 29         Qipan[x][y]=1;
 30         // When put some point on the broad,transfer it into 1.
 31         //input the date,and store it into struct General,Chariot,Horse,Cannon.
 32         i1=i2=i3=0;
 33         for(int i=0;i<n;i++)
 34         {
 35             scanf("%s%d%d",type,&tempx,&tempy);
 36             Qipan[tempx][tempy]=1;
 37             if(type[0]==‘G‘)
 38             {
 39                 General.a=tempx;
 40                 General.b=tempy;
 41             }
 42             else if(type[0]==‘R‘)
 43             {
 44                 Chariot[i1].a=tempx;
 45                 Chariot[i1++].b=tempy;
 46             }
 47             else if(type[0]==‘H‘)
 48             {
 49                 Horse[i2].a=tempx;
 50                 Horse[i2++].b=tempy;
 51             }
 52             else if(type[0]==‘C‘)
 53             {
 54                 Cannon[i3].a=tempx;
 55                 Cannon[i3++].b=tempy;
 56             }
 57         }
 58         // Now it‘s high time to deal with the problem,with all of the dates are stored in order.
 59         // The first we must check whether the two generals face to face directly.
 60         if(checkGenerals(x,y))
 61         {
 62             printf("NO\n");
 63             continue;
 64         }
 65         mainflag=0;
 66         if(x+1<4)
 67         {
 68             Qipan[x][y]=0;
 69             Qipan[x+1][y]++;
 70             if(checkPoint(x+1,y))
 71                 mainflag=1;
 72             Qipan[x][y]=1;
 73             Qipan[x+1][y]--;
 74         }
 75         if(mainflag)
 76         {
 77             printf("NO\n");
 78             continue;
 79         }
 80         if(x-1>0)
 81         {
 82             Qipan[x][y]=0;
 83             Qipan[x-1][y]++;
 84             if(checkPoint(x-1,y))
 85                 mainflag=1;
 86             Qipan[x][y]=0;
 87             Qipan[x-1][y]--;
 88         }
 89         if(mainflag)
 90         {
 91             printf("NO\n");
 92             continue;
 93         }
 94         if(y+1<7)
 95         {
 96             Qipan[x][y]=0;
 97             Qipan[x][y+1]++;
 98             if(checkPoint(x,y+1))
 99                 mainflag=1;
100             Qipan[x][y]=0;
101             Qipan[x][y+1]--;
102         }
103         if(mainflag)
104         {
105             printf("NO\n");
106             continue;
107         }
108         if(y-1>3)
109         {
110             Qipan[x][y]=0;
111             Qipan[x][y-1]++;
112             if(checkPoint(x,y-1))
113                 mainflag=1;
114             Qipan[x][y]=1;
115             Qipan[x][y-1]++;
116         }
117         if(mainflag)
118         {
119             printf("NO\n");
120             continue;
121         }
122         printf("YES\n");
123     }
124 }
125 int checkGenerals(int x,int y)
126 {
127     if(General.b==y)
128     {
129         int Flag=0;
130         for(int i=x+1;i<General.a;i++)
131             if(Qipan[i][y])
132                 Flag++;
133         if(Flag==0)
134             return 1;
135     }
136     return 0;
137 }
138 int checkChariot(int x1,int y1,int x2,int y2)
139 {
140     int Flag;
141     int xj1,yj1,xj2,yj2;
142     xj1=x1<x2?x1:x2;
143     xj2=x1>x2?x1:x2;
144     yj1=y1<y2?y1:y2;
145     yj2=y1>y2?y1:y2;
146     if(x1==x2&&y1==y2)
147         return 0;
148     if(x1==x2)
149     {
150         Flag=0;
151         for(int j=yj1+1;j<yj2;j++)
152             if(Qipan[x1][j])
153                 Flag=1;
154         if(Flag==0) return 1;
155     }
156     if(y1==y2)
157     {
158         Flag=0;
159         for(int i=xj1+1;i<xj2;i++)
160             if(Qipan[i][y1])
161                 Flag=1;
162         if(Flag==0) return 1;
163     }
164     return 0;
165 }
166 int checkHorse(int x1,int y1,int x2,int y2)
167 {
168     if(x1+1==x2&&y1+2==y2)
169         if(Qipan[x1][y1+1]==0)
170             return 1;
171     if(x1+1==x2&&y1-2==y2)
172         if(Qipan[x1][y1-1]==0)
173             return 1;
174     if(x1-1==x2&&y1+2==y2)
175         if(Qipan[x1][y1+1]==0)
176             return 1;
177     if(x1-1==x2&&y1-2==y2)
178         if(Qipan[x1][y1-1]==0)
179             return 1;
180     if(x1+2==x2&&y1+1==y2)
181         if(Qipan[x1+1][y1]==0)
182             return 1;
183     if(x1+2==x2&&y1-1==y2)
184         if(Qipan[x1+1][y1]==0)
185             return 1;
186     if(x1-2==x2&&y1+1==y2)
187         if(Qipan[x1-1][y1]==0)
188             return 1;
189     if(x1-2==x2&&y1-1==y2)
190         if(Qipan[x1-1][y1]==0)
191             return 1;
192     return 0;
193 }
194 int checkCannon(int x1,int y1,int x2,int y2)
195 {
196     int Flag=0;
197     int xj1,yj1,xj2,yj2;
198     xj1=x1<x2?x1:x2;
199     xj2=x1>x2?x1:x2;
200     yj1=y1<y2?y1:y2;
201     yj2=y1>y2?y1:y2;
202     if(x1==x2&&y1==y2) return 0;
203     if(x1==x2)
204     {
205         for(int j=yj1+1;j<yj2;j++)
206             if(Qipan[x1][j]) Flag++;
207         if(Flag==1) return 1;
208     }
209     if(y1==y2)
210     {
211         for(int i=xj1+1;i<xj2;i++)
212             if(Qipan[i][y1]) Flag++;
213         if(Flag==1) return 1;
214     }
215     return 0;
216 }
217 int checkPoint(int x,int y)
218 {
219     if(checkGenerals(x,y))
220         return 0;
221     for(int i=0;i<i1;i++)
222         if(checkChariot(Chariot[i].a,Chariot[i].b,x,y))
223             return 0;
224     for(int i=0;i<i2;i++)
225         if(checkHorse(Horse[i].a,Horse[i].b,x,y))
226             return 0;
227     for(int i=0;i<i3;i++)
228     {
229         if(checkCannon(Cannon[i].a,Cannon[i].b,x,y))
230             return 0;
231     }
232     return 1;
233 }
时间: 2024-11-07 07:13:23

UVA1589 Xiangqi的相关文章

紫书第四章训练 UVA1589 Xiangqi by 15 周泽玺

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

UVa1589 Xiangqi 坑点总结+参考代码

题目大意: 给出一个合乎规则的象棋残局,棋盘上黑方只有将,红方已经“delivered a check”.要求判断红方能否把黑方“checkmate”. 思路: 1. 维护一个矩阵表示棋盘上红方棋子的布局: 2. 维护一个vector存储红方的棋子的位置,以便于后续访问: 3. 模拟黑将走一步的过程,对于每个黑将可能走的位置,判断黑将是否会被吃掉.如果所有能走的位置上黑将都会被吃掉,那么输出YES.如果存在一个位置使黑将逃脱,输出NO. 坑点: 1. 黑将走到下一步时也可以吃掉红方. 2. 虽然

Xiangqi UVa1589(多次WA)

1 #include<stdio.h> 2 #include<string.h> 3 #define maxn 8 4 //#define LOCAL 5 #include<iostream> 6 using namespace std; 7 int test(); 8 int count(int,int); 9 int horse_judge(int); 10 int hobble(int,int); 11 12 int G1, G2; //(G1,G2) of bl

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

UVA 1589 Xiangqi

Z1589 - Xiangqi Time limit: 3.000 seconds 做这题的时候WA了很多次. 解决思路是,枚举黑方将军可以移动的位置,接着判断这些位置是否被红方将军,如果所有位置会被红方吃掉,那么就是checkmate了. 要注意的情况可能就是双炮将军. 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 char board[12][12]; 6 int dr[4] = { 1,

HDU 4121 Xiangqi

模拟吧,算是... 被这个题wa到哭,真是什么都不想说了...上代码 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 struct node { 6 int x,y; 7 char c; 8 }q[10]; 9 10 int main (){ 11 int n,x,y; 12 int kase=0; 13 int map[30][30]; 14 while (cin>>n>

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 pop

UVa1589

1589 XiangqiXiangqi is one of the most popular two-player board games in China. The game represents a battlebetween two armies with the goal of capturing the enemy's \general" piece. In this problem, you aregiven a situation of later stage in the gam

UVA - 1589 Xiangqi (模拟)

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