一道纯模拟题, 曾经写了3次, 一直WA, 完全找不出错, 寒假的时候又是找不出来错, 然后。。。惊奇地发现多了个空行, 去掉, AC。。。真是醉了。
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 10
using namespace std;
int legal[MAXN][MAXN];
char chess, disk[MAXN][MAXN];
char other_chess() {
return chess == 'B' ? 'W' : 'B';
}
bool check() {
memset(legal, 0, sizeof(legal));
int p, q;
char other = other_chess();
bool ok = false;
for(int i = 0; i < MAXN; i++) //搜索当前棋子所在的位置
for(int j = 0; j < MAXN; j++)
if(disk[i][j] == chess) {
if(i - 1 >= 0&& j - 1 >= 0&& disk[i - 1][j - 1] == other) { //如果左上有其他子
p = i - 1;
q = j - 1;
while(p >= 0&& q >= 0) {
if(disk[p][q] == other) {p--; q--; continue;}
if(disk[p][q] == chess) break;
if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
}
}
if(i - 1 >= 0&& disk[i - 1][j] == other) { //正上
p = i - 1;
q = j;
while(p >= 0) {
if(disk[p][q] == other) {p--; continue;}
if(disk[p][q] == chess) break;
if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
}
}
if(i - 1 >= 0&& j + 1 < 8&& disk[i - 1][j + 1] == other) { //右上
p = i - 1;
q = j + 1;
while(p >= 0&& q < 8) {
if(disk[p][q] == other) {p--; q++; continue;}
if(disk[p][q] == chess) break;
if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
}
}
if(j + 1 < 8&& disk[i][j + 1] == other) { //右
p = i;
q = j + 1;
while(q < 8) {
if(disk[p][q] == other) {q++; continue;}
if(disk[p][q] == chess) break;
if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
}
}
if(i + 1 < 8&& j + 1 < 8&& disk[i + 1][j + 1] == other) { //右下
p = i + 1;
q = j + 1;
while(p < 8&& q < 8) {
if(disk[p][q] == other) {p++; q++; continue;}
if(disk[p][q] == chess) break;
if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
}
}
if(i + 1 < 8&& disk[i + 1][j] == other) { //下
p = i + 1;
q = j;
while(p < 8) {
if(disk[p][q] == other) {p++; continue;}
if(disk[p][q] == chess) break;
if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
}
}
if(i + 1 < 8&& j - 1 >= 0&& disk[i + 1][j - 1] == other) { //左下
p = i + 1;
q = j - 1;
while(p < 8&& q >= 0) {
if(disk[p][q] == other) {p++; q--; continue;}
if(disk[p][q] == chess) break;
if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
}
}
if(j - 1 >= 0&& disk[i][j - 1] == other) { //左
p = i;
q = j - 1;
while(q >= 0) {
if(disk[p][q] == other) {q--; continue;}
if(disk[p][q] == chess) break;
if(disk[p][q] == '-') {legal[p][q] = 1; ok = true; break;}
}
}
}
return ok;
}
void change_chess(int const x, int const y) {
int i, j, p, q;
char other = other_chess();
if(x - 1 >= 0&& y - 1 >= 0&& disk[x - 1][y - 1] == other) { //左上
i = p = x - 1;
j = q = y - 1;
while(i >= 0&& j >= 0) {
if(disk[i][j] == other) {i--; j--; continue;}
if(disk[i][j] == '-') break;
if(disk[i][j] == chess) {
while(p != i&& q != j)
disk[p--][q--] = chess;
break;
}
}
}
if(x - 1 >= 0&& disk[x - 1][y] == other) { //上
i = p = x - 1;
j = q = y;
while(i >= 0) {
if(disk[i][j] == other) {i--; continue;}
if(disk[i][j] == '-') break;
if(disk[i][j] == chess) {
while(p != i)
disk[p--][q] = chess;
break;
}
}
}
if(x - 1 >= 0&& y + 1 < 8&& disk[x - 1][y + 1] == other) { //右上
i = p = x - 1;
j = q = y + 1;
while(i >= 0&& j < 8) {
if(disk[i][j] == other) {i--; j++; continue;}
if(disk[i][j] == '-') break;
if(disk[i][j] == chess) {
while(p != i&& q != j)
disk[p--][q++] = chess;
break;
}
}
}
if(y + 1 < 8&& disk[x][y + 1] == other) { //右
i = p = x;
j = q = y + 1;
while(j < 8) {
if(disk[i][j] == other) {j++; continue;}
if(disk[i][j] == '-') break;
if(disk[i][j] == chess) {
while(q != j)
disk[p][q++] = chess;
break;
}
}
}
if(x + 1 < 8&& y + 1 < 8&& disk[x + 1][y + 1] == other) { //右下
i = p = x + 1;
j = q = y + 1;
while(i < 8&& j < 8) {
if(disk[i][j] == other) {i++; j++; continue;}
if(disk[i][j] == '-') break;
if(disk[i][j] == chess) {
while(p != i&& q != j)
disk[p++][q++] = chess;
break;
}
}
}
if(x + 1 < 8&& disk[x + 1][y] == other) { //下
i = p = x + 1;
j = q = y;
while(i < 8) {
if(disk[i][j] == other) {i++; continue;}
if(disk[i][j] == '-') break;
if(disk[i][j] == chess) {
while(p != i)
disk[p++][q] = chess;
break;
}
}
}
if(x + 1 < 8&& y - 1 >= 0&& disk[x + 1][y - 1] == other) { //左下
i = p = x + 1;
j = q = y - 1;
while(i < 8&& j >= 0) {
if(disk[i][j] == other) {i++; j--; continue;}
if(disk[i][j] == '-') break;
if(disk[i][j] == chess) {
while(p != i&& q != j)
disk[p++][q--] = chess;
break;
}
}
}
if(y - 1 >= 0&& disk[x][y - 1] == other) { //左
i = p = x;
j = q = y - 1;
while(j >= 0) {
if(disk[i][j] == other) {j--; continue;}
if(disk[i][j] == '-') break;
if(disk[i][j] == chess) {
while(q != j)
disk[p][q--] = chess;
break;
}
}
}
int B = 0, W = 0;
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
if(disk[i][j] == 'B') B++;
else if(disk[i][j] == 'W') W++;
printf("Black - %2d White - %2d\n", B, W);
}
void print_disk() {
for(int i = 0; i < 8; i++)
printf("%s\n", disk[i]);
return ;
}
int main() {
//#ifndef ONLINE_JUDGE
// freopen("my.in", "r", stdin);
//#endif // ONLINE_JUDGE
int t, T = 0;
scanf("%d", &t);
getchar();
while(t--) {
T ? printf("\n") : T++;
memset(disk, 0, sizeof(disk));
for(int i = 0; i < 8; i++)
gets(disk[i]);
scanf("%c", &chess);
getchar();
while(1) {
check();
char cmd[MAXN];
scanf("%s", cmd);
getchar();
if(cmd[0] == 'Q') break;
if(cmd[0] == 'L') {
bool ok = false;
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
if(legal[i][j]) {
if(ok) printf(" ");
printf("(%d,%d)", i + 1, j + 1);
ok = true;
}
}
}
if(!ok) printf("No legal move.");
printf("\n");
}
if(cmd[0] == 'M') {
int x = cmd[1] - '0' - 1;
int y = cmd[2] - '0' - 1;
if(!check()) chess = other_chess();
disk[x][y] = chess;
change_chess(x, y);
chess = other_chess();
}
}
print_disk();
}
return 0;
}