首页 > 其他分享 >Codeforces Global Round 12 C2. Errich-Tac-Toe (Hard Version) 题解 构造

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

时间:2024-05-24 15:28:20浏览次数:16  
标签:12 int 题解 类点 Hard ++ le grid test

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 1≤t≤100) — 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 1≤n≤300) — 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 1≤⌊5/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 8≤⌊32/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 7≤⌊21/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;
}

标签:12,int,题解,类点,Hard,++,le,grid,test
From: https://blog.csdn.net/bbc_plus/article/details/139166858

相关文章

  • 《错过你的那些年》迅雷下载BT磁力[1.55GB/HD1280P]中字双语高清
    《错过你的那些年》是一部由《月亮爱人》导演田壮壮执导,杨幂、邓超、朱亚文主演的爱情电影。影片根据郭敬明的同名小说改编,讲述了一对初中同学在错过和重逢中的爱情故事。这部电影以独特的叙事风格和真挚的情感,深深触动了观众的心灵。 电影以回忆的方式展开,主要讲......
  • 惊艳亮相!Win12风格系统主题,让你的电脑焕然一新
    你们是否厌倦了千篇一律的操作系统界面,渴望为电脑带来一丝新鲜感和个性魅力?今天,我们为你带来了一个令人振奋的好消息——全新Win12风格系统主题正式上线,让你的电脑焕然一新,彰显你的独特品味!这款Win12风格系统主题,不仅完美还原了Win12的简约大气风格,更在细节处融入了现代审美......
  • 蓝桥楼赛第30期-Python-第二天赛题 题解
    楼赛第30期Python模块大比拼解析网页元素目标本次挑战,我们需要使用Python访问软科世界大学排行榜来获取首页30所学校的信息。为避免目标网站的内容发生变化,我们使用保存之后的网页进行实验。链接如下:https://labfile.oss.aliyuncs.com/courses/4070/rank2021.h......
  • [lnsyoj121/luoguP4513]小白逛公园
    题意原题链接给定序列\(a\),要求处理单点修改和查询区间最大子段和sol单点修改,区间查询,考虑线段树UPDATE操作对于一个区间,其最大子段和的位置只会有三种情况:在左子区间在右子区间在左右两区间都有如果是前两种情况,那么答案就是对应子区间的最大子段和如果是第三种情况......
  • 优美子序列 题解
    有n个整数从左往右排成一行,构成一个序列a。如果通过删除原序列的若干个数(可以是删除0个),其他数保持位置不动,那么得到的序列就称为“子序列”。记sum表示序列a的所有数的总和,即sum=a[1]+a[2]+a[3]+...+a[n]。如果一个“子序列”的各个数加起来的和等于sum-1,那么这个“子序列”......
  • 题解:聪聪与可可(概率与期望)
    [NOI2005]聪聪与可可题目描述在一个魔法森林里,住着一只聪明的小猫聪聪和一只可爱的小老鼠可可。虽然灰姑娘非常喜欢她们俩,但是,聪聪终究是一只猫,而可可终究是一只老鼠,同样不变的是,聪聪成天想着要吃掉可可。一天,聪聪意外得到了一台非常有用的机器,据说是叫GPS,对可可能准确的定位......
  • [Usaco2017 Open]Bovine Genomics 题解^&*^(
    不知道为啥,我死活想不到二分(楼下正解)所以,就有了这篇题解可以看到,这道题离暴力的距离只有一步!就是数组开不下!!小问答:数组开不下时,你会?A:mapB:优化代码C:gp_hash_table由于正在学hash,所以容易想到...tong[本来的下标%9999999]然后就玄学的过了。。。ACcode#include<bi......
  • 世微AP2121太阳能草坪灯驱动芯片
    概述AP2121是一款专为太阳能LED草坪灯设计的专用集成电路。仅需一个外接电感即可组成太阳能照明装置。由开关型驱动电路、光控开关电路、过放保护电路、内部集成的肖特基二极管等电路组成。AP2121采用专利技术,使得欠压关断时LED灯无闪烁AP2121工作电压为0.9V到1.5V,适合......
  • AP2917双路输出降压恒流驱动IC 5-100V 12W 摩托车灯照明IC
    产品描述AP2917是一款可以一路灯串切换两路灯串的降压恒流驱动器,高效率、外围简单、内置功率管,适用于5-100V输入的高精度降压LED恒流驱动芯片。内置功率管输出最大功率可达12W,最大电流1.2A。AP2917一路灯亮切换两路灯亮,其中一路灯亮可以全亮,可以半亮。AP2917工作......
  • 世微AP5125 外置MOS管5-100V 8A平均电流型LED降压恒流驱动器 SOT23-6
    产品描述  AP5125是一款外围电路简单的Buck型平均电流检测模式的LED恒流驱动器,适用于8-100V电压范围的非隔离式大功率恒流LED驱动领域。芯片采用固定频率140kHz的PWM工作模式,利用平均电流检测模式,因此具有优异的负载调整率特性,高精度的输出电流特性。AP5125......