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 }

 

你可能感兴趣的:(uva)