結果

問題 No.217 魔方陣を作ろう
ユーザー otamay6otamay6
提出日時 2019-11-29 18:11:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,314 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 176,012 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-13 04:48:05
合計ジャッジ時間 2,843 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define REP(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define All(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;

int main(){
    int N;cin>>N;
    vector<vector<int>> ans(N,vector<int>(N,0));
    if(N&1){
        int row=0,col=N/2,n=0;
        while(++n){
            if(row==-1) row+=N;
            if(col==N) col-=N;
            if(ans[row][col]){
                row+=2;col--;
                row%=N;
                if(col==-1) col+=N;
            }
            if(ans[row][col]) break;
            ans[row][col]=n;
            row--;col++;
        }
    }
    else if(N%4==0){
        int nl=0,nu=0;
        vector<vector<bool>> t(N,vector<bool>(N,false));
        while(nu<N){
            REP(i,4){
                t[nl+i][nu+i]=true;
                t[nl+3-i][nu+i]=true;
            }
            nl+=4;
            if(nl==N){
                nu+=4;
                nl=0;
            }
        }
        REP(i,N) REP(j,N){
            if(t[i][j]) ans[i][j]=N*i+j+1;
            else ans[i][j]=N*N-(N*i+j);
        }
    }
    else{
        vector<vector<int>> pre(N/2,vector<int>(N/2,0));
        int row=0,col=N/4,n=0;
        while(++n){
            if(row==-1) row+=N/2;
            if(col==N/2) col-=N/2;
            if(pre[row][col]){
                row+=2;col--;
                row%=N/2;
                if(col==-1) col+=N/2;
            }
            if(pre[row][col]) break;
            pre[row][col]=n;
            row--;col++;
        }
        REP(i,N/2) REP(j,N/2){
            pre[i][j]--;
            pre[i][j]*=4;
        }
        REP(i,N/2) REP(j,N/2){
            if(i<=N/4+1){
                ans[2*i][2*j]=4+pre[i][j];
                ans[2*i][2*j+1]=1+pre[i][j];
                ans[2*i+1][2*j]=2+pre[i][j];
                ans[2*i+1][2*j+1]=3+pre[i][j];
                if(i==N/4&&j==N/4||(i==N/4+1&&j!=N/4)){
                    swap(ans[2*i][2*j],ans[2*i][2*j+1]);
                }
            }else{
                ans[2*i][2*j]=1+pre[i][j];
                ans[2*i][2*j+1]=4+pre[i][j];
                ans[2*i+1][2*j]=3+pre[i][j];
                ans[2*i+1][2*j+1]=2+pre[i][j];
            }
        }
    }
    REP(i,N) REP(j,N){
        cout<<ans[i][j]<<" \n"[j==N-1];
    }
}
0