結果

問題 No.217 魔方陣を作ろう
ユーザー tko919tko919
提出日時 2020-12-23 21:50:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,689 bytes
コンパイル時間 1,468 ms
コンパイル使用メモリ 173,260 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 15:18:58
合計ジャッジ時間 2,578 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

typedef vector<vector<int>> VV;

VV magic(int n){
   VV res(n,vector<int>(n));
   if(n&1){
      int x=0,y=n/2,cur=1;
      rep(_,0,n*n){
         res[x][y]=cur++;
         int nx=(x-1+n)%n;
         int ny=(y+1)%n;
         if(res[nx][ny])x=(x+1)%n;
         else{swap(x,nx); swap(y,ny);}
      }
   }
   else if(n%4==0){
      rep(i,0,n)rep(j,0,n){
         int cur=i*n+j+1;
         if((i%4==1 or i%4==2)+(j%4==1 or j%4==2)==1){
            res[i][j]=n*n+1-cur;
         }
         else{
            res[i][j]=cur;
         }
      }
   }
   else{
      int m=n/2;
      auto sub=magic(m);
      for(auto& v:sub)for(auto& x:v)x=(x-1)*4;
      int add[3][2][2]={{{4,1},{2,3}},{{1,4},{2,3}},{{1,4},{3,2}}};
      rep(i,0,m)rep(j,0,m){
         int type;
         if(i<m/2)type=0;
         else if(i==m/2){
            if(j==m/2)type=1;
            else type=0;
         }
         else if(i==m/2+1){
            if(j==m/2)type=0;
            else type=1;
         }
         else type=2;
         rep(a,0,2)rep(b,0,2)res[i*2+a][j*2+b]=sub[i][j]+add[type][a][b];
      }
   }
   return res;
}

int main(){
   int n; cin>>n;
   auto res=magic(n);
   rep(i,0,n){
      rep(j,0,n)cout<<res[i][j]<<(j==n-1?'\n':' ');
   }
   return 0;
}
0