結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー Mcpu3Mcpu3
提出日時 2019-09-25 16:25:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,544 bytes
コンパイル時間 1,157 ms
コンパイル使用メモリ 96,160 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 06:52:10
合計ジャッジ時間 2,036 ms
ジャッジサーバーID
(参考情報)
judge9 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

int main() {
	vector<int> a(16);
	for (int& i : a) cin >> i;
	queue<pair<unordered_set<int>, vector<int>>> b;
	b.emplace(unordered_set<int>(), a);
	while (!b.empty()) {
		int c = find(b.front().second.begin(), b.front().second.end(), 0) - b.front().second.begin(), i;
		for (i = 0; i < 15; ++i) {
			if (1 + i != b.front().second[i]) break;
		}
		if (15 == i) break;
		if (3 != c % 4) {
			if (!b.front().first.count(b.front().second[1 + c])) {
				pair<unordered_set<int>, vector<int>> d(b.front());
				swap(d.second[1 + c], d.second[c]);
				d.first.insert(d.second[c]);
				b.push(d);
			}
		}
		if (3 != c / 4) {
			if (!b.front().first.count(b.front().second[4 + c])) {
				pair<unordered_set<int>, vector<int>> d(b.front());
				swap(d.second[4 + c], d.second[c]);
				d.first.insert(d.second[c]);
				b.push(d);
			}
		}
		if (c % 4) {
			if (!b.front().first.count(b.front().second[c - 1])) {
				pair<unordered_set<int>, vector<int>> d(b.front());
				swap(d.second[c - 1], d.second[c]);
				d.first.insert(d.second[c]);
				b.push(d);
			}
		}
		if (c / 4) {
			if (!b.front().first.count(b.front().second[c - 4])) {
				pair<unordered_set<int>, vector<int>> d(b.front());
				swap(d.second[c - 4], d.second[c]);
				d.first.insert(d.second[c]);
				b.push(d);
			}
		}
		b.pop();
	}
	if (b.empty()) cout << "No";
	else cout << "Yes";
}
0