結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー hanorverhanorver
提出日時 2015-10-08 11:57:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,560 bytes
コンパイル時間 2,370 ms
コンパイル使用メモリ 70,480 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 08:07:16
合計ジャッジ時間 2,308 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
#include<stack>

bool isEnd(std::vector<std::vector<int> > v) {
	if (v[3][3] != 0)return false;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if (i == 3 && j == 3) {
				return true;
			}
			if (v[i][j] != (j + 1) + i * 4) {
				return false;
			}
		}
	}
	return true;
}

int main() {
	std::vector<std::vector<int> > v(4);
	std::stack<std::pair<int, int> > s;

	for (int j = 0; j < 4; j++) {
		int num;
		for (int i = 0; i < 4; i++) {
			std::cin >> num;
			v[j].push_back(num);
			if (num == 0) {
				s.push(std::make_pair(i, j));
			}
		}
	}

	if(isEnd(v)) {
		std::cout << "Yes" << std::endl;
		return 0;
	}

	bool move[16];
	for (int i = 0; i < 16; i++) {
		move[i] = false;
	}
	move[0] = true;

	while (true) {
		int x = s.top().first;
		int y = s.top().second;
		s.pop();
		int n = (x + 1) + y * 4;
		
		if (move[n]) break;
		if (x != 0 && v[y][x - 1] == n) {
			v[y][x] = n;
			s.push(std::make_pair(x - 1, y));
			v[y][x - 1] = 0;
			move[n] = true;
		}else if(x != 3 && v[y][x + 1] == n) {
			v[y][x] = n;
			s.push(std::make_pair(x + 1, y));
			v[y][x + 1] = 0;
			move[n] = true;
		} else if (y != 0 && v[y - 1][x] == n) {
			v[y][x] = n;
			s.push(std::make_pair(x, y - 1));
			v[y - 1][x] = 0;
			move[n] = true;
		} else if (y != 3 && v[y + 1][x] == n) {
			v[y][x] = n;
			s.push(std::make_pair(x, y + 1));
			v[y + 1][x] = 0;
			move[n] = true;
		} else {
			break;
		}
	}


	if (isEnd(v)) {
		std::cout << "Yes" << std::endl;
	} else {
		std::cout << "No" << std::endl;
	}
	
	return 0;
}
0