每次做这种题都是些一堆代码,然后调试提交,最后报WA。和网上的解法一对照就会发现自己的代码过于冗长。暂时没有耐心进行修改了。就记录一下吧。
题目链接:UVa 220
WA代码:
#include <iostream>
#include <cstring>
char board[10][10];
int is_legal[10][10];
int is_change[10][10];
char ch, com[10];
bool flag = 1;
using namespace std;
void judge(int x, int y, char c1, char c2) { //判断合法操作位置并存储到is_legal数组中
for (int i = 1; i <= 8; i++) {
bool s = 1; int a = x, b = y;
if (i == 1 && x - 1 >= 1 && y - 1 >= 1 && x + 1 <= 8 && y + 1 <= 8) {
if (board[x - 1][y - 1] == '-') {
while (board[a + 1][b + 1] == c1) {
a++; b++;
if (a == 8 || b == 8) {
s = 0;
break;
}
}
if (s && board[a + 1][b + 1] == c2)
is_legal[x - 1][y - 1] = 1;
}
}
else if (i == 2 && x - 1 >= 1 && x + 1 <= 8) {
if (board[x - 1][y] == '-') {
while (board[a + 1][b] == c1) {
a++;
if (a == 8) {
s = 0;
break;
}
}
if (s && board[a + 1][b] == c2)
is_legal[x - 1][y] = 1;
}
}
else if (i == 3 && x - 1 >= 1 && y + 1 <= 8 && x + 1 <= 8 && y - 1 >= 1) {
if (board[x - 1][y + 1] == '-') {
while (board[a + 1][b - 1] == c1) {
a++; b--;
if (a == 8 || b == 1) {
s = 0;
break;
}
}
if (s && board[a + 1][b - 1] == c2)
is_legal[x - 1][y + 1] = 1;
}
}
else if (i == 4 && y + 1 <= 8 && y - 1 >= 1) {
if (board[x][y + 1] == '-') {
while (board[a][b - 1] == c1) {
b--;
if (b == 1) {
s = 0;
break;
}
}
if (s && board[a][b - 1] == c2)
is_legal[x][y + 1] = 1;
}
}
else if (i == 5 && x + 1 <= 8 && y + 1 <= 8 && x - 1 >= 1 && y - 1 >= 1) {
if (board[x + 1][y + 1] == '-') {
while (board[a - 1][b - 1] == c1) {
a--; b--;
if (a == 1 || b == 1) {
s = 0;
break;
}
}
if (s && board[a - 1][b - 1] == c2)
is_legal[x + 1][y + 1] = 1;
}
}
else if (i == 6 && x + 1 <= 8 && x - 1 >= 1) {
if (board[x + 1][y] == '-') {
while (board[a - 1][b] == c1) {
a--;
if (a == 1) {
s = 0;
break;
}
}
if (s && board[a - 1][b] == c2)
is_legal[x + 1][y] = 1;
}
}
else if (i == 7 && x + 1 <= 8 && x - 1 >= 1 && y + 1 <= 8 && y - 1 >= 1) {
if (board[x + 1][y - 1] == '-') {
while (board[a - 1][b + 1] == c1) {
a--; b++;
if (a == 1 || b == 8) {
s = 0;
break;
}
}
if (s && board[a - 1][b + 1] == c2)
is_legal[x + 1][y - 1] = 1;
}
}
else if (i == 8 && y + 1 <= 8 && y - 1 >= 1) {
if (board[x][y - 1] == '-') {
while (board[a][b + 1] == c1) {
b++;
if (b == 8) {
s = 0;
break;
}
}
if (s && board[a][b + 1] == c2)
is_legal[x][y - 1] = 1;
}
}
}
}
void print_board() { //打印棋盘
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++)
cout << board[i][j];
cout << endl;
}
}
void print_oper() { //答应合法操作位置
int cnt = 0;
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++) {
if (is_legal[i][j] == 1) {
if (cnt == 0)
cnt++;
else
cout << " ";
cout << "(" << i << "," << j << ")";
}
}
if (!cnt) {
flag = 0;
cout << "No legal move.";
}
memset(is_legal, 0, sizeof(is_legal));
cout << endl;
}
void doChange() {
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++) {
if (is_change[i][j] == 1) {
if (board[i][j] == 'W')
board[i][j] = 'B';
else
board[i][j] = 'W';
}
}
memset(is_change, 0, sizeof(is_change));
}
void change(int x, int y, char c1, char c2) {
for (int i = 1; i <= 8; i++) {
bool s = 1; int a = x, b = y;
if (i == 1 && x + 1 <= 8 && y + 1 <= 8) {
while (board[a + 1][b + 1] == c2) {
a++; b++;
is_change[a][b] = 1;
if (a == 8 || b == 8) {
s = 0;
break;
}
}
if (s && board[a + 1][b + 1] == c1)
doChange();
}
else if (i == 2 && x + 1 <= 8) {
while (board[a + 1][b] == c2) {
a++;
is_change[a][b] = 1;
if (a == 8) {
s = 0;
break;
}
}
if (s && board[a + 1][b] == c1)
doChange();
}
else if (i == 3 && x + 1 <= 8 && y - 1 >= 1) {
while (board[a + 1][b - 1] == c2) {
a++; b--;
is_change[a][b] = 1;
if (a == 8 || b == 1) {
s = 0;
break;
}
}
if (s && board[a + 1][b - 1] == c1)
doChange();
}
else if (i == 4 && y - 1 >= 1) {
while (board[a][b - 1] == c2) {
b--;
is_change[a][b] = 1;
if (b == 1) {
s = 0;
break;
}
}
if (s && board[a][b - 1] == c1)
doChange();
}
else if (i == 5 && x - 1 >= 1 && y - 1 >= 1) {
while (board[a - 1][b - 1] == c2) {
a--; b--;
is_change[a][b] = 1;
if (a == 1 || b == 1) {
s = 0;
break;
}
}
if (s && board[a - 1][b - 1] == c1)
doChange();
}
else if (i == 6 && x - 1 >= 1) {
while (board[a - 1][b] == c2) {
a--;
is_change[a][b] = 1;
if (a == 1) {
s = 0;
break;
}
}
if (s && board[a - 1][b] == c1)
doChange();
}
else if (i == 7 && x + 1 <= 8 && x - 1 >= 1 && y + 1 <= 8 && y - 1 >= 1) {
while (board[a - 1][b + 1] == c2) {
a--; b++;
is_change[a][b] = 1;
if (a == 1 || b == 8) {
s = 0;
break;
}
}
if (s && board[a - 1][b + 1] == c1)
doChange();
}
else if (i == 8 && y - 1 >= 1) {
while (board[a][b + 1] == c2) {
b++;
is_change[a][b] = 1;
if (b == 8) {
s = 0;
break;
}
}
if (s && board[a][b + 1] == c1)
doChange();
}
}
}
void command(char cha, char opre) {
if (opre == 'L') {
if (ch == 'W') {
for (int i = 1; i <= 7; i++)
for (int j = 1; j <= 7; j++) {
if (board[i][j] == 'B') {
judge(i, j, 'B', 'W');
}
}
}
else {
for (int i = 1; i <= 7; i++)
for (int j = 1; j <= 7; j++) {
if (board[i][j] == 'W') {
judge(i, j, 'W', 'B');
}
}
}
print_oper();
}
else if (opre == 'M') {
if (!flag) {
if (cha == 'B')
ch = 'W';
else
ch = 'B';
}
flag = 1;
board[com[1]-'0'][com[2]-'0'] = ch;
if (ch == 'B')
change(com[1]-'0', com[2]-'0', 'B', 'W');
else
change(com[1]-'0', com[2]-'0', 'W', 'B');
int a = 0, b = 0;
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++) {
if (board[i][j] == 'W')
a++;
else if (board[i][j] == 'B')
b++;
}
cout << "Black - " << b << " White - " << a << endl;
}
else
cout << "Player information error" << endl;
}
int main() {
int T, cnt = 0;
cin >> T;
while (T--) {
memset(is_legal, 0, sizeof(is_legal));
memset(is_change, 0, sizeof(is_change));
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++) {
cin >> board[i][j];
}
cin >> ch;
if (cnt++)
cout << endl;
while (cin >> com) {
if (com[0] != 'Q') {
command(ch, com[0]);
if (com[0] == 'M') {
if (ch == 'B')
ch = 'W';
else
ch = 'B';
}
}
else {
print_board();
break;
}
}
}
return 0;
}