結果
| 問題 |
No.217 魔方陣を作ろう
|
| コンテスト | |
| ユーザー |
otamay6
|
| 提出日時 | 2019-11-29 18:06:43 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,226 bytes |
| コンパイル時間 | 1,774 ms |
| コンパイル使用メモリ | 178,772 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-11-20 21:23:50 |
| 合計ジャッジ時間 | 2,933 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 5 WA * 13 |
ソースコード
#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++;
row%=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++;
row%=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];
}
}
otamay6