結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー fiordfiord
提出日時 2015-11-19 20:58:57
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 784 bytes
コンパイル時間 1,222 ms
コンパイル使用メモリ 151,372 KB
実行使用メモリ 4,536 KB
最終ジャッジ日時 2023-10-11 18:06:24
合計ジャッジ時間 2,292 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:31:8: warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  if(dfs(a,x,y)) cout<<"YES"<<endl;
     ~~~^~~~~~~
main.cpp:31:8: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int visited[4][4];
vector<vector<int>> ok={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};
int mx[]={1,-1,0,0},my[]={0,0,1,-1};
bool dfs(vector<vector<int>> a,int x,int y){
	if(a==ok)	return true;
	if(visited[y][x])	return false;
	visited[y][x]=true;
	for(int i=0;i<4;i++){
		int cx=x+mx[i],cy=y+my[i];
		if(cx<0||3<cx||cy<0||3<cy)	continue;
		if(a[cy][cx]==ok[y][x]){
			swap(a[cy][cx],a[y][x]);
			if(dfs(a,cx,cy))	return true;
			swap(a[cy][cx],a[y][x]);
		}
	}
	visited[y][x]=false;
	return false;
}
int main(){
	vector<vector<int>> a(4,vector<int>(4));
	int x,y;
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			cin>>a[i][j];
			if(a[i][j]==0)	x=j,y=i;
		}
	}
	if(dfs(a,x,y))	cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}
0