这是道比较有意思的模拟题,读懂题意并考虑充分。
1 #include <cstdio> 2 #include <queue> 3 using namespace std; 4 5 struct P{ 6 int x, y; 7 P(){}; 8 P(int x, int y) :x(x), y(y){} 9 }; 10 11 char board[10][10]; 12 bool currplay;//true黑方下,fasle白方下 13 int dx[8] = { -1, -1, 0, 1, 1, 1, 0, -1 }; 14 int dy[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }; 15 16 bool find(int r, int c, bool re)//re为true时吃子 17 { 18 if (board[r][c] != '-') return false; 19 20 for (int k = 0; k < 8; k++){ 21 int nx = r + dx[k]; 22 int ny = c + dy[k]; 23 if (board[nx][ny] == (currplay ? 'W' : 'B')){ 24 queue<P> q; 25 for (; 1 <= nx && nx <= 8 && 1 <= ny && ny <= 8; nx += dx[k], ny += dy[k]){ 26 if (board[nx][ny] == '-') break; 27 if (board[nx][ny] == (currplay ? 'W' : 'B')){ 28 if (re) q.push(P(nx, ny)); 29 } 30 else if (board[nx][ny] == (currplay ? 'B' : 'W')){ 31 if (re){ 32 while (!q.empty()){ 33 P p = q.front(); 34 board[p.x][p.y] = (currplay ? 'B' : 'W'); 35 q.pop(); 36 } 37 } 38 else 39 return true; 40 break; 41 } 42 } 43 } 44 } 45 return false; 46 } 47 48 bool list() 49 { 50 int num = 0; 51 for (int i = 1; i <= 8; i++) 52 for (int j = 1; j <= 8; j++) 53 if (find(i,j,false)) 54 printf("%s(%d,%d)", num++ ? " " : "", i, j); 55 if (num) { 56 printf(" "); return true; 57 } 58 return false; 59 } 60 61 void move(int r, int c) 62 { 63 if (!find(r, c, false)) 64 currplay = !currplay; 65 find(r, c, true); 66 67 board[r][c] = currplay ? 'B' : 'W'; 68 int bnum = 0, wnum = 0; 69 for (int i = 1; i <= 8; i++) 70 for (int j = 1; j <= 8; j++){ 71 if (board[i][j] == 'B') bnum++; 72 else if (board[i][j] == 'W') wnum++; 73 } 74 currplay = !currplay; 75 printf("Black - %2d White - %2d ", bnum, wnum); 76 } 77 int main() 78 { 79 int ca; 80 char str[5]; 81 82 scanf("%d", &ca); 83 while (ca--) 84 { 85 getchar(); 86 for (int i = 1; i <= 8; i++){ 87 for (int j = 1; j <= 8; j++){ 88 scanf("%c", &board[i][j]); 89 } 90 getchar(); 91 } 92 scanf("%s", str); 93 currplay = (str[0] == 'B'); 94 while (str[0] != 'Q') 95 { 96 scanf("%s", str); 97 if (str[0] == 'L'){ 98 if (!list()) 99 printf("No legal move. "); 100 } 101 else if (str[0] == 'M'){ 102 int x = str[1] - '0'; int y = str[2] - '0'; 103 move(x, y); 104 } 105 else{ 106 for (int i = 1; i <= 8; i++){ 107 for (int j = 1; j <= 8; j++) 108 printf("%c", board[i][j]); 109 printf(" "); 110 } 111 if (ca) printf(" "); 112 } 113 } 114 } 115 }