結果
| 問題 |
No.228 ゆきこちゃんの 15 パズル
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-13 10:51:22 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,259 bytes |
| コンパイル時間 | 1,048 ms |
| コンパイル使用メモリ | 89,972 KB |
| 最終ジャッジ日時 | 2025-01-09 17:54:24 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:29:19: warning: ‘sx’ may be used uninitialized [-Wmaybe-uninitialized]
29 | while (goal[sx][sy] != -1) {
| ^
main.cpp:7:9: note: ‘sx’ was declared here
7 | int sx, sy;
| ^~
main.cpp:29:23: warning: ‘sy’ may be used uninitialized [-Wmaybe-uninitialized]
29 | while (goal[sx][sy] != -1) {
| ^
main.cpp:7:13: note: ‘sy’ was declared here
7 | int sx, sy;
| ^~
ソースコード
#include <iostream>
#include <cmath>
#include <vector>
#include <tuple>
void solve() {
int sx, sy;
std::vector<std::pair<int, int>> pos(15);
std::vector<std::vector<int>> goal(4, std::vector<int>(4));
for (int x = 0; x < 4; ++x) {
for (int y = 0; y < 4; ++y) {
goal[x][y] = x * 4 + y;
int a;
std::cin >> a;
--a;
if (a < 0) {
sx = x;
sy = y;
} else {
pos[a] = std::make_pair(x, y);
}
}
}
goal[3][3] = -1;
while (goal[sx][sy] != -1) {
int a = goal[sx][sy];
int x, y;
std::tie(x, y) = pos[a];
if (std::abs(x - sx) + std::abs(y - sy) > 1) {
std::cout << "No" << std::endl;
return;
}
pos[a] = std::make_pair(sx, sy);
sx = x, sy = y;
}
for (int a = 0; a < 15; ++a) {
int x, y;
std::tie(x, y) = pos[a];
if (goal[x][y] != a) {
std::cout << "No" << std::endl;
return;
}
}
std::cout << "Yes" << std::endl;
return;
}
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
solve();
return 0;
}