結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー koyumeishikoyumeishi
提出日時 2015-06-19 22:51:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,375 bytes
コンパイル時間 658 ms
コンパイル使用メモリ 82,640 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 10:02:33
合計ジャッジ時間 1,561 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

vector<vector<int>> goal = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};

int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int main(){
	vector<vector<int>> v(4, vector<int>(4));
	for(int i=0; i<4; i++){
		for(int j=0; j<4; j++){
			cin >> v[i][j];
		}
	}

	int zero = -1;
	auto x = v;
	vector<vector<int>> e(16);
	bool ok = true;
	for(int i=0; i<4; i++){
		for(int j=0; j<4; j++){
			if(v[i][j] == 0){
				zero = i*4 + j;
			}
			if(i==3 && j == 3) continue;
			if(v[i][j] == goal[i][j]) continue;
			bool tmp = false;
			for(int k=0; k<4; k++){
				int yy = i+dy[k];
				int xx = j+dx[k];
				if(xx<0 || xx>=4 || yy<0 || yy>=4) continue;
				if(v[yy][xx] == goal[i][j]){
					e[i*4 + j].push_back(yy*4 + xx);
					tmp = true;
				}
			}
			if(tmp == false){
				ok = false;
			}
		}
	}

	for(int i=0; i<16; i++){
		if(e[i].size() > 1) ok = false;
	}

	if(ok == false){
		cout << "No" << endl;
		return 0;
	}

	int pos = zero;
	while(pos != 15){
		if(e[pos].size() == 0) break;
		int next = e[pos][0];
		swap(v[pos/4][pos%4], v[next/4][next%4]);
		pos = next;
	}

	if(v == goal){
		cout << "Yes" << endl;
	}else{
		cout << "No" << endl;
	}



	return 0;
}
0