C1. Errich-Tac-Toe (Easy Version)

该博客介绍了如何通过改变最少数量的X标记为O,以确保Tic-Tac-Toe棋盘没有连续的三个相同标记行或列,从而达到平局状态。提供的C++代码实现了一个解决方案,该方案适用于包含O和X标记的棋盘,且在易于理解的版本中,输入不包含O标记。示例展示了不同测试用例的处理过程,证明了总能找到至少一种使棋盘变为平局状态的方法。
摘要由CSDN通过智能技术生成

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
inputCopy
3
3
.X.
XXX
.X.
6
XX.XXX
XXXXXX
XXX.XX
XXXXXX
XX.X.X
XXXXXX
5
XXX.X
.X…X
XXX.X
…X…
…X…
outputCopy
.X.
XOX
.X.
XX.XXO
XOXXOX
OXX.XX
XOOXXO
XX.X.X
OXXOXX
XOX.X
.X…X
XXO.O
…X…
…X…
Note
In the first test case, there are initially three ‘X’ consecutive in the second row and the second column. By changing the middle token to ‘O’ we make the grid a draw, and we only changed 1≤⌊5/3⌋ token.

In the second test case, we change only 9≤⌊32/3⌋ tokens, and there does not exist any three ‘X’ or ‘O’ consecutive in a row or column, so it is a draw.

In the third test case, we change only 3≤⌊12/3⌋ tokens, and the resulting grid is a draw.

题意:给你个图,要求不能有三个横着或者竖着的X相连,你可以把X改成O
当时是百思不得题解啊。。。看完真正的题解才恍然大明白。。
在这里插入图片描述
找最小的数量的X,把那个改了就行了。。

#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        string s[400];
        int cnt[3]={0,0,0};
        for(int i=0;i<n;i++)
        {
            cin>>s[i];
            for(int j=0;j<n;j++)
            {
                if(s[i][j]=='X')
                    cnt[(i+j)%3]++;
            }
        }
        int val=min_element(cnt,cnt+3)-cnt;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(s[i][j]=='X'&&(i+j)%3==val) s[i][j]='O';
            }
            cout<<s[i]<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JdiLfc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值