結果

問題 No.217 魔方陣を作ろう
ユーザー face4face4
提出日時 2019-10-19 16:47:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,174 bytes
コンパイル時間 789 ms
コンパイル使用メモリ 74,852 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-09 14:22:47
合計ジャッジ時間 2,304 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

vector<vector<int>> f(int n){
    vector<vector<int>> ret(n, vector<int>(n, 0));
    int now = 1, i = 0, j = n/2;
    while(now <= n*n){
        if(ret[i][j])   i = (i+2)%n, j = (j-1+n)%n;
        ret[i][j] = now++;
        i = (i-1+n)%n, j = (j+1)%n;
    }
    return ret;
}

vector<vector<int>> g(int n){
    vector<vector<int>> ret(n, vector<int>(n, 0));
    vector<bool> used(n*n+1, 0);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(i%4 == 0 || i%4 == 3){
                if(j%4 == 0 || j%4 == 3)    ret[i][j] = i*n+j+1, used[ret[i][j]] = true;
            }else if(i%4 == 1 || i%4 == 2){
                if(j%4 == 1 || j%4 == 2)    ret[i][j] = i*n+j+1, used[ret[i][j]] = true;
            }
        }
    }
    int now = 1;
    for(int i = n-1; i >= 0; i--){
        for(int j = n-1; j >= 0; j--){
            if(ret[i][j])   continue;
            while(used[now])    now++;
            ret[i][j] = now++;
        }
    }
    return ret;
}

int lux[3][2][2] = {
    {{4, 1}, {2, 3}},
    {{1, 4}, {2, 3}},
    {{1, 4}, {3, 2}}
};

vector<vector<int>> h(int n){
    vector<vector<int>> ret(n, vector<int>(n, 0));
    vector<vector<int>> ing = f(n/2);
    for(int i = 0; i < n/2; i++){
        for(int j = 0; j < n/2; j++){
            ing[i][j] = (ing[i][j]-1)*4;
        }
    }
    for(int i = 0; i < n/2; i++){
        for(int j = 0; j < n/2; j++){
            int ind;
            if(i <= n/2/2)          ind = j==n/2/2&&i==n/2/2;
            else if(i > n/2/2+1)    ind = 2;
            else                    ind = !(j==n/2/2&&i==n/2/2+1);
            for(int k = 0; k < 2; k++){
                for(int l = 0; l < 2; l++){
                    ret[i*2+k][j*2+l] = ing[i][j]+lux[ind][k][l];
                }
            }
        }
    }
    return ret;
}

int main(){
    int n;
    cin >> n;
    vector<vector<int>> v;
    if(n%2 == 1)        v = f(n);
    else if(n%4 == 0)   v = g(n);
    else                v = h(n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cout << v[i][j] << " \n"[j==n-1];
        }
    }
    return 0;
}
0