题解:【ARC225】A - Four Coloring

· · 题解

题解:ARC225 - A

题面:Four Coloring。

解题思路

虽然是道构造题,但是比想象中的要简单。

由于题目中 Y 中相邻格子的权值由 X 的决定,又因为题目中给出了 X 中相邻格子颜色不同这一条件,同时 X 中的权值只能为 1 \sim 4,那么可以想到直接构造一个序列 A,由 X 的权值 i 直接对应 Y 的权值 A_i。

即:对于任意 i - j = 1,2 \leq A_i - A_j \leq 3;对于任意 2 \leq i - j \leq 3,A_i - A_j \leq 1。对这个进行一次暴力枚举,很容易得到 A = \{ 2, 4, 1, 3 \} 这个序列。

最后根据此序列进行输入输出即可,在输入 X_{i, j} 时,直接输出 Y_{i, j} = A_{X_{i, j}} 即可。

代码实现

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <atcoder/all>
#define endl '\n'
using namespace std;
typedef long long ll;

int main(int argc, char* argv[]) {
    // freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    int n; cin >> n;
    array<int, 5> res = { 0, 2, 4, 1, 3 }; // res[0] 进行占位
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int x; cin >> x;
            cout << res[x] << ' ';
        }
        cout << endl;
    }
    return 0;
}