結果

問題 No.217 魔方陣を作ろう
ユーザー nebukuro09nebukuro09
提出日時 2016-12-31 03:44:24
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,375 bytes
コンパイル時間 687 ms
コンパイル使用メモリ 106,892 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 00:20:50
合計ジャッジ時間 1,703 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;


int[][] ms_odd(int n) {
    auto ans = new int[][](n, n);
    int r = 0;
    int c = n / 2;
    foreach (i; 1..n*n+1) {
        ans[r][c] = i;
        int nr = r == 0 ? n - 1 : r-1;
        int nc = (c+1) % n;
        if (ans[nr][nc] > 0) {
            nr = (r+1) % n;
            nc = c;
        }
        r = nr;
        c = nc;
    }
    return ans;
}

int[][] ms_4n(int n) {
    auto ans = new int[][](n, n);

    int i = 1;
    foreach (r; 0..n) {
        foreach (c; 0..n) {
            if ((r % 4 == 0 || r % 4 == 3) && (c % 4 == 0 || c % 4 == 3))
                ans[r][c] = i;
            if ((r % 4 == 1 || r % 4 == 2) && (c % 4 == 1 || c % 4 == 2))
                ans[r][c] = i;
            i++;
        }
    }

    i = 1;
    foreach (r; iota(n-1, -1, -1)) {
        foreach (c; iota(n-1, -1, -1)) {
            if (ans[r][c] == 0)
                ans[r][c] = i;
            i++;
        }
    }
    return ans;
}

int[][] ms_4n2(int n) {
    auto ans = new int[][](n, n);
    auto base = ms_odd(n/2);

    foreach (r; 0..n/2)
        foreach (c; 0..n/2)
            base[r][c] = (base[r][c]-1) * 4;

    auto LUX = new char[][](n/2, n/2);
    foreach (r; 0..n/2)
        foreach (c; 0..n/2) {
            if (r <= n/4)
                LUX[r][c] = 'L';
            else if (r == n/4+1)
                LUX[r][c] = 'U';
            else
                LUX[r][c] = 'X';
        }
    swap(LUX[n/4][n/4], LUX[n/4+1][n/4]);

    foreach (r; 0..n/2) {
        foreach (c; 0..n/2) {
            auto p = new int[][](2, 2);
            if      (LUX[r][c] == 'L') p = [[4, 1], [2, 3]];
            else if (LUX[r][c] == 'U') p = [[1, 4], [2, 3]];
            else if (LUX[r][c] == 'X') p = [[1, 4], [3, 2]];
            foreach (dr; 0..2) {
                foreach (dc; 0..2) {
                    ans[r*2+dr][c*2+dc] = p[dr][dc] + base[r][c];
                }
            }

        }
    }

    return ans;
}

void main() {
    auto N = readln.chomp.to!int;

    int[][] ans;

    if (N % 2 == 1)
        ans = ms_odd(N);
    else if (N % 4 == 0)
        ans = ms_4n(N);
    else
        ans = ms_4n2(N);

    ans.each!(a => a.map!(to!string).join(" ").writeln);


}
0