题意:
给出一个黑方的将, 然后 红方有 2 ~ 7 个棋子, 给出摆放位置,问是否已经把黑将将死, 红方已经将军。
分析:
分情况处理, 车 马 炮, 红将情况跟车是一样的。
建一个数组board保存棋局, 然后建一个数组moveable用来算出黑将当前位置能走的格子(1可走, 0不可走, 一开始都是1), 判断一下黑将上下左右四个方位是否能走, 而且走了之后算一下当前位置moveable是否0即可。
代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 int N, bx, by; 4 int board[11][10]; 5 int tboard[11][10]; 6 int movable[11][10]; 7 struct CHESS 8 { 9 char name; 10 int x, y; 11 }chess[10]; 12 void Chariot(int x, int y){ 13 for(int i = x - 1; i >= 1; i--){ 14 movable[i][y] = 0; 15 if(tboard[i][y] && tboard[i][y] != ‘B‘) 16 break; 17 } 18 for(int i = x + 1; i <= 10; i++){ 19 movable[i][y] = 0; 20 if(tboard[i][y] && tboard[i][y] != ‘B‘) 21 break; 22 } 23 for(int i = y + 1; i <= 9; i++){ 24 movable[x][i] = 0; 25 if(tboard[x][i] && tboard[x][i] != ‘B‘) 26 break; 27 } 28 for(int i = y - 1; i >= 1; i --){ 29 movable[x][i] = 0; 30 if(tboard[x][i] && tboard[x][i] != ‘B‘) 31 break; 32 } 33 } 34 void Cannon(int x, int y) 35 { 36 for(int i = x - 1; i >= 1; i --){ 37 if(tboard[i][y] && tboard[i][y] != ‘B‘){ 38 for(int j = i - 1; j >= 1; j--){ 39 movable[j][y] = 0; 40 if(tboard[j][y] && tboard[j][y] != ‘B‘) 41 break; 42 } 43 break; 44 } 45 } 46 for(int i = x + 1; i <= 10 ; i++){ 47 if(tboard[i][y] && tboard[i][y] != ‘B‘){ 48 for(int j = i+1; j <= 10; j++){ 49 movable[j][y] = 0; 50 if(tboard[j][y] && tboard[j][y] != ‘B‘) 51 break; 52 } 53 break; 54 } 55 } 56 for(int i = y - 1; i >= 1; i--){ 57 if(tboard[x][i] && tboard[x][i] != ‘B‘){ 58 for(int j = i - 1; j >= 1; j--){ 59 movable[x][j] = 0; 60 if(tboard[x][j] && tboard[j][y] != ‘B‘) 61 break; 62 } 63 break; 64 } 65 } 66 67 for(int i = y + 1; i <= 9; i++){ 68 if(tboard[x][i] && tboard[x][i] != ‘B‘){ 69 for(int j = i + 1; j <= 9; j++){ 70 movable[x][j] = 0; 71 if(tboard[x][j] && tboard[j][y] != ‘B‘) 72 break; 73 } 74 break; 75 } 76 } 77 78 } 79 int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};//上右下左 80 int dir2[8][2] = {{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}}; 81 void Horse(int x, int y) 82 { 83 for(int i = 0; i < 4; i++){ 84 int tx = x, ty = y; 85 tx += dir[i][0]; 86 ty += dir[i][1]; 87 if(tx >= 1 && ty >= 1 && tx <= 10 && ty <= 9); 88 else continue; 89 if(tboard[tx][ty]) continue; 90 tx = x; ty = y; 91 tx += dir2[(i+1)*2 - 2][0]; 92 ty += dir2[(i+1)*2 - 2][1]; 93 if(tx >= 1 && ty >= 1 && tx <= 10 && ty <= 9) 94 movable[tx][ty] = 0; 95 tx = x; ty = y; 96 tx += dir2[(i+1)*2 - 1][0]; 97 ty += dir2[(i+1)*2 - 1][1]; 98 if(tx >= 1 && ty >= 1 && tx <= 10 && ty <= 9) 99 movable[tx][ty] = 0; 100 } 101 } 102 int main() 103 { 104 105 while(scanf("%d %d %d", &N, &bx, &by) != EOF){ 106 if(N == 0) break; 107 memset(board, 0, sizeof(board)); 108 board[bx][by] = ‘B‘; 109 for(int i = 1; i <= 10; i++){ 110 for(int j = 1; j<= 9; j++){ 111 movable[i][j] = 1; 112 } 113 } 114 115 for(int i = 0; i < N; i++){ 116 int x, y; 117 char name; 118 scanf(" %c", &name); 119 scanf("%d %d", &x, &y); 120 board[x][y] = name; 121 } 122 int ok = 0; 123 for(int i = 0; i < 4; i++) 124 { 125 int tx = bx, ty = by; 126 tx += dir[i][0]; 127 ty += dir[i][1]; 128 if(tx >= 1 && tx <= 3 && ty >= 4 && ty <= 6); 129 else continue; 130 for(int k = 1; k <= 10; k++){ 131 for(int j = 1; j <= 10; j++){ 132 tboard[k][j] = board[k][j]; 133 } 134 } 135 tboard[tx][ty] = ‘B‘; 136 137 for(int k = 1; k <= 10; k++){ 138 for(int j = 1; j<= 9; j++){ 139 movable[k][j] = 1; 140 } 141 } 142 143 for(int k = 1; k <= 10; k++){ 144 for(int j = 1; j <= 9; j++){ 145 switch(tboard[k][j]){ 146 case ‘G‘: 147 Chariot(k,j); 148 break; 149 case ‘R‘: 150 Chariot(k,j); 151 break; 152 case ‘H‘: 153 Horse(k,j); 154 break; 155 case ‘C‘: 156 Cannon(k,j); 157 break; 158 } 159 } 160 } 161 162 if(movable[tx][ty]) 163 ok = 1; 164 } 165 printf("%s\n", ok ? "NO":"YES"); 166 167 } 168 return 0; 169 }
时间: 2024-11-05 23:20:15