[CF1450C1] Errich-Tac-Toe (Easy Version) - 构造
Description
给定一个网格,有的格子填了 X,要求不能有三个横着或者竖着的 X 或 O 相连,你可以把 X 改成 O。构造一种方案。
Solution
模仿黑白染色的思路,考虑对所有的格子按 ((i+j) mod 3) 染色,那么无论横竖,相邻的三个格子一定横跨了三种颜色。
我们只看所有某一种颜色(一定存在一种颜色的数量满足要求)的格子,如果其中是 X 就改成 O,这样构造出来的一定是合法的。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 305;
char a[N][N];
int n;
signed main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
memset(a, 0, sizeof a);
int c[3];
c[0] = c[1] = c[2] = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i] + 1;
for (int j = 1; j <= n; j++)
{
if (a[i][j] == 'X')
c[(i + j) % 3]++;
}
}
int minx = min_element(c, c + 3) - c;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if ((i + j) % 3 == minx)
{
if (a[i][j] == 'X')
a[i][j] = 'O';
}
}
}
for (int i = 1; i <= n; i++)
{
cout << a[i] + 1 << endl;
}
}
}