結果

問題 No.217 魔方陣を作ろう
ユーザー itezpaceitezpace
提出日時 2016-06-24 15:35:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,550 bytes
コンパイル時間 749 ms
コンパイル使用メモリ 72,124 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-02 01:44:23
合計ジャッジ時間 1,926 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 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,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 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>> o(int n){
  vector<vector<int>> a(n);
  for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) a[i].push_back(-1);

  int x,r,c,f;
  x=1;
  r=0;
  c=n/2;
  a[r][c]=x;

  while(1){
    x++;
    if(x>n*n){
      break;
    }
    if(r==0){
      r=n-1;
    } else {
      r--;
    }
    if(c==n-1){
      c=0;
    } else {
      c++;
    }
    if(a[r][c]==-1){
      a[r][c]=x;
    } else {
      if(r==n-1){
        r=0;
      } else {
        r++;
      }
      if(c==0){
        c=n-1;
      } else {
        c--;
      }
      if(r==n-1){
        r=0;
      } else {
        r++;
      }
      if(a[r][c]==-1) {
        a[r][c]=x;
      }
    }
  }
  return a;
}


vector<vector<int>> f(int n){
  vector<vector<int>> a(n);
  for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) a[i].push_back(-1);

  vector<vector<int>> b(n);
  for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) b[i].push_back(0);

  int m;
  m=n/4;
  for(int i=0; i<m; ++i){
    for(int j=0; j<m; ++j){
      for(int k=0; k<4; ++k){
        for(int l=0; l<4; ++l){
          int klp;
          klp=k*4+l+1;
          if(klp==1 || klp==4 || klp==6 || klp==7 || klp==10 || klp==11 || klp==13 || klp==16){
            b[4*i+k][4*j+l]=1;
          }
        }
      }
    }
  }

  int x;
  x=0;
  for(int i=0; i<n; ++i){
    for(int j=0; j<n; ++j){
      x++;
      if(b[i][j]==1) a[i][j]=x;
    }
  }

  int y;
  y=0;
  for(int i=n-1; i>=0; --i){
    for(int j=n-1; j>=0; --j){
      y++;
      if(b[i][j]==0) a[i][j]=y;
    }
  }
  return a;
}


vector<vector<int>> fp(int n){
  vector<vector<int>> a(n);
  for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) a[i].push_back(-1);

  int nn;
  nn=n/2;
  vector<vector<int>> b;
  b=o(nn);

  for(int i=0; i<nn; ++i){
    for(int j=0; j<nn; ++j){
      int x;
      x=b[i][j];
      x-=1;
      x*=4;
      b[i][j]=x;
    }
  }

  vector<vector<char>> c(nn);
  for(int i=0; i<nn; ++i) c[i].resize(nn);
  for(int i=0; i<nn/2+1; ++i) for(int j=0; j<nn; ++j) c[i][j]='L';
  for(int j=0; j<nn; ++j) c[nn/2+1][j]='U';
  for(int i=nn-1; i>nn/2+1; --i) for(int j=0; j<nn; ++j) c[i][j]='X';

  char t;
  t=c[nn/2][nn/2];
  c[nn/2][nn/2]=c[nn/2+1][nn/2];
  c[nn/2+1][nn/2]=t;

  int al[2][2]={{4,1},{2,3}};
  int au[2][2]={{1,4},{2,3}};
  int ax[2][2]={{1,4},{3,2}};

  for(int i=0; i<nn; ++i){
    for(int j=0; j<nn; ++j){
      char ch;
      ch=c[i][j];
      if(ch=='L'){
        for(int k=0; k<2; ++k){
          for(int l=0; l<2; ++l){
            int x;
            x=b[i][j];
            int y;
            y=al[k][l];
            int z;
            z=x+y;
            a[i*2+k][j*2+l]=z;
          }
        }
      } else if(ch=='U'){
        for(int k=0; k<2; ++k){
          for(int l=0; l<2; ++l){
            int x;
            x=b[i][j];
            int y;
            y=au[k][l];
            int z;
            z=x+y;
            a[i*2+k][j*2+l]=z;
          }
        }
      } else if(ch=='X'){
        for(int k=0; k<2; ++k){
          for(int l=0; l<2; ++l){
            int x;
            x=b[i][j];
            int y;
            y=ax[k][l];
            int z;
            z=x+y;
            a[i*2+k][j*2+l]=z;
          }
        }
      }
    }
  }

  return a;
}


int main(){
  int n;
  cin>>n;
  vector<vector<int>> m;
  if(n%2==1) m=o(n);
  else if(n%4==0) m=f(n);
  else if(n%4==2) m=fp(n);

  for(int i=0; i<m.size(); ++i){
    for(int j=0; j<m[i].size(); ++j){
      if(j==0) cout<<m[i][j];
      else cout<<" "<<m[i][j];
    }
    cout<<endl;
  }
  return 0;
}
0