結果

問題 No.217 魔方陣を作ろう
ユーザー tko919tko919
提出日時 2020-12-23 21:43:50
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,651 bytes
コンパイル時間 3,302 ms
コンパイル使用メモリ 172,800 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 15:18:40
合計ジャッジ時間 2,997 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 WA -
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 WA -
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 WA -
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{
      auto sub=magic(n/2);
      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,n/2)rep(j,0,n/2){
         int type;
         if(i<=n/2){
            if(j==n/2)type=1;
            else type=0;
         }
         else if(i==n/2+1){
            if(j==n/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]<<' ';
      cout<<'\n';
   }
   return 0;
}
0