結果

問題 No.34 砂漠の行商人
ユーザー PachicobuePachicobue
提出日時 2017-07-03 03:27:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 5,000 ms
コード長 2,363 bytes
コンパイル時間 2,157 ms
コンパイル使用メモリ 168,968 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 19:29:45
合計ジャッジ時間 3,239 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cout << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}

constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

constexpr int dir[5] = {0, 1, 0, -1, 0};

constexpr int MAX = 100;
int N, V;
int field[MAX][MAX];
int checked[MAX][MAX];
int s, g;

int encode(int x, int y)
{
    return x * N + y;
}

pii decode(int code)
{
    return make_pair(code / N, code % N);
}

struct Info {
    Info() = default;
    Info(int p, int d, int h) : pos{p}, day{d}, hp{h} {}
    int pos;
    int day;
    int hp;
    void print() const
    {
        cout << "pos: " << pos << "\nday: " << day << "\nhp: " << hp << endl;
    }
};

int solve()
{
    queue<Info> q;
    q.push(Info{s, 0, V});
    while (not q.empty()) {
        const auto p = q.front();
        q.pop();
        if (g == p.pos) {
            return p.day;
        }
        const pii pos = decode(p.pos);
        const int x = pos.first;
        const int y = pos.second;
        const int d = p.day;
        const int hp = p.hp;
        for (int di = 0; di < 4; di++) {
            const int newx = x + dir[di];
            const int newy = y + dir[di + 1];
            if (0 <= newx and newx < N and 0 <= newy and newy < N) {
                if (hp > field[newx][newy] and checked[newx][newy] < hp - field[newx][newy]) {
                    checked[newx][newy] = hp - field[newx][newy];
                    q.push(Info{encode(newx, newy), d + 1, hp - field[newx][newy]});
                }
            }
        }
    }
    return -1;
}
int main()
{
    cin >> N >> V;
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    sx--, sy--, gx--, gy--;
    s = encode(sx, sy);
    g = encode(gx, gy);
    for (ll y = 0; y < N; y++) {
        for (ll x = 0; x < N; x++) {
            cin >> field[x][y];
            checked[x][y] = -1;
        }
    }

    cout << solve() << endl;

    return 0;
}
0