結果

問題 No.217 魔方陣を作ろう
ユーザー t98slidert98slider
提出日時 2022-07-31 18:27:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,763 bytes
コンパイル時間 1,898 ms
コンパイル使用メモリ 180,940 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-28 05:27:51
合計ジャッジ時間 3,270 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

int N;

int safe_mod(int v){
    v %= N;
    if(v < 0)v += N;
    return v;
}

int main(){
    cin >> N;
    vector<vector<int>> A(N, vector<int>(N));
    int cnt = 1;
    if(N & 1){
        for(int i = 0; i < N; i++){
            for(int j = 0; j < N; j++){
                A[safe_mod(2 * i - j)][safe_mod(N / 2 - i + j)] = cnt++;
            }
        }
    }else if(N % 4 == 0){
        vector<vector<bool>> B = {{1,0,0,1},{0,1,1,0},{0,1,1,0},{1,0,0,1}};
        for(int i = 0; i < N; i++){
            for(int j = 0; j < N; j++){
                A[i][j] = (B[i][j] ? i * N + j + 1: N * N - (i * N + j));
            }
        }
    }else{
        N /= 2;
        vector<vector<int>> B(N, vector<int>(N));
        cnt--;
        for(int i = 0; i < N; i++){
            for(int j = 0; j < N; j++){
                B[safe_mod(2 * i - j)][safe_mod(N / 2 - i + j)] = 4 * cnt++;
            }
        }
        N *= 2;
        vector<vector<vector<int>>> L = {{{4, 1}, {2, 3}}, {{1, 4}, {2, 3}}, {{1, 4}, {3, 2}}};
        int c;
        for(int i = 0; i < N / 2; i++){
            for(int j = 0; j < N / 2; j++){
                if(i == N / 4 && j == N / 4)c = 1;
                else if(i == N / 4 + 1 && j == N / 4)c = 0;
                else c = (i <= N / 4 ? 0 : (i == N / 4 + 1 ? 1 : 2));
                for(int k = 0; k < 2; k++){
                    for(int l = 0; l < 2; l++){
                        A[2 * i + k][2 * j + l] = B[i][j] + L[c][k][l];
                    }
                }
            }
        }
    }

    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            if(j)cout << " ";
            cout << A[i][j];
        }
        cout << '\n';
    }
}
0