結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー data9824data9824
提出日時 2015-06-19 22:45:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,032 bytes
コンパイル時間 477 ms
コンパイル使用メモリ 65,364 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 10:01:16
合計ジャッジ時間 1,326 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int dx[] = { -1, 1, 0, 0 };
const int dy[] = { 0, 0, -1, 1 };
vector<int> a(16);
vector<bool> locked(16);

int index(int x, int y) {
	return x + 4 * y;
}

bool scan(int space) {
	bool result = true;
	for (int i = 0; i < 16 - 1; ++i) {
		if (a[i] != (i + 1)) {
			result = false;
			break;
		}
	}
	if (result) {
		return result;
	}
	int x = space % 4;
	int y = space / 4;
	for (int i = 0; i < 4; ++i) {
		if (0 <= x + dx[i] && x + dx[i] < 4 && 0 <= y + dy[i] && y + dy[i] < 4) {
			int moved = index(x + dx[i], y + dy[i]);
			if (!locked[moved]) {
				swap(a[space], a[moved]);
				locked[space] = true;
				result = scan(moved);
				locked[space] = false;
				swap(a[space], a[moved]);
				if (result) {
					return true;
				}
			}
		}
	}
	return false;
}

int main() {
	for (int i = 0; i < 16; ++i) {
		cin >> a[i];
	}
	int space = distance(a.begin(), find(a.begin(), a.end(), 0));
	cout << (scan(space) ? "Yes" : "No") << endl;
	return 0;
}
0