跳到正文

题解

题解:【ARC225】A - Four Coloring

Jason_wzc约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年7月21日
更新
2026年7月21日
篇幅
约 2 分钟

版本

  1. v1当前

题解:ARC225 - A

题面:Four Coloring。

解题思路

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

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

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

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

代码实现

C++
#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;
}
原文