結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー HAHAHAHAHAHA
提出日時 2016-09-20 12:57:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 826 bytes
コンパイル時間 1,256 ms
コンパイル使用メモリ 144,548 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 23:27:58
合計ジャッジ時間 2,455 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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


int G[4][4]={
	{1,   2,  3,  4},
	{5,   6,  7,  8},
	{9,  10, 11, 12},
	{13, 14, 15,  0}
};

int S[4][4];

int dy[]={0, 0, 1, -1};
int dx[]={1,-1, 0,  0};

bool F(int &zy, int &zx){
	for(int k=0;k<4;k++){
		int ny=zy+dy[k];
		int nx=zx+dx[k];
		
		if(ny<0 || nx<0 || ny>=4 || nx>=4) continue;
		if(S[ny][nx] != G[zy][zx]) continue;
		
		swap(S[ny][nx], S[zy][zx]);
		zy=ny;
		zx=nx;
		return true;
	}
	
	return false;
}


bool F2(){
	for(int y=0;y<4;y++) for(int x=0;x<4;x++){
		if(S[y][x] != G[y][x]) return false;
	}
	return true;
}

int main(){
	int zy,zx;
	for(int y=0;y<4;y++) for(int x=0;x<4;x++) cin >> S[y][x];
	for(int y=0;y<4;y++) for(int x=0;x<4;x++){
		if(S[y][x]==0){
			zy=y;
			zx=x;
		}
	}
	
	while(F(zy,zx));
	cout << (F2() ? "Yes":"No") << endl;
	
	return 0;
}
0