結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー kongarishisyamokongarishisyamo
提出日時 2016-02-28 04:20:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,034 bytes
コンパイル時間 420 ms
コンパイル使用メモリ 58,792 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 18:43:16
合計ジャッジ時間 1,169 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>

using namespace std;

int main(){

	int a[4][4];
	bool f[16];

	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			cin>>a[i][j];
		}
	}
	for(int i=1;i<=15;i++) f[i]=true;

	while(1){
		bool fm=true;
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				//cout<<a[i][j]<<",";
				if(i==3&&j==3){
					if(a[i][j]!=0) fm=false;
					break;
				}
				if(a[i][j]!=i*4+j+1) fm=false;
			}
			//cout<<endl;
		}
		if(fm){
			cout<<"Yes"<<endl;
			break;
		}
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				if(a[i][j]==0){
					int dx[]={0,0,1,-1};
					int dy[]={1,-1,0,0};
					for(int k=0;k<4;k++){
						if(j+dx[k]>=0&&j+dx[k]<4&&i+dy[k]>=0&&i+dy[k]<4&&a[i+dy[k]][j+dx[k]]==i*4+j+1&&f[a[i+dy[k]][j+dx[k]]]){
							f[a[i+dy[k]][j+dx[k]]]=false;
							swap(a[i][j],a[i+dy[k]][j+dx[k]]);
							i=3;
							j=3;
							k=3;
							//cout<<k<<","<<i<<","<<j<<","<<i+dy[k]<<","<<j+dx[k]<<endl;
						}
						else if(k==3){
							cout<<"No"<<endl;
							return 0;
						}
					}
				}
			}
		}
	}

}
0