結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー minamiminami
提出日時 2019-04-05 02:10:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,475 bytes
コンパイル時間 1,409 ms
コンパイル使用メモリ 167,124 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 15:24:32
合計ジャッジ時間 2,476 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

// n*nパズルが解けるかどうか
// 転倒数は O(m^2) で求めている
// 空きは m - 1 で表現する
bool solvable(const vector<int> &v, int n) {
	int m = v.size();
	int cnt = 0;
	for (int i = 0; i < m; i++) {
		for (int j = i + 1; j < m; j++) {
			if (v[i] == m - 1 || v[j] == m - 1)continue;
			if (v[i] > v[j])
				cnt++;
		}
	}
	int s = find(v.begin(), v.end(), m - 1) - v.begin();
	int si = s / n, sj = s % n;
	return cnt % 2 == (n - 1 - si + n - 1 - sj) % 2;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	vector<int> v(16);
	rep(i, 0, 16) {
		cin >> v[i];
		v[i]--;
		if (v[i] < 0)v[i] = 15;
	}
	bool yes = solvable(v, 4);
	rep(i, 0, 4)rep(j, 0, 4) {
		int ni = v[i * 4 + j] / 4, nj = v[i * 4 + j] % 4;
		int d = abs(i - ni) + abs(j - nj);
		if (d > 1)
			yes = false;
	}
	if (yes) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	return 0;
}
0