CF1450C1 题解

· · 题解

思路

对矩阵进行 (i+j)\bmod3 编号,只要改变其中一种颜色的所有 X 棋子就不会出现连续 3 个行列同色。

记得选最小的,最小的那个肯定 \le\lfloor\frac k3\rfloor。

代码

# include <bits/stdc--.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
# define print(a) for (int i = 0; i < n; ++ i) cout << a[i] << '\n'; //我懒
int t, n, tot1, tot2, tot3;
string a[305], b[305], c[305];
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> t;
    while (t --) {
        tot1 = tot2 = tot3 = 0;
        cin >> n;
        for (int i = 0; i < n; ++ i) {
            cin >> a[i];
            c[i] = b[i] = a[i];
            for (int j = 0; j < n; ++ j)
                if (a[i][j] == 'X')
                    if ((i + j) % 3 == 0)
                        a[i][j] = 'O', ++ tot1;
                    else if ((i + j) % 3 == 1)
                        b[i][j] = 'O', ++ tot2;
                    else
                        c[i][j] = 'O', ++ tot3;
        }
        if (tot1 <= tot2 && tot1 <= tot3)
            print (a)
        else if (tot2 <= tot1 && tot2 <= tot3)
            print (b)
        else
            print (c)
    }
    return 0;
}