Codeforces Global Round 12 C2. Errich-Tac-Toe (Hard Version) 题解 构造

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 n n rows and n n 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 k k denote the total number of tokens in the grid. Your task is to make the grid a draw in at most ⌊ k 3 ⌋ \lfloor \frac{k}{3}\rfloor 3k (rounding down) operations.

You are not required to minimize the number of operations.

输入描述

The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1\le t\le 100 1t100) — the number of test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 300 1\le n\le 300 1n300) — the size of the grid.

The following n n n lines each contain a string of n n n characters, denoting the initial grid. The character in the i i i-th row and j j 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 n n across all test cases does not exceed 300 300 300.

输出描述

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.

样例输入 #1

3
3
.O.
OOO
.O.
6
XXXOOO
XXXOOO
XX..OO
OO..XX
OOOXXX
OOOXXX
5
.OOO.
OXXXO
OXXXO
OXXXO
.OOO.

样例输出 #1

.O.
OXO
.O.
OXXOOX
XOXOXO
XX..OO
OO..XX
OXOXOX
XOOXXO
.OXO.
OOXXO
XXOXX
OXXOO
.OXO.

提示

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 ⌋ 1\le \lfloor 5/3\rfloor 15/3 token.

In the second test case, the final grid is a draw. We only changed 8 ≤ ⌊ 32 / 3 ⌋ 8\le \lfloor 32/3\rfloor 832/3 tokens.

In the third test case, the final grid is a draw. We only changed 7 ≤ ⌊ 21 / 3 ⌋ 7\le \lfloor 21/3\rfloor 721/3 tokens.

原题

CF——传送门
洛谷——传送门

思路

形成胜局的条件有两个:

  • 水平上三个连续的 ‘O’ 或者 ‘X’
  • 竖直上三个连续的 ‘O’ 或者 ‘X’

而二者有一个共同性质,即由三个 x , y x,y x,y 坐标值之和连续递减 1 1 1 的点组成。所以,我们考虑将所有的点分成三类,即 ( x + y ) % 3 = = 0 , 1 , 2 (x+y)\%3==0,1,2 (x+y)%3==0,1,2 的三类点,将它们分别命名为第 0 类点,第 1 类点和第 2 类点。那么,可以找到其中一定能形成平局的三种构造:

  • 第 0 类点上的 ‘X’ 全部改为 ‘O’,第 1 类点上的 ‘O’ 全部改为 ‘X’
  • 第 1 类点上的 ‘X’ 全部改为 ‘O’,第 2 类点上的 ‘O’ 全部改为 ‘X’
  • 第 2 类点上的 ‘X’ 全部改为 ‘O’,第 0 类点上的 ‘O’ 全部改为 ‘X’

可以证明,这样的构造一定会使得任意水平上三个连续的点和任意竖直上三个连续的点都不会是全部为 ‘X’ 或全部为 ‘O’。
现在的问题是这样的构造是否能够满足操作次数至多为 ⌊ k 3 ⌋ \lfloor \frac{k}{3}\rfloor 3k。我们发现,上述的三种构造,操作次数之和一定为 k。那么根据鸽巢原理,一定存在一种构造,它的操作次数至多为 ⌊ k 3 ⌋ \lfloor \frac{k}{3}\rfloor 3k

代码

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

void solve()
{
    int n;
    cin >> n;
    vector<vector<char>> v(n, vector<char>(n));
    int k = 0; //'O'或'X'标记的总个数
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> v[i][j];
            if (v[i][j] != '.')
                k++;
        }
    }
    vector<vector<char>> ans; // 当找到满足条件的(a,b)对时,存储下替换后的答案
    int a, b;                 //(a,b)对用来表示(0,1),(1,2),(2,0)对
    auto cfg = [&](vector<vector<char>> g) -> bool
    {
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (g[i][j] == 'X' && (i + j) % 3 == a)
                {
                    g[i][j] = 'O';
                    cnt++;
                }
                if (g[i][j] == 'O' && (i + j) % 3 == b)
                {
                    g[i][j] = 'X';
                    cnt++;
                }
            }
        }
        if (cnt <= k / 3) // 操作次数小于等于k/3,满足条件
        {
            ans = g;
            return 1;
        }
        else
            return 0;
    };
    for (a = 0; a < 3; a++)
    {
        b = (a + 1) % 3;
        if (cfg(v))
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    cout << ans[i][j];
                }
                cout << '\n';
            }
            return;
        }
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}
  • 33
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值