CF1450 C1. Errich-Tac-Toe (Easy Version) C2. Errich-Tac-Toe (Hard Version)(构造)

链接 https://codeforces.com/contest/1450/problem/C2
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.

In the easy version, the character ‘O’ does not appear in the input.

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.

题意
给你一个图,只要X . O,问你操作三分之已经下的格子数使其变为平局的场面,简单版输入没有O,就这一个区别。

思路
一开始做C1,队友说我们分块来做,先分成33,发现最省的是对角线来搞,基本思路有了,这时候另一个队友说只有对角线上有,这种情况应该不需要改变,而我们都变为O,仔细思考一手,发现我们平移对角线就行了,这时候不考虑分块了,我们考虑88全是X的,发现有三种对角线情况,我就想对角线上X最少的就是我们要搞成O的那种情况,因为这样步数少,因为搞了一组8*7都是X(有一列都是.)的样例把队友代码hack了,我们发现同样最少的,越后面越好,2 2 3 2 2 3 2 2才能不超过18次,然后还注意到输出,C1就愉快的过了。
然后想C2,很快啊,我们就想到将XO绑定在一起,根据做C1的经验,我口胡了一下,让自己相信了自己,交了一发AC了,整体还是很轻松的,之前做类似的构造题,用分块写疯了,这次想都不敢想。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 1e6 + 5;
ll qpow(ll a, ll b){
	ll ans = 1, base = a;
	while(b){
		if(b&1) ans=ans*base%mod;
		base=base*base%mod;
		b>>=1;
	}	
	return ans;
}
int Case,n;
char mp[305][305];
int cnt[4];
void solve(){
	scanf("%d",&n);
	cnt[0]=cnt[1]=cnt[2]=0;
	for(int i=1;i<=n;i++){
		getchar();
		for(int j=1;j<=n;j++){
			scanf("%c",&mp[i][j]);
			if(mp[i][j]=='X'){
				cnt[(i+j)%3]++;
			}
			if(mp[i][j]=='O'){
				cnt[(i+j-1)%3]++;
			}
		}
	}
	int vis=0;
	for(int i=0;i<3;i++)
		if(cnt[i]<=cnt[vis]) vis = i;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(mp[i][j]=='X'&&(i+j)%3==vis) mp[i][j]='O';
			if(mp[i][j]=='O'&&(i+j-1)%3==vis) mp[i][j]='X';
			printf("%c",mp[i][j]);
		}
		puts("");
	}
}
int main(){
	Case=1;
	scanf("%d",&Case);
	while(Case--){
		solve();
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值