結果
| 問題 | No.228 ゆきこちゃんの 15 パズル | 
| コンテスト | |
| ユーザー |  hanorver | 
| 提出日時 | 2015-10-08 11:57:46 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 1,560 bytes | 
| コンパイル時間 | 717 ms | 
| コンパイル使用メモリ | 70,376 KB | 
| 実行使用メモリ | 6,948 KB | 
| 最終ジャッジ日時 | 2024-07-20 02:14:20 | 
| 合計ジャッジ時間 | 1,442 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 17 | 
ソースコード
#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;
}
            
            
            
        