結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー btkbtk
提出日時 2015-06-19 23:56:02
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,749 bytes
コンパイル時間 1,060 ms
コンパイル使用メモリ 113,812 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-21 10:23:20
合計ジャッジ時間 4,647 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:116:39: warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     swap(u.first[xx][yy], u.first[x][y]);
                                       ^
main.cpp:116:36: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     swap(u.first[xx][yy], u.first[x][y]);
                                    ^
main.cpp:58:19: warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   if (v.first[x][y] != 0)return 0;
                   ^
main.cpp:58:16: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   if (v.first[x][y] != 0)return 0;
                ^

ソースコード

diff #

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<utility>
#include<complex>
#include<functional>
using namespace std;


typedef vector<int> VI;
typedef vector<VI> VVI;
const int dir[] = { 0, -1, 0, 1, 0 };
typedef pair<VVI,int> P;

#define mp(a,b) make_pair(a,b)


void cp(VVI& d, VVI& s){
	for (int i = 1; i < 5; i++)
		for (int j = 1; j < 5; j++)
			d[i][j] = s[i][j];
}
int main(void){
	map<VVI,list<int>> memo;
	auto a = mp(VVI(6, VI(6, 16)), 1 << 16);
	for (int i = 1; i < 5; i++){
		for (int j = 1; j < 5; j++){
			a.first[i][j] = ((i - 1) * 4 + j)%16;
		}
	}
	queue<P> que;
	que.push(a);
	memo[a.first].push_back(a.second);
	//*
	while (que.empty() == false){
		auto v = que.front(); que.pop();
		int x, y;
		for (int i = 0; i < 5; i++)
			for (int j = 0; j < 5; j++)
				if (v.first[i][j] == 0)x = i, y = j;
		int cnt = 0;
		for (int i = 1; i < 16; i++)
			if (((v.second&(1 << i)) > 0))cnt++;
		if (cnt >= 9)
			continue;
		if (v.first[x][y] != 0)return 0;
		for (int i = 0; i < 5; i++){
			int xx = x + dir[i];
			int yy = y + dir[i + 1];
			if (v.first[xx][yy]<16&&((v.second&(1<<v.first[xx][yy])) == 0)){
				if (v.first[xx][yy] == 16)return 0;
				P u;
				u.first.resize(6);
				for (auto &it : u.first){
					it.resize(6);
					for (auto &jt : it)jt = 16;
				}
				cp(u.first, v.first);
				u.second = v.second;
				u.second |= (1 << v.first[xx][yy]);
				swap(u.first[xx][yy], u.first[x][y]);
				//if (memo.count(u.first) == 0)memo.insert(memo.begin(), mp(u.first, list<int>()));
				memo[u.first].push_back(u.second);
				que.push(u);
			}
		}
	}
	//*/
	for (int i = 1; i < 5; i++){
		for (int j = 1; j < 5; j++){
			cin >> a.first[i][j];
		}
	}
	a.second = (1 << 16);
	que.push(a);
	while (que.empty() == false){
		auto v = que.front(); que.pop();
		if (memo.count(v.first) > 0){
			for (auto &it : memo[v.first]){
				if ((v.second&it) == (1 << 16)){
					cout << "Yes" << endl;
					return 0;
				}
			}
			
		}
		int x, y;
		for (int i = 0; i < 5; i++)
			for (int j = 0; j < 5; j++)
				if (v.first[i][j] == 0)x = i, y = j;
		int cnt = 0;
		for (int i = 1; i < 16; i++)
			cnt += ((v.second&(1 << i)) > 0);
		if (cnt >= 17)
			continue;

		for (int i = 0; i < 5; i++){
			int xx = x + dir[i];
			int yy = y + dir[i + 1];
			if (v.first[xx][yy]<16 && ((v.second&(1 << v.first[xx][yy])) == 0)){
				auto u = mp(VVI(6, VI(6, 16)), v.second);
				cp(u.first, v.first);
				u.second |= (1 << v.first[xx][yy]);
				swap(u.first[xx][yy], u.first[x][y]);
				que.push(u);
			}
		}
	}
	cout << "No" << endl;
	return(0);
}
0