結果

問題 No.34 砂漠の行商人
ユーザー fantasiabaeticafantasiabaetica
提出日時 2018-09-27 21:13:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 731 ms / 5,000 ms
コード長 1,753 bytes
コンパイル時間 925 ms
コンパイル使用メモリ 77,120 KB
実行使用メモリ 395,108 KB
最終ジャッジ日時 2023-08-02 09:46:26
合計ジャッジ時間 5,659 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 10 ms
4,812 KB
testcase_05 AC 10 ms
5,088 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 15 ms
6,088 KB
testcase_08 AC 19 ms
7,840 KB
testcase_09 AC 284 ms
229,756 KB
testcase_10 AC 211 ms
394,128 KB
testcase_11 AC 677 ms
395,108 KB
testcase_12 AC 7 ms
4,792 KB
testcase_13 AC 731 ms
102,152 KB
testcase_14 AC 534 ms
98,232 KB
testcase_15 AC 14 ms
42,704 KB
testcase_16 AC 51 ms
81,392 KB
testcase_17 AC 11 ms
30,376 KB
testcase_18 AC 6 ms
9,196 KB
testcase_19 AC 141 ms
23,692 KB
testcase_20 AC 266 ms
38,936 KB
testcase_21 AC 4 ms
5,212 KB
testcase_22 AC 17 ms
38,264 KB
testcase_23 AC 31 ms
115,428 KB
testcase_24 AC 437 ms
206,672 KB
testcase_25 AC 20 ms
9,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define P pair<int, int>
#define PP pair<P, int>

int main(){
    // 辺の長さ,体力,スタート地点の座標,ゴール地点の座標
    int n, v, sx, sy, gx, gy;
    cin >> n >> v >> sx >> sy >> gx >> gy;
    sx--;
    sy--;
    gx--;
    gy--;
    int map[n][n];
    // bfs[x][y][消費コスト] = 移動回数
    int bfs[n][n][v];
    FOR(i, 0, n){
        FOR(j, 0, n){
            FOR(k, 0, v) bfs[i][j][k] = -1;
        }
    }
    // 移動方向
    int dx[4] = {1, -1, 0, 0};
    int dy[4] = {0, 0, 1, -1};
    FOR(i, 0, n){
        FOR(j, 0, n) cin >> map[j][i];
    }
    queue<PP> q;
    q.push(make_pair(make_pair(sx, sy), 0));
    bfs[sx][sy][0] = 0;
    while(!(q.empty())){
        PP pp = q.front();
        q.pop();
        int x = pp.first.first;
        int y = pp.first.second;
        int cost = pp.second;
        int next_x, next_y, next_cost;
        // ゴール
        if (x == gx && y == gy) {
            cout << bfs[x][y][cost] << endl;
            return 0;
        }
        FOR(i, 0, 4){
            next_x = x + dx[i];
            next_y = y + dy[i];
            // はみ出る場合
            if (!(0 <= next_x && next_x < n && 0 <= next_y && next_y < n)) continue;
            // 体力切れ
            next_cost = pp.second + map[next_x][next_y];
            if (next_cost >= v) continue;
            // bfsを続ける
            if (bfs[next_x][next_y][next_cost] == -1){
                bfs[next_x][next_y][next_cost] = bfs[x][y][cost] + 1;
                q.push(make_pair(make_pair(next_x, next_y), next_cost));
            }
        }        
    }
    cout << -1 << endl;
    return 0;
}
0