結果

問題 No.34 砂漠の行商人
ユーザー なおなお
提出日時 2014-10-06 01:13:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,605 bytes
コンパイル時間 477 ms
コンパイル使用メモリ 67,660 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 18:53:07
合計ジャッジ時間 1,604 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// 想定解法: 格子点平面上の最短経路探索(Dijkstra, SPFA, etc.)
//           各位置での累計移動距離ごとの必要コスト最小化
//           もっといい解がありそうな気がするのでつよい人に期待

#include <cstdio>
#include <iostream>
#include <deque>
#include <map>
using namespace std;
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))

const int MAXN = 100;

int board[MAXN][MAXN];
int cost[MAXN][MAXN];
const int INF = 1<<29;
const int dir[][2] = {{1,0},{0,1},{-1,0},{0,-1}};

int N, V, SX, SY, GX, GY;

int solve(){
    REP(y,N) REP(x,N) cost[y][x] = INF;

    deque<pair<pair<int,int>,int> > q;
    q.push_back(make_pair(make_pair(SX,SY),0));
    cost[SY][SX] = 0;

    while(!q.empty()){
        auto &v = q.front();
        int x = v.first.first, y = v.first.second, s = v.second;
        q.pop_front();
        int nowcost = cost[y][x];
        if(x == GX && y == GY) return s;

        for(int d = 0; d < 4; d++){
            int mx = x + dir[d][0];
            int my = y + dir[d][1];
            if(mx < 0 || my < 0 || mx >= N || my >= N) continue;
            int nextcost = nowcost + board[my][mx];
            if(cost[my][mx] > nextcost && nextcost < V){
                cost[my][mx]= nextcost;
                q.push_back(make_pair(make_pair(mx,my),s+1));
            }
        }
    }
    return -1;
}

int main(){
    cin >> N >> V >> SX >> SY >> GX >> GY;
    REP(y,N) REP(x,N) cin >> board[y][x];
    SX--,SY--,GX--,GY--;

    cout << solve() << endl;
    return 0;
}
0