結果

問題 No.283 スライドパズルと魔方陣
ユーザー sigma425sigma425
提出日時 2017-09-15 03:14:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,600 bytes
コンパイル時間 1,471 ms
コンパイル使用メモリ 153,856 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-27 02:04:35
合計ジャッジ時間 7,704 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 3 ms
4,384 KB
testcase_22 AC 3 ms
4,384 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 3 ms
4,384 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 8 ms
4,384 KB
testcase_28 AC 8 ms
4,384 KB
testcase_29 AC 8 ms
4,384 KB
testcase_30 AC 8 ms
4,380 KB
testcase_31 AC 8 ms
4,384 KB
testcase_32 AC 9 ms
4,380 KB
testcase_33 AC 9 ms
4,384 KB
testcase_34 AC 8 ms
4,384 KB
testcase_35 AC 8 ms
4,380 KB
testcase_36 AC 9 ms
4,384 KB
testcase_37 AC 8 ms
4,380 KB
testcase_38 AC 8 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
イワナ
*/
#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("possible");
		puts("1");
		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);
				bool U = (i<=n/2+1) && !L;
				bool X = !L && !U;
				if((i==n/2 || i==n/2+1) && j == n/2) swap(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]<<(j==N-1?"\n":" ");
	}
}
0