結果

問題 No.217 魔方陣を作ろう
ユーザー koyoprokoyopro
提出日時 2015-10-13 16:12:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,920 bytes
コンパイル時間 2,436 ms
コンパイル使用メモリ 146,400 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 12:54:14
合計ジャッジ時間 2,838 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
int pymod(int i, int j) {
    return (i % j + j) % j;
}

int N,T;
int M[30][30];
void oddsqure(int n) {
    int r = 0;
    int c = n / 2;
    REP(i,n*n) {
        M[r][c] = i + 1;
        int nr = pymod(r - 1, n);
        int nc = pymod(c + 1, n);
        if (M[nr][nc] != 0) {
            r = pymod(r + 1, n);
        } else {
            r = nr;
            c = nc;
        }
    }
}
void times4square(int n) {
    int i = 0;
    REP(r,n) REP(c,n) {
        i++;
        if (r%4 == c%4 || r%4 == 3 - c%4) {
            M[r][c] = i;
        }
    }
    i = 0;
    RREP(r,n) RREP(c,n) {
        i++;
        if (M[r][c]) continue;
        M[r][c] = i;
    }
}
signed main()
{
    cin >> N;
    if (N%2) oddsqure(N);
    else if (N%4 == 0) times4square(N);
    else {
        oddsqure(N/2);
        int P[30][30];
        int L[2][2] = {{4, 1}, {2, 3}};
        int U[2][2] = {{1, 4}, {2, 3}};
        int X[2][2] = {{1, 4}, {3, 2}};
        int K[2][2];
        REP(r,N) REP(c,N) M[r][c] = 4 * (M[r][c] - 1);
        REP(r,N/2) REP(c,N/2) {
            if (r > N/2/2 + 1) {
                REP(ar,2) REP(ac,2) K[ar][ac] = X[ar][ac];
            } else if (r == N/2/2 + 1 && c != N/2/2) {
                REP(ar,2) REP(ac,2) K[ar][ac] = U[ar][ac];
            } else if (r == N/2/2 && c == N/2/2) {
                REP(ar,2) REP(ac,2) K[ar][ac] = U[ar][ac];
            } else {
                REP(ar,2) REP(ac,2) K[ar][ac] = L[ar][ac];
            }
            REP(ar, 2) REP(ac, 2) {
                P[2*r + ar][2*c + ac] = M[r][c] + K[ar][ac];
            }
        }
        REP(r,N) REP(c,N) M[r][c] = P[r][c];
    }
    REP(col,N) {
        REP(row,N) {
            if  (row) cout << " ";
            cout << M[col][row];
        }
        cout << endl;
    }
    return 0;
}
0