結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー tkmst201tkmst201
提出日時 2017-05-06 11:31:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,473 bytes
コンパイル時間 2,819 ms
コンパイル使用メモリ 153,344 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-12 14:59:42
合計ジャッジ時間 2,585 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-10;

int main() {
	vector<int> in;
	REP(i, 16) {
		int a;
		scanf("%d", &a);
		if (a == 0) in.push_back(15);
		else in.push_back(a - 1);
	}
	
	vector<int> tmp;
	REP(i, 16) tmp.push_back(i);
	
	queue<pair<vector<int>, int> > que;
	
	que.push(make_pair(tmp, 15));
	bool ans = false;
	
	while (!que.empty()) {
		vector<int> now = que.front().first;
		int pos = que.front().second;
		que.pop();
		
		
		if (now == in) ans = true;
		
		int x = pos % 4, y = pos / 4;
		
		int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
		
		REP(i, 4) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (!(nx >= 0 && nx < 4 && ny >= 0 && ny < 4)) continue;
			
			int npos = ny * 4 + nx;
			
			int tx = now[npos] % 4, ty = now[npos] / 4;
			if (abs(tx - nx) + abs(ty - ny) == 1) continue;
			
			swap(now[pos], now[npos]);
			
			que.push(make_pair(now, npos));
			
			swap(now[pos], now[npos]);
		}
	}
	
	puts(ans ? "Yes" : "No");
	
	return 0;
}
0