题目描述
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 rows and 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 denote the total number of tokens in the grid. Your task is to make the grid a draw in at most ?k3? (rounding down) operations.
You are not required to minimize the number of operations.
Input
The first line contains a single integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤300) — the size of the grid.
The following n lines each contain a string of n characters, denoting the initial grid. The character in the i-th row and 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 across all test cases does not exceed 300.
Output
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.
Example
input
3
3
.O.
OOO
.O.
6
XXXOOO
XXXOOO
XX…OO
OO…XX
OOOXXX
OOOXXX
5
.OOO.
OXXXO
OXXXO
OXXXO
.OOO.
output
.O.
OXO
.O.
OXXOOX
XOXOXO
XX…OO
OO…XX
OXOXOX
XOOXXO
.OXO.
OOXXO
XXOXX
OXXOO
.OXO.
Note
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? token.
In the second test case, the final grid is a draw. We only changed 8≤?32/3? tokens.
In the third test case, the final grid is a draw. We only changed 7≤?21/3? tokens.
题目大意
给出一个n*n的图像其中X和O一共有k个。我们可以将任意一个X修改为O或者O修改为X。问最多修改k/3,使得图中没有三个O(X)在同一行(列)。
题目分析
我们可以发现:通过(当前行数+当前列数)%3,可以将图分成三份,并且每三个连续的位置上都是这三个部分的组合。
我们可以记录下这三个部分中X和O的个数,a[0]、a[1]、a[2](x)和b[0]、b[1]、b[2](o)。并找出a[i]+b[j]的最小值,因为k=a[0]+a[1]+a[2]+b[0]+b[1]+b[2],因此min(a[i]+b[j])一定是小于等于k/3的。
然后将 i 部分的X全部改为O,而 j 部分的O全部改为X。这样三部分中的两个部分中的元素就都确定了。因为每三个连续的位置上都是这三个部分的组合,而这三个部分中保证有一部分全是X,一份部分全是O,这样就一定不会有三个O(X)在同一行(列)。
代码如下 #include <iostream>#include <cstdio>#include 美国高防vps <cmath>#include <string>#include <cstring>#include <map>#include <unordered_map>#include <queue>#include <vector>#include <set>#include <bitset>#include <algorithm>#define LL long long#define PII pair<int,int>#define x first#define y secondusing namespace std;const int N=3e2+5,mod=1e9+7;char s[N][N];int main(){int t;scanf(“%d”,&t);while(t–){int n;scanf(“%d”,&n);for(int i=0;i<n;i++) scanf(“%s”,s[i]);int a[3]={0},b[3]={0};//a[]记录每部分X的个数,b[]记录每部分O的个数for(int i=0;i<n;i++)for(int j=0;j<n;j++)if(s[i][j]==’X’) a[(i+j)%3]++;else if(s[i][j]==’O’) b[(i+j)%3]++;int ak=0,bk=1;//找出min(a[i]+a[j])for(int i=0;i<3;i++)for(int j=0;j<3;j++)//因为我们要在三部分中确定两部分,因此i不能等于jif(i!=j&&a[ak]+b[bk]>a[i]+b[j]) ak=i,bk=j;for(int i=0;i<n;i++)for(int j=0;j<n;j++)//将算出的两部分进行修改{if(s[i][j]==’X’&&(i+j)%3==ak) s[i][j]=’O’;else if(s[i][j]==’O’&&(i+j)%3==bk) s[i][j]=’X’;}for(int i=0;i<n;i++) printf(“%s\n”,s[i]);//输出答案} return 0; } 09336012