知识点:模拟
花了32分钟过了,不过是我下午写了1个小时后吃顿饭回来重新写的,要是我真的有这么厉害就好了,
这个题很像我刚做过的那道蓝题,就是在二维平面某行某列,换句话说是,某个方向上找符合条件的连续的元素,应该是可以用递归做,不过我还没怎么学递归,就先用循环了,前面那道题领我入门,这道题做完算是对这一类模拟很熟练了,其实也很简单,一个while循环就行了,循环结束,保存的就是符合条件的点的后面一个(或者说外面一个)的坐标,可能是出界的坐标,可能是界内不符合条件的坐标,这时我们就可以拿它作为边界来进行处理了,
当我写完判断函数的时候,再去看第二个操作,发现第一个操作的函数稍微更改一下就能两个操作公用,然后就设置了一个全局变量,函数修改一下,多了一个参数,因为第二个操作是要修改点的,我发现我的判断函数已经把修改的工作做完了一大半了,再补上去一小段就可以,第二个操作不要忘记把下棋的那个点修改了,
最后要注意第二个操作,操作完之后还要换一下下棋的人,
#include <bits/stdc++.h>
using namespace std;
string s[10];
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
bool flag;
void judge(int x, int y, char ch, int change) {
flag = false;
if (s[x][y] != '-') return;
for (int i = 0; i < 8; i++) {
int x1 = x + dx[i];
int y1 = y + dy[i];
if (x1 < 1 || x1 > 8 || y1 < 1 || y1 > 8) continue;
if (s[x1][y1] == '-' || s[x1][y1] == ch) continue;
int x2 = x1, y2 = y1;
while (x2 >= 1 && x2 <= 8 && y2 >= 1 && y2 <= 8 && s[x2][y2] == s[x1][y1]) {
x2 += dx[i]; y2 += dy[i];
}
if (x2 >= 1 && x2 <= 8 && y2 >= 1 && y2 <= 8 && s[x2][y2] == ch) {
flag = true;
if (change) {
while (!(x1 == x2 && y1 == y2)) {
s[x1][y1] = ch;
x1 += dx[i];
y1 += dy[i];
}
}
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
for (int i = 1; i <= 8; i++) {
cin >> s[i]; s[i] = " " + s[i];
}
char ch;
cin >> ch;
string order;
while (cin >> order && order != "Q") {
if (order == "L") {
int rec = 0;
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
judge(i, j, ch, 0);
if (flag) {
if (rec++) cout << ' ';
printf("(%d,%d)", i, j);
}
}
}
if (!rec) cout << "No legal move.";
cout << endl;
} else {
int x = order[1] - '0';
int y = order[2] - '0';
judge(x, y, ch, 1);
if (!flag) {
ch = (ch == 'W' ? 'B' : 'W');
judge(x, y, ch, 1);
}
s[x][y] = ch;
ch = (ch == 'W' ? 'B' : 'W');
int n1 = 0, n2 = 0;
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
if (s[i][j] == 'B') n1++;
if (s[i][j] == 'W') n2++;
}
}
printf("Black -%3d White -%3d\n", n1, n2);
}
}
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
cout << s[i][j];
}
cout << endl;
}
if (t) cout << endl;
}
return 0;
}