結果

問題 No.217 魔方陣を作ろう
ユーザー h_nosonh_noson
提出日時 2016-04-23 13:17:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,690 bytes
コンパイル時間 512 ms
コンパイル使用メモリ 62,584 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 04:18:28
合計ジャッジ時間 1,064 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define RREP(i,s,e) for (i = s; i >= e; i--)
#define rrep(i,n) RREP(i,n,0)
#define REP(i,s,e) for (i = s; i < e; i++)
#define rep(i,n) REP(i,0,n)
#define INF 100000000

typedef long long ll;

int main() {
    int i, j, n;
    int s[20][20] {};
    cin >> n;
    if (n % 2 == 1) {
        int x, y;
        x = 0;
        y = n/2;
        REP (i,1,n*n+1) {
            s[x][y] = i;
            x = (x-1+n) % n;
            y = (y+1) % n;
            if (s[x][y]) {
                x = (x+2) % n;
                y = (y-1+n) % n;
            }
        }
    }
    else if (n % 4 == 0) {
        int x, y;
        rep (i,n) {
            rep (j,n) {
                x = i % 4;
                y = j % 4;
                if (x == y || 3-x == y)
                    s[i][j] = i*n + j + 1;
                else
                    s[i][j] = n*n - i*n - j;
            }
        }
    }
    else {
        int sub[9][9] {};
        int x, y;
        x = 0;
        y = n/4;
        REP (i,1,n*n/4+1) {
            sub[x][y] = i;
            x = (x-1+n/2) % (n/2);
            y = (y+1) % (n/2);
            if (sub[x][y]) {
                x = (x+2) % (n/2);
                y = (y-1+n/2) % (n/2);
            }
        }
        rep (x,n/2) rep (y,n/2) {
            sub[x][y] = (sub[x][y] - 1) * 4;
            if (x == n/4 && y == n/4) {
                s[x*2][y*2] = sub[x][y] + 1;
                s[x*2+1][y*2] = sub[x][y] + 2;
                s[x*2][y*2+1] = sub[x][y] + 4;
                s[x*2+1][y*2+1] = sub[x][y] + 3;
            }
            else if (x == n/4+1 && y == n/4) {
                s[x*2][y*2] = sub[x][y] + 4;
                s[x*2+1][y*2] = sub[x][y] + 2;
                s[x*2][y*2+1] = sub[x][y] + 1;
                s[x*2+1][y*2+1] = sub[x][y] + 3;
            }
            else if (x <= n/4) {
                s[x*2][y*2] = sub[x][y] + 4;
                s[x*2+1][y*2] = sub[x][y] + 2;
                s[x*2][y*2+1] = sub[x][y] + 1;
                s[x*2+1][y*2+1] = sub[x][y] + 3;
            }
            else if (x == n/4+1) {
                s[x*2][y*2] = sub[x][y] + 1;
                s[x*2+1][y*2] = sub[x][y] + 2;
                s[x*2][y*2+1] = sub[x][y] + 4;
                s[x*2+1][y*2+1] = sub[x][y] + 3;
            }
            else {
                s[x*2][y*2] = sub[x][y] + 1;
                s[x*2+1][y*2] = sub[x][y] + 3;
                s[x*2][y*2+1] = sub[x][y] + 4;
                s[x*2+1][y*2+1] = sub[x][y] + 2;
            }
        }
    }
    rep (i,n) {
        rep (j,n)
            cout << s[i][j] << " ";
        cout << endl;
    }
    return 0;
}

0