結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー komori3komori3
提出日時 2017-10-03 04:12:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,762 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 75,396 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 00:22:22
合計ジャッジ時間 1,920 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,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <climits>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <functional>
#include <set>
#include <numeric>
#include <stack>
#include <utility>
#include <time.h>
//#include "util.h"

using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;



//呪文
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const pair<_KTy, _Ty>& m) { cout << "{" << m.first << ", " << m.second << "}"; return ostr; }
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const map<_KTy, _Ty>& m) { if (m.empty()) { cout << "{ }"; return ostr;	} cout << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { cout << ", " << *itr; } cout << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const vector<_Ty>& v) { if (v.empty()) { cout << "{ }"; return ostr; }	cout << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { cout << ", " << *itr; }	cout << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const set<_Ty>& s) { if (s.empty()) { cout << "{ }"; return ostr; } cout << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { cout << ", " << *itr; }	cout << "}"; return ostr; }
#define PI 3.14159265358979323846
#define EPS 1e-6
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define all(x) (x).begin(), (x).end()


enum DIR { RIGHT = 0, UP = 1, LEFT = 2, DOWN = 3 };

struct hoge {
	int mp[16];
	int vacant;
	int moved;
};

void init(hoge& obj)
{
	for (int i = 0; i < 15; i++) obj.mp[i] = i + 1;
	obj.mp[15] = 0;
	obj.vacant = 15;
	obj.moved = 1;
}

bool movable(hoge& obj, DIR dir)
{
	int* mp = obj.mp;
	int& vacant = obj.vacant;
	int& moved = obj.moved;

	switch (dir)
	{
	case RIGHT:
		if (vacant % 4 == 3) return false;
		if (moved & (1 << mp[vacant + 1])) return false;
		return true;
		break;
	case UP:
		if (vacant < 4) return false;
		if (moved & (1 << mp[vacant - 4])) return false;
		return true;
		break;
	case LEFT:
		if (vacant % 4 == 0) return false;
		if (moved & (1 << mp[vacant - 1])) return false;
		return true;
		break;
	case DOWN:
		if (vacant >= 12) return false;
		if (moved & (1 << mp[vacant + 4])) return false;
		return true;
		break;
	default:
		break;
	}
	cout << "err : movable" << endl;
	return false;
}

hoge move(hoge obj, DIR dir)
{
	int* mp = obj.mp;
	int& vacant = obj.vacant;
	int& moved = obj.moved;

	switch (dir)
	{
	case RIGHT:
		swap(mp[vacant], mp[vacant + 1]);
		moved |= (1 << mp[vacant]);
		vacant++;
		break;
	case UP:
		swap(mp[vacant], mp[vacant - 4]);
		moved |= (1 << mp[vacant]);
		vacant -= 4;
		break;
	case LEFT:
		swap(mp[vacant], mp[vacant - 1]);
		moved |= (1 << mp[vacant]);
		vacant--;
		break;
	case DOWN:
		swap(mp[vacant], mp[vacant + 4]);
		moved |= (1 << mp[vacant]);
		vacant += 4;
		break;
	default:
		break;
	}
	
	return obj;
}

bool equal(int* mp1, int* mp2)
{
	for (int i = 0; i < 16; i++)
		if (mp1[i] != mp2[i])
			return false;
	return true;
}

int mp_target[16];
bool flag = false;

void rec(hoge obj)
{
	if (equal(obj.mp, mp_target)) flag = true;
	
	if (flag) return;

	//全部動かした
	if (obj.moved == (1 << 16) - 1) return;

	for (int dir = 0; dir < 4; dir++) {
		if (movable(obj, (DIR)dir)) {
			rec(move(obj, (DIR)dir));
		}
	}

}

int yuki0228()
{
	hoge obj;
	init(obj);

	for (int i = 0; i < 16; i++) cin >> mp_target[i];

	rec(obj);

	cout << (flag ? "Yes" : "No") << endl;

	return 0;
}

int main()
{
	//clock_t start, end;
	//start = clock();

	yuki0228();
	
	//end = clock();
	//printf("%d msec.\n", end - start);

	return 0;
}
0