結果

問題 No.217 魔方陣を作ろう
ユーザー fumofumofunifumofumofuni
提出日時 2021-03-15 16:28:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,603 bytes
コンパイル時間 2,359 ms
コンパイル使用メモリ 203,084 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-24 19:02:23
合計ジャッジ時間 3,264 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
 ll MOD=1000000007;
 ll MOD9=998244353;
 int inf=1e9+10;
 ll INF=1e18;
 ll dy[8]={1,0,-1,0,1,1,-1,-1};
 ll dx[8]={0,1,0,-1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}
vvl solve1(ll n){
  vvl mat(n,vl(n));
  ll now=1,r=0,c=n/2;
  while(now<=n*n){
    mat[r][c]=now;
    now++;
    r=(r-1+n)%n,c=(c+1+n)%n;
    if(mat[r][c]){
      r=(r+2+n)%n,c=(c-1+n)%n;
    }
  }
  return mat;
}
vvl solve2(ll n){
  vvl mat(n,vl(n));
  ll now=1;
  rep(i,n){
    rep(j,n){
      if(abs(i-j)%4==0||(i+j)%4==3)mat[i][j]=now;
      now++;
    }
  }
  now=1;
  per(i,n){
    per(j,n){
      if(!(abs(i-j)%4==0||(i+j)%4==3))mat[i][j]=now;
      now++;
    }
  }
  return mat;
}
vvl solve3(ll n){
  vvl pl=solve1(n/2);
  rep(i,n/2)rep(j,n/2)pl[i][j]=(pl[i][j]-1)*4;
  vvl lux(n/2,vl(n/2));
  rep(i,n/2)lux[n/2/2+1][i]=1;//u
  rep(j,n/2){
    for(int i=n/2/2+2;i<n/2;i++)lux[i][j]=2;//x;
  }
  swap(lux[n/2/2][n/2/2],lux[n/2/2+1][n/2/2]);
  /*rep(i,n/2){
    rep(j,n/2)cout << lux[i][j] <<" ";cout << endl;
  }*/
  vvl mat(n,vl(n));
  rep(i,n/2){
    rep(j,n/2){
      if(lux[i][j]==0){
        mat[i*2][j*2]=4;
        mat[i*2][j*2+1]=1;
        mat[i*2+1][j*2]=2;
        mat[i*2+1][j*2+1]=3;
      }
      if(lux[i][j]==1){
        mat[i*2][j*2]=1;
        mat[i*2][j*2+1]=4;
        mat[i*2+1][j*2]=2;
        mat[i*2+1][j*2+1]=3;
      }
      if(lux[i][j]==2){
        mat[i*2][j*2]=1;
        mat[i*2][j*2+1]=4;
        mat[i*2+1][j*2]=3;
        mat[i*2+1][j*2+1]=2;
      }
    }
  }
  rep(i,n/2){
    rep(j,n/2){
      mat[i*2][j*2]+=pl[i][j];
      mat[i*2][j*2+1]+=pl[i][j];
      mat[i*2+1][j*2]+=pl[i][j];
      mat[i*2+1][j*2+1]+=pl[i][j];
    }
  }
  return mat;
}
int main(){
  ll n;cin >> n;
  vvl mat;
  if(n%2)mat=solve1(n);
  else if(n%4==0)mat=solve2(n);
  else mat=solve3(n);
  rep(i,n){
    rep(j,n)cout << mat[i][j] <<" ";cout << endl;
  }
}
0