結果

問題 No.34 砂漠の行商人
ユーザー fantasiabaeticafantasiabaetica
提出日時 2018-09-27 21:03:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,724 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 76,680 KB
実行使用メモリ 394,264 KB
最終ジャッジ日時 2024-04-20 09:11:11
合計ジャッジ時間 2,785 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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<<27;
        }
    }
    // 移動方向
    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));
    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] << 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 (cost >= v) continue;
            // bfsを続ける
            if (bfs[next_x][next_y][next_cost] != 1<<27){
                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