結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 2 ms
4,356 KB
testcase_19 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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