結果

問題 No.217 魔方陣を作ろう
ユーザー maine_honzukimaine_honzuki
提出日時 2020-12-24 01:26:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,848 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 208,304 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 15:26:04
合計ジャッジ時間 3,137 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
4,348 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
4,348 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> solve(int N) {
    vector<vector<int>> magic(N, vector<int>(N));
    if (N & 1) {
        int i = 0, j = N / 2, num = 1;
        while (num <= N * N) {
            magic[i][j] = num++;
            (i += 1) %= N;
            (j += 1) %= N;
            if (magic[i][j])
                (j += N - 1) %= N;
        }
    } else if (N % 4 == 0) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                magic[i][j] = i * N + j + 1;
            }
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N / 2; j++) {
                if (i % 4 == j % 4 || i % 4 == 3 - (j % 4))
                    swap(magic[i][j], magic[N - 1 - i][N - 1 - j]);
            }
        }
    } else {
        auto origin = solve(N / 2);
        for (auto& V : origin)
            for (auto& x : V)
                (x -= 1) *= 4;
        int LUX[3][2][2] = {{{4, 1}, {2, 3}}, {{1, 4}, {2, 3}}, {{1, 4}, {3, 2}}};
        auto lux = [](int N, int i, int j) {
            if (i > N / 2 + 1)
                return 2;
            if ((i == N / 2 && j == N / 2) || (i == N / 2 + 1) && j != N / 2)
                return 1;
            return 0;
        };
        for (int i = 0; i < N / 2; i++) {
            for (int j = 0; j < N / 2; j++) {
                for (int k = 0; k < 2; k++) {
                    for (int l = 0; l < 2; l++) {
                        magic[i * 2 + k][j * 2 + l] = origin[i][j] + LUX[lux(N / 2, i, j)][k][l];
                    }
                }
            }
        }
    }
    return magic;
}

int main() {
    int N;
    cin >> N;

    auto magic = solve(N);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << magic[i][j] << " ";
        }
        cout << endl;
    }
}
0