結果

問題 No.2673 A present from B
ユーザー 👑 nu50218nu50218
提出日時 2024-03-13 21:06:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 2,615 bytes
コンパイル時間 936 ms
コンパイル使用メモリ 88,336 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-13 21:54:34
合計ジャッジ時間 1,951 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 8 ms
6,676 KB
testcase_07 AC 8 ms
6,676 KB
testcase_08 AC 7 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 3 ms
6,676 KB
testcase_13 AC 3 ms
6,676 KB
testcase_14 AC 5 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 3 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 4 ms
6,676 KB
testcase_21 AC 6 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
                      << " " << __FILE__ << endl;

int main() {
    int w, h;
    cin >> w >> h;
    vector<int> a(h);
    for (int &x : a) cin >> x;

    // アリスが 席 p のプレゼントを受け取ったと仮定する。
    // a を逆にして a に従って最短経路を求めることは、アリスが受け取ったプレゼントを席 p に戻すことに対応
    // 受け取る最短経路と、戻す最短経路は一致(自明)なので、これは正しい
    reverse(a.begin(), a.end());

    vector<vector<int> > dist(h + 3, vector<int>(w, 1e9));

    dist[0][0] = 0;  // 最初は 0

    deque<int> q;
    q.push_front(0 * w + 0);

    while (!q.empty()) {
        int now = q.front();
        q.pop_front();
        int x = now % w;
        int y = now / w;
        if (y >= h + 2) continue;

        pair<int, int> freeswap = {-1, -1};
        if (1 <= y && y <= h) freeswap = {a[y - 1] - 1, a[y - 1]};

        if (y == 0 || y == h + 1) {
            if (dist[y + 1][x] > dist[y][x]) {
                dist[y + 1][x] = dist[y][x];
                q.push_front((y + 1) * w + x);
            }
        }

        if (x + 1 < w) {
            if (freeswap.second == x + 1) {
                if (dist[y + 1][x + 1] > dist[y][x]) {
                    dist[y + 1][x + 1] = dist[y][x];
                    q.push_front((y + 1) * w + x + 1);
                }
            } else {
                if (dist[y][x + 1] > dist[y][x] + 1) {
                    dist[y][x + 1] = dist[y][x] + 1;
                    q.push_back((y)*w + x + 1);
                }
            }
        }
        if (x > 0) {
            if (freeswap.first == x - 1) {
                if (dist[y + 1][x - 1] > dist[y][x]) {
                    dist[y + 1][x - 1] = dist[y][x];
                    q.push_front((y + 1) * w + x - 1);
                }
            } else {
                if (dist[y][x - 1] > dist[y][x] + 1) {
                    dist[y][x - 1] = dist[y][x] + 1;
                    q.push_back((y)*w + x - 1);
                }
            }
        }
        if (freeswap.first != x && freeswap.second != x) {
            if (dist[y + 1][x] > dist[y][x]) {
                dist[y + 1][x] = dist[y][x];
                q.push_front((y + 1) * w + x);
            }
        }
    }
    for (int x = 1; x < w; x++) {
        cout << dist[h + 2][x];
        if (x != w - 1) cout << " ";
    }
    cout << endl;

    return 0;
}
0