算法竞赛入门经典第四章习题4-3 Othello UVA - 220
2018-01-05 00:10
507 查看
习题4-3
https://vjudge.net/problem/UVA-220
https://vjudge.net/problem/UVA-220
#include<iostream> #include<string> #include<cstring> #pragma warning(disable:4996) using namespace std; string s[9]; int turn; int Qi_n[2]; char Qi[2] = {'B','W'}; bool is_true(int r,int c,int flag,int x,int y) {//(x,y)是方向向量 flag=0时查询,1时实际操作 int k = 1; if (r + k * x > 7 || r + k * x < 0 || c + k * y>7 || c + k * y <0 || s[r + x][c + y] != Qi[1 - turn]) return false; for (k = 2; k < 8; ++k) { if (r + k*x > 7 || r + k*x < 0 || c + k*y>7 || c + k*y <0 || s[r + k*x][c + k*y] == '-') return false; if (s[r + k * x][c + k * y] == Qi[turn]) break; } if (flag) { for (int k = 1; k < 8; ++k) { if (s[r + k * x][c + k * y] == Qi[turn]) break; s[r + k * x][c + k * y] = Qi[turn]; ++Qi_n[turn]; --Qi_n[1 - turn]; } } return true; } //判断是否可以落子到(r,c) bool del_IQ(int r, int c, int flag) { bool is_ok = false; for (int x = -1; x < 2; ++x) for (int y = -1; y < 2; ++y) if (x || y) if (is_true(r, c, flag, x, y)) is_ok = true; if (flag && is_ok) { s[r][c] = Qi[turn]; ++Qi_n[turn]; } return is_ok; } int main() { #ifdef _DEBUG freopen("in", "rb", stdin); freopen("out", "wb", stdout); #endif // _DEBUG int N; cin >> N; for (int i = 0; i < N;++i) { if (i) cout << endl; for (int i = 0; i < 8; ++i) cin >> s[i]; Qi_n[0] = Qi_n[1] = 0; for (int i = 0; i < 8; ++i) for (int j = 0; j < 8; ++j) if (s[i][j] == 'B') ++Qi_n[0]; else if (s[i][j] == 'W') ++Qi_n[1]; string str; cin >> str; if (str[0] == 'B') turn = 0; else turn = 1; while (cin >> str) { if (str == "Q") { for (int i = 0; i < 8; ++i) cout<< s[i]<<endl; break; } if (str == "L") { int cnt = 0; for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) if (s[i][j] == '-' && del_IQ(i, j, 0)) if (cnt++) printf(" (%d,%d)", i+1, j+1); else printf("(%d,%d)", i+1, j+1); } if (!cnt) printf("No legal move."); printf("\n"); } else {//str==Mrc int r=str[1]-'0'-1, c=str[2]-'0'-1; if(!del_IQ(r, c, 0)) turn = 1 - turn; del_IQ(r, c, 1); turn = 1 - turn; printf("Black - %2d White - %2d\n", Qi_n[0], Qi_n[1]); } } } }
相关文章推荐
- UVa220 算法竞赛入门经典(第2版)习题4-3 黑白棋 Othello
- 算法竞赛入门经典(第2版)习题4-3 黑白棋 Othello UVa220
- 算法竞赛入门经典第四章习题4-5 IP Networks UVA - 1590
- [刷题]算法竞赛入门经典(第2版) 4-3/UVa220 - Othello
- 算法竞赛入门经典第四章习题4-10 Flooded! UVA - 815
- 算法竞赛入门经典第四章习题4-6 Morse Mismatches UVA - 508
- 算法竞赛入门经典(刘汝佳)习题3-4 UVa455
- 算法竞赛入门经典第四章:部分习题解答
- 算法竞赛入门经典(刘汝佳)习题3-5 UVa227
- 习题4-3 黑白棋 UVa220
- UVA - 220 Othello
- BFS:UVa220 ACM/ICPC 1992-Othello(黑白棋)
- 算法竞赛入门经典(紫书)第四章——Ancient Cipher UVA-1339
- 算法竞赛入门经典(刘汝佳)习题3-6 UVa232
- Uva220 Othello
- 算法竞赛入门经典第四章例题4-1 Ancient Cipher UVA - 1339
- 算法竞赛入门经典第四章习题4-8 Extraordinarily Tired Students UVA - 12108
- 算法竞赛入门经典(刘汝佳)习题3-8 UVa202
- 算法竞赛入门经典(第2版)习题5-7 打印队列 UVa12100
- UVa 220 - Othello