結果

問題 No.34 砂漠の行商人
ユーザー krotonkroton
提出日時 2014-10-18 20:04:16
言語 C++11
(gcc 11.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,805 bytes
コンパイル時間 655 ms
コンパイル使用メモリ 75,752 KB
実行使用メモリ 480,956 KB
最終ジャッジ日時 2023-09-10 19:13:19
合計ジャッジ時間 13,305 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 185 ms
476,812 KB
testcase_01 AC 182 ms
476,704 KB
testcase_02 AC 183 ms
476,528 KB
testcase_03 AC 184 ms
476,784 KB
testcase_04 AC 191 ms
476,704 KB
testcase_05 AC 193 ms
476,856 KB
testcase_06 AC 183 ms
476,720 KB
testcase_07 AC 195 ms
476,640 KB
testcase_08 AC 201 ms
476,672 KB
testcase_09 AC 3,130 ms
477,648 KB
testcase_10 TLE -
testcase_11 AC 4,799 ms
478,092 KB
testcase_12 AC 192 ms
476,976 KB
testcase_13 AC 1,627 ms
478,428 KB
testcase_14 AC 1,616 ms
478,388 KB
testcase_15 AC 638 ms
476,964 KB
testcase_16 AC 1,090 ms
477,020 KB
testcase_17 AC 510 ms
477,544 KB
testcase_18 AC 244 ms
476,612 KB
testcase_19 AC 366 ms
478,124 KB
testcase_20 AC 551 ms
478,236 KB
testcase_21 AC 186 ms
476,868 KB
testcase_22 AC 613 ms
477,848 KB
testcase_23 AC 1,594 ms
477,180 KB
testcase_24 AC 3,132 ms
478,212 KB
testcase_25 AC 216 ms
477,112 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;
};

int dist[110][110][10010];

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

int getDist(int x, int y, int v){
    int d = dist[x][y][v];
    
    if(d != -1){
        return d;
    } else {
        return INF;
    }
}

int solve(){
    memset(dist, -1, sizeof(dist));
    dist[Sx][Sy][V] = 0;
    
    deque<State> Q;
    Q.push_back(State{Sx, Sy, V, 0});
    
    int res = INF;
    while(!Q.empty()){
        State s = Q.front(); Q.pop_front();
        int x = s.x, y = s.y, v = s.v, d = s.d;
        
        if(x == Gx && y == Gy){
            res = min(res, d);
            continue;
        }
        
        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;
            
            dist[nx][ny][nv] = nd;
            Q.push_back(State{nx, ny, nv, nd});
        }
    }
    
    if(res >= INF){
        return -1;
    } else {
        return res;
    }
}

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