cf1450 C2. Errich-Tac-Toe (Hard Version)(数学,思维,字符串)

题目链接:https://codeforces.ml/contest/1450/problem/C2

题意:给定一个长度为n(n<=300)的字符正方形,'.' , 'X' , 'O'。'X'可以与'O'互换,cnt为'X'与'O'的个数和,请改变x个不为 '.' 的保证字符,其中x<=cnt/3下取整,使不出现任意三个连续的字符为'O'或’X'。保证一定存在这样的操作。

题解:分别统计t=(i+j)%3的位置的‘X',与'O'数。x0,x1,x2,o1,o2,o3分别对应t值'X'或'O'的个数。

如图,值相等的为一条斜分割线,如果在0,1或者1,2或者2,0之间,前所有x->o,后所有o->下即可满足条件。然后只需证明x<=cnt/3。

2012012012
0120120120
1201201201
2012012012
0120120120
1201201201
2012012012
0120120120
1201201201
2012012012

证明:首先x0+x1+x2+o0+o1+o2=cnt。然后x0+o1,x1+o2,x2+o0,至少一对满足x<=cnt/3。(至于C1题,就是这题的简化版。u1s1,我感觉我现在挺擅长做字符串的题)

啊这*2300??

代码:

#include <bits/stdc++.h>

#define ll long long
#define ld double
#define pi acos(-1)
#define pb push_back
#define mst(a, i) memset(a, i, sizeof(a))
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define rep(i, a, n) for (ll i = a; i <= n; i++)
#define per(i, n, a) for (ll i = n; i >= a; i--)
#define dbg(x) cout << #x << "===" << x << endl
#define dbgg(l, r, x)                                                          \
    for (ll i = l; i <= r; i++)                                                \
        cout << x[i] << " ";                                                   \
    cout << "<<<" << #x;                                                       \
    cout << endl
using namespace std;

template <class T> void read(T &x) {
    T res = 0, f = 1;
    char c = getchar();
    while (!isdigit(c)) {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (isdigit(c)) {
        res = (res << 3) + (res << 1) + c - '0';
        c = getchar();
    }
    x = res * f;
}
inline void print(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
const ll maxn = 3e2 + 10;
const ll mod = 1e9 + 7;

ll n, a[5], x[5], o[5];
char c[maxn][maxn];
int main() {
    ll _s = 1;
    read(_s);
    // freopen("testdata.in","r",stdin);
    // freopen("testout.out","w",stdout);

    for (ll _ = 1; _ <= _s; _++) {
        mst(x, 0), mst(o, 0);
        read(n);
        ll cnt = 0;
        rep(i, 1, n) {
            rep(j, 1, n) {
                cin >> c[i][j];
                if (c[i][j] == 'O') o[(i + j) % 3]++, cnt++;
                if (c[i][j] == 'X') x[(i + j) % 3]++, cnt++;
            }
        }
        ll p = 0; //?x->o
        rep(i, 0, 2) {
            if (x[i] + o[(i + 1) % 3] <= cnt / 3) {
                p = i;
                break;
            }
        }
        rep(i, 1, n) {
            rep(j, 1, n) {
                ll t = (i + j) % 3;
                if (t == p && c[i][j] == 'X') c[i][j] = 'O';
                if (t == (p + 1) % 3 && c[i][j] == 'O') c[i][j] = 'X';
            }
        }
        rep(i, 1, n) {
            rep(j, 1, n) { cout << c[i][j]; }
            cout << endl;
        }
    }
    return 0;
}
/*
input:::

output:::

*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值