Errich-Tac-Toe (Hard Version)
题目描述
The only difference between the easy and hard versions is that tokens of type O do not appear in the input of the easy version.
Errichto gave Monogon the following challenge in order to intimidate him from taking his top contributor spot on Codeforces.
In a Tic-Tac-Toe grid, there are n n n rows and n n n columns. Each cell of the grid is either empty or contains a token. There are two types of tokens: X and O. If there exist three tokens of the same type consecutive in a row or column, it is a winning configuration. Otherwise, it is a draw configuration.
The patterns in the first row are winning configurations. The patterns in the second row are draw configurations.
In an operation, you can change an X to an O, or an O to an X. Let k k k denote the total number of tokens in the grid. Your task is to make the grid a draw in at most ⌊ k 3 ⌋ \lfloor \frac{k}{3}\rfloor ⌊3k⌋ (rounding down) operations.
You are not required to minimize the number of operations.
输入描述
The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1\le t\le 100 1≤t≤100) — the number of test cases.
The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 300 1\le n\le 300 1≤n≤300) — the size of the grid.
The following n n n lines each contain a string of n n n characters, denoting the initial grid. The character in the i i i-th row and j j j-th column is ‘.’ if the cell is empty, or it is the type of token in the cell: ‘X’ or ‘O’.
It is guaranteed that not all cells are empty.
The sum of n n n across all test cases does not exceed 300 300 300.
输出描述
For each test case, print the state of the grid after applying the operations.
We have proof that a solution always exists. If there are multiple solutions, print any.
样例输入 #1
3
3
.O.
OOO
.O.
6
XXXOOO
XXXOOO
XX..OO
OO..XX
OOOXXX
OOOXXX
5
.OOO.
OXXXO
OXXXO
OXXXO
.OOO.
样例输出 #1
.O.
OXO
.O.
OXXOOX
XOXOXO
XX..OO
OO..XX
OXOXOX
XOOXXO
.OXO.
OOXXO
XXOXX
OXXOO
.OXO.
提示
In the first test case, there are initially three ‘O’ consecutive in the second row and the second column. By changing the middle token to ‘X’ we make the grid a draw, and we only changed 1 ≤ ⌊ 5 / 3 ⌋ 1\le \lfloor 5/3\rfloor 1≤⌊5/3⌋ token.
In the second test case, the final grid is a draw. We only changed 8 ≤ ⌊ 32 / 3 ⌋ 8\le \lfloor 32/3\rfloor 8≤⌊32/3⌋ tokens.
In the third test case, the final grid is a draw. We only changed 7 ≤ ⌊ 21 / 3 ⌋ 7\le \lfloor 21/3\rfloor 7≤⌊21/3⌋ tokens.
原题
思路
形成胜局的条件有两个:
- 水平上三个连续的 ‘O’ 或者 ‘X’
- 竖直上三个连续的 ‘O’ 或者 ‘X’
而二者有一个共同性质,即由三个 x , y x,y x,y 坐标值之和连续递减 1 1 1 的点组成。所以,我们考虑将所有的点分成三类,即 ( x + y ) % 3 = = 0 , 1 , 2 (x+y)\%3==0,1,2 (x+y)%3==0,1,2 的三类点,将它们分别命名为第 0 类点,第 1 类点和第 2 类点。那么,可以找到其中一定能形成平局的三种构造:
- 第 0 类点上的 ‘X’ 全部改为 ‘O’,第 1 类点上的 ‘O’ 全部改为 ‘X’
- 第 1 类点上的 ‘X’ 全部改为 ‘O’,第 2 类点上的 ‘O’ 全部改为 ‘X’
- 第 2 类点上的 ‘X’ 全部改为 ‘O’,第 0 类点上的 ‘O’ 全部改为 ‘X’
可以证明,这样的构造一定会使得任意水平上三个连续的点和任意竖直上三个连续的点都不会是全部为 ‘X’ 或全部为 ‘O’。
现在的问题是这样的构造是否能够满足操作次数至多为
⌊
k
3
⌋
\lfloor \frac{k}{3}\rfloor
⌊3k⌋。我们发现,上述的三种构造,操作次数之和一定为 k。那么根据鸽巢原理,一定存在一种构造,它的操作次数至多为
⌊
k
3
⌋
\lfloor \frac{k}{3}\rfloor
⌊3k⌋。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
int n;
cin >> n;
vector<vector<char>> v(n, vector<char>(n));
int k = 0; //'O'或'X'标记的总个数
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> v[i][j];
if (v[i][j] != '.')
k++;
}
}
vector<vector<char>> ans; // 当找到满足条件的(a,b)对时,存储下替换后的答案
int a, b; //(a,b)对用来表示(0,1),(1,2),(2,0)对
auto cfg = [&](vector<vector<char>> g) -> bool
{
int cnt = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (g[i][j] == 'X' && (i + j) % 3 == a)
{
g[i][j] = 'O';
cnt++;
}
if (g[i][j] == 'O' && (i + j) % 3 == b)
{
g[i][j] = 'X';
cnt++;
}
}
}
if (cnt <= k / 3) // 操作次数小于等于k/3,满足条件
{
ans = g;
return 1;
}
else
return 0;
};
for (a = 0; a < 3; a++)
{
b = (a + 1) % 3;
if (cfg(v))
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << ans[i][j];
}
cout << '\n';
}
return;
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
标签:12,int,题解,类点,Hard,++,le,grid,test
From: https://blog.csdn.net/bbc_plus/article/details/139166858