点击打开题目链接
一秒钟想思路,一下午调BUG
注意的地方:
1.输出格式最后不要多加空行。
2.坐标之间一个空格间隔,行尾没空格
3.棋子数的输出%2d
4.No legal move.(我忘了点,udebug样例全过了,找了半天才找出来TAT)
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int mp[10][10];
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int N;
int player, x, y, cnt[3];
int nx, ny;
char op, c;
void printMp() {
for(int i = 1; i <= 8; i++) {
for(int j = 1; j <= 8; j++) {
if(mp[i][j] == 0) cout << '-';
else if(mp[i][j] == 1) cout << 'W';
else if(mp[i][j] == 2)cout << 'B';
}
cout << endl ;
}
return;
}
int isLegal(int x, int y, int i) {
x += dx[i];
y += dy[i];
if(mp[x][y] == 0 || mp[x][y] == player) return 0;
if(x <= 0 || x >= 9 || y <= 0 || y >= 9) return 0;
while(mp[x][y] == 3-player) {
x += dx[i];
y += dy[i];
if(x <= 0 || x >= 9 || y <= 0 || y >= 9) return 0;
while(mp[x][y] == player) {
x += dx[i];
y += dy[i];
if(x <= 0 || x >= 9 || y <= 0 || y >= 9) return 1;
if(mp[x][y] == player) continue;
if(mp[x][y] == 0) return 1;
return 1;
}
}
return 0;
}
void L() {
int flag = 0;
for(int i = 1; i<= 8; i++) {
for(int j = 1; j <= 8; j++) {
if(!mp[i][j]) {
for(int k = 0; k < 8; k++) {
if(isLegal(i, j, k)) {
if(!flag) printf("(%d,%d)", i, j);
else printf(" (%d,%d)", i, j);
flag = 1;
break;
}
}
}
}
}
if(flag) cout << endl;
if(!flag) cout << "No legal move." << endl;
return;
}
void M(int x, int y) {
int flag = 0;
for(int i = 0; i < 8; i++) {
if(isLegal(x, y, i)) {
mp[x][y] = player;
nx = x + dx[i], ny = y + dy[i];
while(mp[nx][ny] == 3-player) {
mp[nx][ny] = player;
cnt[player]++;
cnt[3-player]--;
nx += dx[i];
ny += dy[i];
}
flag = 1;
}
}
if(flag == 1) {
cnt[player]++;
}
else if(flag == 0) {
player = 3 - player;
mp[x][y] = player;
cnt[player]++;
for(int i = 0; i < 8; i++) {
if(isLegal(x, y, i)) {
mp[x][y] = player;
nx = x + dx[i], ny = y + dy[i];
while(mp[nx][ny] == 3-player) {
mp[nx][ny] = player;
cnt[player]++;
cnt[3-player]--;
nx +=- dx[i];
ny += dy[i];
}
}
}
}
player = 3-player;
printf("Black - %2d White - %2d\n", cnt[2], cnt[1]);
}
int main() {
//freopen("D:\\input.txt","r",stdin);
//freopen("D:\\output.txt","w",stdout);
//ios::sync_with_stdio(false);
cin >> N;
while(N--) {
cnt[1] = cnt[2] = player = 0;
memset(mp, 0, sizeof(mp));
for(int i = 1; i <= 8; i++) {
for(int j = 1; j <= 8; j++) {
cin >> c;
if(c == '-') mp[i][j] = 0;
if(c == 'W') mp[i][j] = 1;
if(c == 'B') mp[i][j] = 2;
cnt[mp[i][j]]++;
}
}
cin >> c;
if(c == 'W') player = 1;
else if(c == 'B') player = 2;
while(cin >> op) {
if(op == 'L') L();
if(op == 'M') {
cin >> x;
y = x % 10;
x= x / 10;
M(x, y);
}
if(op == 'Q') {
printMp();
if(N >= 1) cout << endl;
break;
}
}
}
return 0;
}