結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー 158b158b
提出日時 2015-06-19 22:53:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,393 bytes
コンパイル時間 575 ms
コンパイル使用メモリ 75,632 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 10:03:16
合計ジャッジ時間 1,534 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <climits>
#include <vector>
#include <numeric>
#include <complex>
#include <map>
#include <bitset>
using namespace std;

//#define __int64 long long
#define long __int64
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
const int Vecy[4] = {0,-1,0,1};
const int Vecx[4] = {1,0,-1,0};

bool check[4][4] = {false};
int from[4][4];
int to[4][4] = {0};
	
bool func(){
	for(int i=0; i<16; i++){
		if(from[i/4][i%4] != to[i/4][i%4]){
			return false;
		}
	}
	
	return true;
}

int main(){

	bool c[4][4] = {false};
	int nowy = 3;
	int nowx = 3;
	int iy,ix;
	int veci;
	bool ans = true;
	
	for(int i=0; i<16; i++){
		from[i/4][i%4] = i + 1;
	}
	from[3][3] = 0;
	
	for(int i=0; i<16; i++){
		cin >> to[i/4][i%4];
		if(from[i/4][i%4] == to[i/4][i%4]){
			check[i/4][i%4] = true;
		}
	}
	
	while(!func()){
		for(veci=0; veci<4; veci++){
			iy = nowy + Vecy[veci];
			ix = nowx + Vecx[veci];
			if(iy >= 0 && iy<4 && ix >= 0 && ix < 4){
				if(from[iy][ix] == to[nowy][nowx]){
					break;
				}
			}
		}
		
		if(veci == 4){
			ans = false;
			break;
		}
		
		if(c[iy][ix] == true){
			ans = false;
			break;
		}
		
		c[iy][ix] = true;
		swap(from[nowy][nowx], from[iy][ix]);
		
		nowy = iy;
		nowx = ix;
	}
	
	if(ans){
		cout << "Yes" << endl;
	}else{
		cout << "No" << endl;
	}
	
    return 0;
}
0