結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー MisterMister
提出日時 2020-04-13 10:51:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,259 bytes
コンパイル時間 934 ms
コンパイル使用メモリ 93,508 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 20:06:56
合計ジャッジ時間 1,717 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void solve()':
main.cpp:35:43: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
   35 |         if (std::abs(x - sx) + std::abs(y - sy) > 1) {
      |                                         ~~^~~~
main.cpp:7:13: note: 'sy' was declared here
    7 |     int sx, sy;
      |             ^~
main.cpp:35:24: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
   35 |         if (std::abs(x - sx) + std::abs(y - sy) > 1) {
      |                      ~~^~~~
main.cpp:7:9: note: 'sx' was declared here
    7 |     int sx, sy;
      |         ^~

ソースコード

diff #

#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;
}
0