这道题也是个模拟,没什么特别的想法,提交时一定要小心空格、\n一定不要多,否则会WA
这就是我冗长的代码:
//UVa 220 //Othello #define LOCAL #include <stdio.h> #include <string.h> //use memset strcmp char chess[10][10], cnt; void Printf(int T) { for(int i = 1; i <= 8; i++) { for(int j = 0; j < 8; j++) printf("%c", chess[i][j]); printf("\n"); } if(T != 0) printf("\n"); } int check(int x, int y, int choose) { char target; if(cnt == 'W') target = 'B'; else target = 'W'; if(x > 2 && chess[x-1][y] == target) for(int i = x-2; i > 0; i--) { if(chess[i][y] == '-') break; if(chess[i][y] == cnt) if(choose) { for(int k = i+1; k <= x-1; k++) chess[k][y] = cnt; break; } else return 1; } if(x < 7 && chess[x+1][y] == target) for(int i = x+2; i < 9; i++) { if(chess[i][y] == '-') break; if(chess[i][y] == cnt) if(choose) { for(int k = x+1; k < i; k++) chess[k][y] = cnt; break; } else return 1; } if(y > 1 && chess[x][y-1] == target) for(int i = y-2; i >= 0; i--) { if(chess[x][i] == '-') break; if(chess[x][i] == cnt) if(choose) { for(int k = i+1; k <= y-1; k++) chess[x][k] = cnt; break; } else return 1; } if(y < 6 && chess[x][y+1] == target) for(int i = y+2; i < 8; i++) { if(chess[x][i] == '-') break; if(chess[x][i] == cnt) if(choose) { for(int k = y+1; k < i; k++) chess[x][k] = cnt; break; } else return 1; } int i = x; int j = y; if(chess[x-1][y-1] == target) while(--i && --j >= 0) { if(chess[i][j] == '-') break; if(chess[i][j] == cnt) if(choose) { i = x; j = y; while(--i && --j >= 0) { if(chess[i][j] == cnt) break; chess[i][j] = cnt; } break; } else return 1; } i = x; j = y; if(chess[x+1][y+1] == target) while(++i < 9 && ++j < 8) { if(chess[i][j] == '-') break; if(chess[i][j] == cnt) if(choose) { i = x; j = y; while(++i < 9 && ++j < 8) { if(chess[i][j] == cnt) break; chess[i][j] = cnt; } break; } else return 1; } i = x; j = y; if(chess[x-1][y+1] == target) while(--i && ++j < 8) { if(chess[i][j] == '-') break; if(chess[i][j] == cnt) if(choose) { i = x; j = y; while(--i && ++j < 8) { if(chess[i][j] == cnt) break; chess[i][j] = cnt; } break; } else return 1; } i = x; j = y; if(chess[x+1][y-1] == target) while(++i < 9 && --j >= 0) { if(chess[i][j] == '-') break; if(chess[i][j] == cnt) if(choose) { i = x; j = y; while(++i < 9 && --j >= 0) { if(chess[i][j] == cnt) break; chess[i][j] = cnt; } break; } else return 1; } return 0; } void Legal_Move() { int T = 0; for(int i = 1; i <= 8; i++) for(int j = 0; j < 8; j++) if(chess[i][j] == '-' && check(i, j, 0)) { if(!T) printf("(%d,%d)", i, j+1); else printf(" (%d,%d)", i, j+1); T = 1; } if(!T) printf("No legal move."); printf("\n"); } void Move_To(int x, int y) { if(!check(x, y, 0)) { if(cnt == 'W') cnt = 'B'; else cnt = 'W'; } int a = check(x, y, 1); //no use in a int numW = 0; int numB = 0; chess[x][y] = cnt; for(int i = 1; i <= 8; i++) for(int j = 0; j < 8; j++) { if(chess[i][j] == 'W') numW++; if(chess[i][j] == 'B') numB++; } // Printf(); printf("Black - %2d White - %2d\n", numB, numW); if(cnt == 'W') cnt = 'B'; else cnt = 'W'; } int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int N; scanf("%d", &N); while(N--) { for(int i = 1; i <= 8; i++) scanf("%s", chess[i]); char a[5]; scanf("%s", a); cnt = a[0]; while(scanf("%s", a) && strcmp(a, "Q") != 0) { if(!strcmp(a, "L")) Legal_Move(); else Move_To(a[1]-'0', a[2]-'0'-1); } Printf(N); } return 0; }