题目描述

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不能等于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;
}

Codeforces Global Round 12 C2. Errich-Tac-Toe (Hard Version)(思维)相关推荐

  1. Codeforces Global Round 12 C1 C2. Errich-Tac-Toe 思维构造 好题

    传送门 题意: 给了如下规则,上面三个只要出现一个情况就是非平局,现在给你个字符矩阵,让后其中XXX字符有KKK个(hardhardhard版本XXX和OOO一共KKK个),每次操作可以将XXX变成O ...

  2. Codeforces Global Round 12 E. Capitalism 差分约束

    传送门 题意: 思路: 一开始被题意迷惑了,没看出来差分约束,老菜鸡啦.首先看到aj=ai+1a_j=a_i+1aj​=ai​+1可以把aia_iai​分成奇偶,让后这个图就变成一个二分图了.再考虑如 ...

  3. Codeforces Global Round 12 D. Rating Compression 思维 + 贪心

    传送门 题意: 给一个长度为nnn的数组aaa,定义一个数组bbb,且bj=minj<=i<=j+k−1aib_j=min_{j<=i<=j+k-1}a_ibj​=minj&l ...

  4. Codeforces Global Round 1 晕阙记

    Codeforces Global Round 1 晕阙记 我做这场比赛的时候晕得要死.做这三道题做太久了,rating涨不起来啊! A 如果愿意的话你可以看做是膜2意义下的运算,写快速幂等各种膜运算 ...

  5. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  6. Codeforces Global Round 14 F. Phoenix and Earthquake 思维 + 并查集

    传送门 文章目录 题意: 思路: 题意: 给你nnn个点,mmm条边,限制xxx,每个点都有沥青aia_iai​,定义合并两个点即两点之间有边且au+av≥xa_u+a_v\ge xau​+av​≥x ...

  7. Codeforces Global Round 1

    Codeforces Global Round 1 题解:The Editorial of the First Codeforces Global Round A:其实mod 2计算一下就行了 B:删 ...

  8. 【Codeforces Global Round 23】B. Rebellion

    Codeforces Global Round 23中B. Rebellion Codeforces比赛记录 文章目录 题目链接: 一.B. Rebellion 题目意思: 上思路: 总结 B. Re ...

  9. Codeforces Global Round 4-D. Prime Graph(伯特兰-切比雪夫定理)

    题目:Codeforces Global Round 4-D. Prime Graph 题意:给出n(顶点的个数),要求所得图满足: 1.无平行边和自环 2.边的总数是个质数 3.每个点的度(也就是点 ...

最新文章

  1. 用fieldset标签轻松实现Tab选项卡效果
  2. Eclipse新建web项目和“javax.servlet.http.HttpServlet“ was not found on the Java Build Path错误
  3. idea数据库反向生成实体类_IntelliJ IDEA 的数据库管理工具实在太方便了
  4. 搜索表单制作语法:强大的搜索功能
  5. anaconda降级python_anaconda python更换清华源
  6. 种草!这只鹅虽然没有什么用,但是好可爱呀!
  7. 用python画多来a梦-python 绘制哆啦A梦
  8. [谈现在的PSP与NDSi]
  9. (转)用Scintilla让程序支持语法高亮
  10. 置换矩阵、转置矩阵以及向量空间、子空间
  11. X1000对于音频播放控制部分的翻译
  12. Crashing Balloon
  13. THRESH_OTSU mode: > ‘src_type == CV_8UC1 || src_type == CV_16UC1‘ > where > ‘src_type‘ is 6
  14. matlab 矩阵元素求和、求均值(期望)和均方差
  15. 彻底解剖人民币升值问题 文/岑科
  16. 抹掉数据时显示连接服务器失败,无法连接iCloud,连接icloud验证失败,icloud连接不到:《苹果抹掉数据验证失败》 苹果icloud怎么登陆不上?-南开游戏网...
  17. 光纤收发器常见指示灯的意义
  18. 开博尔智能android播放器C3,开博尔C3第9代不能进桌面的刷机方法
  19. 关键词提取-论文研读-betweenness centrality相关算法(2)
  20. bistuacm 2019年第⑦场新生训练赛题解

热门文章

  1. android 通知多行,Android多行通知,例如Gmail应用
  2. 框架-tp一般建站步骤
  3. 【原创】【FS】FATFS文件系统介绍(未完待续........2018.4.1)
  4. 学习笔记 | 多层感知机(MLP)、Transformer
  5. msf注入payload
  6. Android 旧项目打包 api-versions.xml Stream closed
  7. springboot打包时跳过测试代码
  8. 科东软件与工信部电子五所达成战略合作,共同推动国产工业基础软件高质量发展
  9. 虚拟局域网 VLAN
  10. Log4Qt的基本使用