結果
| 問題 |
No.283 スライドパズルと魔方陣
|
| コンテスト | |
| ユーザー |
sigma425
|
| 提出日時 | 2017-09-15 03:04:39 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,545 bytes |
| コンパイル時間 | 1,951 ms |
| コンパイル使用メモリ | 166,800 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-26 06:37:47 |
| 合計ジャッジ時間 | 5,389 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 2 WA * 37 |
ソースコード
/*
なにこれは
*/
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
bool ok(vector<vector<int>> a,vector<vector<int>> b){
int N = a.size();
int c = 0;
vector<int> aa;
rep(i,N) rep(j,N) aa.pb(a[i][j]);
rep(j,N*N) rep(i,j) if(aa[i]>aa[j]) c++;
vector<int> bb;
rep(i,N) rep(j,N) bb.pb(b[i][j]);
rep(j,N*N) rep(i,j) if(bb[i]>bb[j]) c++;
rep(i,N) rep(j,N){
if(a[i][j] == N*N) c += i+j;
if(b[i][j] == N*N) c += i+j;
}
return c%2==0;
}
int main(){
int N;
cin>>N;
vector<vector<int>> a(N,vector<int>(N));
vector<vector<int>> b(N,vector<int>(N));
rep(i,N) rep(j,N){
cin>>a[i][j];
if(a[i][j] == 0) a[i][j] = N*N;
}
if(N==1){
puts("0");
return 0;
}
if(N==2){
puts("impossible");
return 0;
}
if(N%2==1){
int x = 0, y = N/2;
rep(t,N*N){
b[x][y] = t+1;
if(t%N != N-1){
x = (x+N-1)%N;
y = (y+1)%N;
}else{
x = (x+1)%N;
}
}
if(!ok(a,b)){
swap(b[0],b[N-1]);
}
}else{
if(N%4==0){
rep(i,N) rep(j,N){
bool d = (i%4==j%4) || (i%4+j%4==3);
if(d) b[i][j] = i*N+j+1;
else b[i][j] = N*N+1 - (i*N+j+1);
}
}else{
int n = N/2;
vector<vector<int>> c(n,vector<int>(n));
int x = 0, y = n/2;
rep(t,n*n){
c[x][y] = t*4;
if(t%n != n-1){
x = (x+n-1)%n;
y = (y+1)%n;
}else{
x = (x+1)%n;
}
}
rep(i,n) rep(j,n){
bool L = (i<n-2)^(i==n-1&&j==n/2);
bool U = (i<n-1) && !L;
bool X = !L && !U;
if(L){
b[i*2][j*2] = c[i][j]+4;
b[i*2][j*2+1] = c[i][j]+1;
b[i*2+1][j*2] = c[i][j]+2;
b[i*2+1][j*2+1] = c[i][j]+3;
}
if(U){
b[i*2][j*2] = c[i][j]+1;
b[i*2][j*2+1] = c[i][j]+4;
b[i*2+1][j*2] = c[i][j]+2;
b[i*2+1][j*2+1] = c[i][j]+3;
}
if(X){
b[i*2][j*2] = c[i][j]+1;
b[i*2][j*2+1] = c[i][j]+4;
b[i*2+1][j*2] = c[i][j]+3;
b[i*2+1][j*2+1] = c[i][j]+2;
}
}
}
if(!ok(a,b)){
rep(i,N/2) swap(b[i],b[N-1-i]);
}
}
puts("possible");
rep(i,N){
rep(j,N) cout<<b[i][j]<<" ";
puts("");
}
}
sigma425