CF1450C1 Errich-Tac-Toe (Easy Version)/CF1450C2 Errich-Tac-Toe (Hard Version)
Description
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 $ \lfloor \frac{k}{3}\rfloor $ (rounding down) operations.
You are not required to minimize the number of operations.
Solution
考虑横纵坐标之和模三同余的所有点构成一组直线,每个三个连续的方块必包含一个直线组中的点
所以扫全图,找到某个/两个直线组上的标记之和小于$\frac k3$,将这个/这些直线组的所有方块改变标记种类即可
一定能找到至少一个/一对直线组
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n,dp[3][2],k,tag1,tag2; char map[305][305]; inline int read() { int f=1,w=0; char ch=0; while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') w=(w<<1)+(w<<3)+ch-'0',ch=getchar(); return f*w; } int main() { for(int T=read();T;T--) { memset(dp,0,sizeof(dp)); n=read(),k=tag1=tag2=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { char ch=getchar(); while(ch!='X'&&ch!='O'&&ch!='.') ch=getchar(); if(ch!='.') ++k; map[i][j]=ch; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(map[i][j]!='.') dp[(i+j)%3][map[i][j]=='X']++; for(int i=0;i<=2;i++) for(int j=0;j<=2;j++) if(i!=j&&dp[i][0]+dp[j][1]<=k/3) tag1=i,tag2=j; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if((i+j)%3==tag1&&map[i][j]=='O') map[i][j]='X'; if((i+j)%3==tag2&&map[i][j]=='X') map[i][j]='O'; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) putchar(map[i][j]); putchar(10); } } return 0; }