結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー sugim48sugim48
提出日時 2014-12-14 22:28:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 864 bytes
コンパイル時間 1,625 ms
コンパイル使用メモリ 52,352 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-02 14:30:58
合計ジャッジ時間 1,387 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <utility>
using namespace std;

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

bool slide(int& x, int& y) {
	int k = b[y][x];
	if (k == 0) return false;
	for (int i = 0; i < 4; i++) {
		int _x = x + dx[i], _y = y + dy[i];
		if (_x >= 0 && _x < 4 && _y >= 0 && _y < 4 && a[_y][_x] == k) {
			swap(a[y][x], a[_y][_x]);
			x = _x; y = _y;
			return true;
		}
	}
	return false;
}

bool check() {
	for (int y = 0; y < 4; y++)
		for (int x = 0; x < 4; x++)
			if (a[y][x] != b[y][x])
				return false;
	return true;
}

int main() {
	for (int y = 0; y < 4; y++)
		for (int x = 0; x < 4; x++)
			cin >> b[y][x];
	int x = 3, y = 3;
	while (slide(x, y));
	cout << (check() ? "Yes" : "No") << endl;
}
0