TZOJ 4746 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 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”.
输入
The
input contains multiple 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.
输出
For each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘NO’.
样例输入
2 1 4
G 10 5
R 6 4
3 1 5
H 4 5
G 10 5
C 7 5
0 0 0
样例输出
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.

题意
黑方只右1个将,红方有车马炮帅,当前黑方走,判断红方是否已经将死黑方
题解
模拟将能走的4个点,判断这个点是否被红子杀
代码
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<iostream> 5 using namespace std; 6 int Map1[15][15],Map2[15][15],Dead[15][15];//Map1原来的棋盘,2将移动后的棋盘 7 void Up1(int x,int y)//帅 8 { 9 for(int i=x-1;i>=1;i--)//往上找 10 { 11 if(Map2[i][y]>=1)//遇到阻拦 12 break; 13 Dead[i][y]=0;//帅杀死的区域 14 } 15 } 16 void Up2(int x,int y)//车 17 { 18 for(int i=x-1;i>=1;i--)//往上找 19 { 20 if(Map2[i][y]>=1)//遇到阻拦 21 break; 22 Dead[i][y]=0;//车杀死 23 } 24 for(int i=x+1;i<=10;i++)//下 25 { 26 if(Map2[i][y]>=1) 27 break; 28 Dead[i][y]=0; 29 } 30 for(int j=y-1;j>=1;j--)//左 31 { 32 if(Map2[x][j]>=1) 33 break; 34 Dead[x][j]=0; 35 } 36 for(int j=y+1;j<=9;j++)//右 37 { 38 if(Map2[x][j]>=1) 39 break; 40 Dead[x][j]=0; 41 } 42 } 43 int dx[]={0,0,-1,1};//右,左,下,上 44 int dy[]={1,-1,0,0}; 45 int ddx[]={-1,1,-1,1,-2,-2,2,2}; 46 int ddy[]={2,2,-2,-2,-1,1,-1,1}; 47 bool bj3(int i,int j)//马边界 48 { 49 if(i>=1&&i<=10&&j>=1&&j<=9) 50 return true; 51 return false; 52 } 53 void Up3(int x,int y)//马 54 { 55 for(int i=0;i<4;i++) 56 { 57 //马脚 58 if(Map2[x+dx[i]][y+dy[i]]>=1)continue; 59 //马杀死 60 if(bj3(x+ddx[2*i],y+ddy[2*i]))Dead[x+ddx[2*i]][y+ddy[2*i]]=0; 61 if(bj3(x+ddx[2*i+1],y+ddy[2*i+1]))Dead[x+ddx[2*i+1]][y+ddy[2*i+1]]=0; 62 63 } 64 } 65 void Up4(int x,int y)//跑 66 { 67 int F=0; 68 for(int i=x-1;i>=1;i--)//上 69 { 70 if(Map2[i][y]>=1) 71 F++; 72 if(F==1)//架炮 73 Dead[i][y]=0; 74 if(F==2)//阻拦 75 break; 76 } 77 F=0; 78 for(int i=x+1;i<=10;i++)//下 79 { 80 if(Map2[i][y]>=1) 81 F++; 82 if(F==1) 83 Dead[i][y]=0; 84 if(F==2) 85 break; 86 } 87 F=0; 88 for(int j=y-1;j>=1;j--)//左 89 { 90 if(Map2[x][j]>=1) 91 F++; 92 if(F==1) 93 Dead[x][j]=0; 94 if(F==2) 95 break; 96 } 97 F=0; 98 for(int j=y+1;j<=9;j++)//右 99 { 100 if(Map2[x][j]>=1) 101 F++; 102 if(F==1) 103 Dead[x][j]=0; 104 if(F==2) 105 break; 106 } 107 } 108 bool bj(int i,int j)//将边界 109 { 110 if(1<=i&&i<=3&&4<=j&&j<=6) 111 return true; 112 return false; 113 } 114 void Clear() 115 { 116 for(int i=1;i<=10;i++) 117 for(int j=1;j<=9;j++) 118 Map2[i][j]=Map1[i][j],Dead[i][j]=1; 119 } 120 void Updata() 121 { 122 for(int i=1;i<=10;i++) 123 { 124 for(int j=1;j<=9;j++) 125 { 126 if(Map2[i][j]==1)//帅 127 Up1(i,j); 128 if(Map2[i][j]==2)//车 129 Up2(i,j); 130 if(Map2[i][j]==3)//马 131 Up3(i,j); 132 if(Map2[i][j]==4)//炮 133 Up4(i,j); 134 } 135 } 136 } 137 int main() 138 { 139 int n,a,b,hx,hy,gx,gy; 140 char c; 141 //freopen("in.txt","r",stdin); 142 //freopen("out.txt","w",stdout); 143 while(scanf("%d%d%d",&n,&hx,&hy)!=EOF,n||hx||hy) 144 { 145 memset(Map1,0,sizeof(Map1)); 146 for(int i=1;i<=n;i++) 147 { 148 c=getchar(); 149 while(c==' ')c=getchar();//不加会错,估计卡空格了 150 scanf("%c %d %d",&c,&a,&b); 151 if(c=='G')//帅 152 Map1[a][b]=1,gx=a,gy=b; 153 if(c=='R')//车 154 Map1[a][b]=2; 155 if(c=='H')//马 156 Map1[a][b]=3; 157 if(c=='C')//炮 158 Map1[a][b]=4; 159 } 160 int win=1; 161 //黑方直接飞将杀死红方(TOJ上是不考虑,其他平台要考虑) 162 //题意没说清,按残局来说的话这样毫无意义 163 /* 164 if(hy==gy) 165 { 166 int F=1; 167 for(int i=gx-1;i>=hx;i--) 168 { 169 if(Map1[i][gy]>=1) 170 { 171 F=0;break; 172 } 173 } 174 if(F) 175 { 176 printf("NO\n"); 177 continue; 178 } 179 } 180 */ 181 for(int i=0;i<4;i++) 182 { 183 if(!bj(hx+dx[i],hy+dy[i]))continue; 184 Clear(); 185 Map2[hx+dx[i]][hy+dy[i]]=0;//吃子 186 Updata(); 187 if(Dead[hx+dx[i]][hy+dy[i]]) 188 { 189 win=0;break; 190 } 191 } 192 if(win)printf("YES\n"); 193 else printf("NO\n"); 194 } 195 return 0; 196 }
浙公网安备 33010602011771号