Codeforces Global Round 12 C2. 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 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 <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 second
using 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不能等于j
				if(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; 
}
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值