結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー krotonkroton
提出日時 2014-12-30 21:54:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 888 bytes
コンパイル時間 1,703 ms
コンパイル使用メモリ 144,300 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-03 20:18:29
合計ジャッジ時間 2,802 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

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

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

bool slide(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(now[ny][nx] != goal[zy][zx])continue;

		swap(now[ny][nx], now[zy][zx]);
		zy = ny;
		zx = nx;
		return true;
	}

	return false;
}

bool last_check(){
	for(int y=0;y<4;y++)for(int x=0;x<4;x++){
		if(now[y][x] != goal[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 >> now[y][x];
	for(int y=0;y<4;y++)for(int x=0;x<4;x++){
		if(now[y][x] == 0){
			zy = y;
			zx = x;
		}
	}

	while(slide(zy, zx));
	cout << (last_check() ? "Yes" : "No") << endl;
	return 0;
}
0