結果

問題 No.34 砂漠の行商人
ユーザー krotonkroton
提出日時 2014-10-18 21:59:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 37 ms / 5,000 ms
コード長 1,825 bytes
コンパイル時間 695 ms
コンパイル使用メモリ 84,840 KB
実行使用メモリ 8,888 KB
最終ジャッジ日時 2023-09-10 18:54:28
合計ジャッジ時間 1,962 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <sstream>
using namespace std;
typedef long long ll;

const int INF = 1 << 20;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

struct State {
    int x, y, v, d;
    bool operator>(const State& other) const {
        return d > other.d;
    }
};

map<int,int> dist[100][100];
int getDist(int x, int y, int v){
    return dist[x][y].lower_bound(v)->second;
}

int N, V, Sx, Sy, Gx, Gy;
int in[110][110];

int solve(){
    if(V >= 9 * N * 2){
        return abs(Sx - Gx) + abs(Sy - Gy);
    }
    
    for(int x=0;x<N;x++)for(int y=0;y<N;y++)dist[x][y][V] = INF;
    priority_queue<State, vector<State>, greater<State> > Q;
    
    Q.push(State{Sx, Sy, V, 0});
    dist[Sx][Sy][V] = 0;
    
    while(!Q.empty()){
        State s = Q.top(); Q.pop();
        int x = s.x, y = s.y, v = s.v, d = s.d;
        
        if(x == Gx && y == Gy){
            return d;
        }
        
        for(int i=0;i<4;i++){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny < 0 || ny >= N || nx < 0 || nx >= N)continue;
            
            int nv = v - in[nx][ny];
            if(nv <= 0)continue;
            
            int nd = d + 1;
            if(nd >= getDist(nx, ny, nv))continue;
            
            Q.push(State{nx, ny, nv, nd});
            dist[nx][ny][nv] = nd;
        }
    }
    
    return -1;
}

int main(){
    cin >> N >> V >> Sx >> Sy >> Gx >> Gy;
    
    --Sx; --Sy;
    --Gx; --Gy;
    
    for(int y=0;y<N;y++)for(int x=0;x<N;x++){
        cin >> in[x][y];
    }
    
    cout << solve() << endl;
    return 0;
}
0