結果
| 問題 |
No.228 ゆきこちゃんの 15 パズル
|
| コンテスト | |
| ユーザー |
hanorver
|
| 提出日時 | 2015-10-08 11:50:30 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,477 bytes |
| コンパイル時間 | 732 ms |
| コンパイル使用メモリ | 70,484 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-20 02:13:18 |
| 合計ジャッジ時間 | 1,544 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 9 WA * 8 |
ソースコード
#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));
move[n] = true;
}else if(x != 3 && v[y][x + 1] == n) {
v[y][x] = n;
s.push(std::make_pair(x + 1, y));
move[n] = true;
} else if (y != 0 && v[y - 1][x] == n) {
v[y][x] = n;
s.push(std::make_pair(x, y - 1));
move[n] = true;
} else if (y != 3 && v[y + 1][x] == n) {
v[y][x] = n;
s.push(std::make_pair(x, y + 1));
move[n] = true;
} else {
break;
}
}
if (isEnd(v)) {
std::cout << "Yes" << std::endl;
} else {
std::cout << "No" << std::endl;
}
return 0;
}
hanorver