比赛 2025暑期集训第7场 评测结果 ATAAAAAAAA
题目名称 填数 最终得分 90
用户昵称 ChenBp 运行时间 3.380 s
代码语言 C++ 内存使用 3.62 MiB
提交时间 2025-08-11 15:37:57
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 13;
//  0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21
const bool p[350] = {
    0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,
    1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,
    0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
    0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
    0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0,
    0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
int n;
bool vis[N * N];
int g[N][N];
int ans[N][N];
bool ch(int x, int y, int num) {
    if (x != 1 && !p[g[x - 1][y] + num])
        return 0;
    if (y != 1 && !p[g[x][y - 1] + num])
        return 0;
    return 1;
}
void dfs(int x, int y) {
    // cout << x << " " << y << endl;
    if (x == n + 1) {
        int ok = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (g[i][j] < ans[i][j]) {
                    ok = 1;
                    break;
                }
                if (g[i][j] > ans[i][j]) {
                    return;
                }
            }
            if (ok)
                break;
        }
        memcpy(ans, g, sizeof(g));
        return;
    }
    if (y == n + 1) {
        dfs(x + 1, 1);
    }
    int s, k;
    if (y == 1) {
        s = 2;
        k = 1;
    } else if (g[x][y - 1] & 1) {
        s = 2;
        k = 2;
    } else {
        s = 3;
        k = 2;
    }
    for (int i = s; i <= ans[x][y]; i += k) {
        if (!vis[i] && ch(x, y, i)) {
            g[x][y] = i;
            vis[i] = 1;
            dfs(x, y + 1);
            vis[i] = 0;
            g[x][y] = 0;
        }
    }
}
int main() {
    freopen("tianshu.in", "r", stdin);
    freopen("tianshu.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            ans[i][j] = n * n;
        }
    }
    g[1][1] = 1;
    vis[1] = 1;
    dfs(1, 2);
    if (ans[1][1] == n * n) {
        cout << "NO";
        return 0;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << ans[i][j] << " \n"[j == n];
        }
    }
    return 0;
}