結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー 184184
提出日時 2015-06-19 23:13:33
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 808 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 39,668 KB
実行使用メモリ 4,512 KB
最終ジャッジ日時 2023-09-21 10:10:18
合計ジャッジ時間 1,292 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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(y,x))printf("YES\n");
     ~~~^~~~~
main.cpp:31:8: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;

//namaega184

int a[4][4],b[4][4],used[16];
int dx[4]={1,0,-1,0},dy[4]={0,-1,0,1};
int dfs(int y,int x){
	int ans=0;
	int dis=0;
	for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(a[i][j]!=b[i][j])dis++;
	if(!dis)return 1;
	if(!ans)for(int i=0;i<4;i++){
		if(y+dy[i]<0||y+dy[i]>3||x+dx[i]<0||x+dx[i]>3||used[a[y+dy[i]][x+dx[i]]])continue;
		used[a[y+dy[i]][x+dx[i]]]=1;
		swap(a[y][x],a[y+dy[i]][x+dx[i]]);
		ans+=dfs(y+dy[i],x+dx[i]);
		swap(a[y][x],a[y+dy[i]][x+dx[i]]);
		used[a[y+dy[i]][x+dx[i]]]=0;
	}
	return ans;
}
int main(){
	int x,y,cnt=0;
	for(int i=0;i<4;i++)for(int j=0;j<4;j++){
		scanf("%d",&a[i][j]);if(a[i][j]==0)x=j,y=i;b[i][j]=(i*4+j+1)%16;
	} 
	if(dfs(y,x))printf("YES\n");
	else printf("NO\n");
	
	
	
	return 0;
}
0