結果

問題 No.217 魔方陣を作ろう
ユーザー mayoko_mayoko_
提出日時 2015-05-26 23:28:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,716 bytes
コンパイル時間 1,330 ms
コンパイル使用メモリ 151,940 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 15:22:05
合計ジャッジ時間 2,464 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 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,380 KB
testcase_17 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

void print(vector<vector<int> > mat) {
    int n = mat.size();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << mat[i][j];
            if (j < n-1) cout << " ";
        }
        cout << endl;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<vector<int> > mat(n, vector<int>(n));
    if (n % 2 == 1) {
        mat[0][n/2] = 1;
        int y = 0, x = n/2;
        int cur = 2;
        for (int i = 0; i < n*n-1; i++) {
            y = (y+n-1)%n;
            x = (x+1)%n;
            if (mat[y][x] != 0) {
                y = (y+2)%n;
                x = (x+n-1)%n;
            }
            mat[y][x] = cur++;
        }
    } else if (n % 4 == 0) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int tmp = n*i+j+1;
                if (i%4 == 0 || i % 4 == 3) {
                    if (j%4 == 0 || j % 4 == 3) {
                        mat[i][j] = tmp;
                    }
                } else {
                    if (j%4 == 1 || j%4 == 2) {
                        mat[i][j] = tmp;
                    }
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int tmp = n*i+j+1;
                if (mat[n-1-i][n-1-j] == 0) mat[n-1-i][n-1-j] = tmp;
            }
        }
    } else {
        int N = n/2;
        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mat[i][j] = -1;
        vector<vector<int> > mm(N, vector<int>(N, -1));
        mm[0][N/2] = 0;
        int y = 0, x = N/2;
        int cur = 1;
        for (int i = 0; i < N*N-1; i++) {
            y = (y+N-1)%N;
            x = (x+1)%N;
            if (mm[y][x] != -1) {
                y = (y+2)%N;
                x = (x+n-1)%N;
            }
            mm[y][x] = cur*4;
            cur++;
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (i <= N/2) {
                    if (j != N/2 || i != N/2) {
                        mat[i*2][j*2] = mm[i][j]+4;
                        mat[i*2][j*2+1] = mm[i][j]+1;
                        mat[i*2+1][j*2] = mm[i][j]+2;
                        mat[i*2+1][j*2+1] = mm[i][j]+3;
                    } else {
                        mat[i*2][j*2] = mm[i][j]+1;
                        mat[i*2][j*2+1] = mm[i][j]+4;
                        mat[i*2+1][j*2] = mm[i][j]+2;
                        mat[i*2+1][j*2+1] = mm[i][j]+3;
                    }
                } else if (N/2+1 < i && i <= N-1) {
                    mat[i*2][j*2] = mm[i][j]+1;
                    mat[i*2][j*2+1] = mm[i][j]+4;
                    mat[i*2+1][j*2] = mm[i][j]+3;
                    mat[i*2+1][j*2+1] = mm[i][j]+2;
                } else {
                    if (j == N/2 && i == N/2+1) {
                        mat[i*2][j*2] = mm[i][j]+4;
                        mat[i*2][j*2+1] = mm[i][j]+1;
                        mat[i*2+1][j*2] = mm[i][j]+2;
                        mat[i*2+1][j*2+1] = mm[i][j]+3;
                    } else {
                        mat[i*2][j*2] = mm[i][j]+1;
                        mat[i*2][j*2+1] = mm[i][j]+4;
                        mat[i*2+1][j*2] = mm[i][j]+2;
                        mat[i*2+1][j*2+1] = mm[i][j]+3;
                    }
                }
            }
        }
    }
    print(mat);
    return 0;
}
0