题目大意
n×m个由’.’ 和 ‘-’ 组成的格子,要求在’.'上放棋子,并且相邻的格子没有相同的棋子。
解题
先看下面的图片,我们可以得出一个格子相邻的地方没有相同的字符,可以得出结论,只要用一个字符来标记自己当前的字符,并将周围的都变成和自己不同的就可以了,那么这道题就可以做出来了。找到 ’ . ’ 的格子,并将四周的 ’ . ’ 变成不同的字符就好了。
代码
#include <bits/stdc++.h>
using namespace std;
#define For(i, k, n) for (int i = k; i < n; i ++)
#define IOS ios::sync_with_stdio(false)
typedef long long LL;
#define P acos(-1);
const int N = 1e5 + 10, M = 1e6 + 10;
int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1};
int n, m;
char q[111][111];
void dfs(int x, int y, bool f)
{
if (f == 1)
q[x][y] = 'B';
else
q[x][y] = 'W';
For (i, 0, 4)
{
int xx = dx[i] + x, yy = dy[i] + y;
if (xx >= 0 && xx < n && yy >= 0 && yy < m && q[xx][yy] == '.')
{
if (f == 1)
dfs(xx, yy, 0);
else
dfs(xx, yy, 1);
}
}
}
int main()
{
IOS;
cin >> n >> m;
For (i, 0, n)
{
For (j, 0, m)
cin >> q[i][j];
}
For (i, 0, n)
{
For (j, 0, m)
{
if (q[i][j] == '.')
dfs(i, j, 1);
}
}
For (i, 0, n)
{
For (j, 0, m)
cout << q[i][j];
cout << '\n';
}
return 0;
}