結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー HAHAHAHAHAHA
提出日時 2016-09-20 13:08:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 883 bytes
コンパイル時間 1,386 ms
コンパイル使用メモリ 160,572 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 16:34:25
合計ジャッジ時間 2,011 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 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 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

const int n=4;
int A[4][4];
bool vis[4][4];
const int dy[]={0, 1, 0,-1};
const int dx[]={1, 0,-1, 0};


bool dfs(int i, int j){
	if(vis[i][j]) return false;
	vis[i][j]=true;
	
	bool ok=true;
	for(int k=0;k<n;k++) for(int l=0;l<n;l++){
		ok &= A[k][l] == (k*4 + l+1) %16;
	}
	if(ok) return true;
	
	for(int d=0;d<4;d++){
		int yy= i + dy[d], xx= j + dx[d];
		if(yy<0 || yy>=n || xx<0 || xx>=n) continue;
		swap(A[i][j], A[yy][xx]);
		if(dfs(yy, xx)) return true;
		swap(A[i][j], A[yy][xx]);
	}
	
	vis[i][j]=false;
	return false;
}

int main(){
	while(1){
		for(int i=0;i<n;i++) for(int j=0;j<n;j++){
			if(!(cin >> A[i][j])) return 0;
		}
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++) for(int j=0;j<n;j++){
			if(A[i][j]==0){
				bool ans=dfs(i,j);
				cout << (ans? "Yes":"No") << endl;
				goto aa;
			}
		}
		aa:;
	}
	return 0;
}
0