ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CF1450C1 Errich-Tac-Toe (Easy Version)/CF1450C2 Errich-Tac-Toe (Hard Version)

2020-12-16 10:01:14  阅读:260  来源: 互联网

标签:直线 ch tokens Version grid Tac Errich Toe


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$,将这个/这些直线组的所有方块改变标记种类即可

一定能找到至少一个/一对直线组

#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;
}
Errich-Tac-Toe

 

标签:直线,ch,tokens,Version,grid,Tac,Errich,Toe
来源: https://www.cnblogs.com/JDFZ-ZZ/p/14142398.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有