結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー IL_mstaIL_msta
提出日時 2015-06-20 03:57:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,588 bytes
コンパイル時間 1,439 ms
コンパイル使用メモリ 81,416 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 10:32:32
合計ジャッジ時間 1,700 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>

#include <algorithm>
#include <cmath>

#include <string>
#include <list>
#include <queue>
#include <vector>
#include <complex>

/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) std::cout<<(p)<<std::endl;
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////
struct ban{
	int n[4][4];
	bool b[16];
	int wh;
};
int A[16];
bool sol(ban b){
	int ans = 0;
	rep(i,16){
		if(A[i] == b.n[i/4][i%4]){
			++ans;
		}
	}
	if(ans == 16){
		return true;
	}
	return false;
}
int main(void){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    //cout << setprecision(6);//
	int aaa;
	rep(i,16){
		cin>>aaa;
		A[i] = (aaa+15)%16;
	}

	ban b,temp,cop;
	rep(i,16){
		b.n[i/4][i%4] = i;
		b.b[i] = true;
	}
	b.wh=15;

	if(sol(b) == true){
		P("Yes");
		return 0;
	}

	queue<ban> qu;
	qu.push(b);
	int dx[4] = {  1,  0, -1,  0};
	int dy[4] = {  0,  1,  0, -1};
	
	int nx,ny;

	while(!qu.empty()){
		cop = qu.front();
		qu.pop();
		for(int i=0;i<4;++i){
			temp = cop;
			nx = (temp.wh)%4 + dx[i];
			ny = (temp.wh)/4 + dy[i];
			if( 0 <= nx && nx < 4 &&
				0 <= ny && ny < 4 &&
				temp.b[ temp.n[ ny ][ nx ] ] == true
				){
				temp.b[ temp.n[ ny ][ nx ] ] = false;
				temp.n[ (temp.wh)/4 ][ (temp.wh)%4 ] = temp.n[ ny ][ nx ];
				temp.n[ ny ][ nx ] = 15;
				temp.wh = (ny)*4 + nx;
				if( sol(temp) == true){
					P("Yes");
					return 0;
				}
				qu.push(temp);
			}
		}

	}
	P("No");
	return 0;
}
0