結果

問題 No.34 砂漠の行商人
ユーザー ayame_pyayame_py
提出日時 2016-02-09 16:20:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,017 ms / 5,000 ms
コード長 1,403 bytes
コンパイル時間 1,428 ms
コンパイル使用メモリ 150,736 KB
実行使用メモリ 140,892 KB
最終ジャッジ日時 2023-09-10 19:19:52
合計ジャッジ時間 6,928 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 4 ms
5,140 KB
testcase_03 AC 2 ms
5,552 KB
testcase_04 AC 20 ms
14,980 KB
testcase_05 AC 23 ms
18,064 KB
testcase_06 AC 10 ms
12,012 KB
testcase_07 AC 39 ms
27,580 KB
testcase_08 AC 49 ms
32,388 KB
testcase_09 AC 309 ms
60,328 KB
testcase_10 AC 43 ms
26,168 KB
testcase_11 AC 741 ms
115,608 KB
testcase_12 AC 13 ms
10,616 KB
testcase_13 AC 1,017 ms
140,892 KB
testcase_14 AC 787 ms
117,548 KB
testcase_15 AC 5 ms
6,908 KB
testcase_16 AC 51 ms
19,396 KB
testcase_17 AC 3 ms
5,852 KB
testcase_18 AC 6 ms
5,956 KB
testcase_19 AC 231 ms
57,144 KB
testcase_20 AC 408 ms
80,364 KB
testcase_21 AC 5 ms
7,316 KB
testcase_22 AC 10 ms
8,916 KB
testcase_23 AC 5 ms
6,936 KB
testcase_24 AC 562 ms
95,176 KB
testcase_25 AC 34 ms
16,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(ll i=0; i<(ll)(n); i++)
#define FOR(i,n,m) for (ll i=n; i<(ll)(m); i++)
#define pb push_back
#define INF 1000000007
#define all(a) (a).begin(),(a).end()
#define per(b)
#define MAX_N 102
#define MAX_V 1785

typedef long long ll;
typedef pair<int,int> p;
typedef pair<p,int> pv;

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

int N,V,Sx,Sy,Gx,Gy;
int L[MAX_N][MAX_N];

bool canProceed(int y, int x) {return 0<=x&&x<N&&0<=y&&y<N;}


ll d[MAX_N][MAX_N][MAX_V];
void bfs(int y, int x, int v){
    queue<pv> q;
    d[y][x][v]=0;
    q.push(pv(p(y,x),v));
    while(!q.empty()){
        pv tmp = q.front();q.pop();
        y=tmp.first.first;x=tmp.first.second;v=tmp.second;
        REP(i,4){
            int ny=y+dy[i],nx=x+dx[i];
            if(!canProceed(ny, nx)) continue;
            int nv=v-L[ny][nx];
            if(nv<=0) continue;
            if(d[ny][nx][nv]) continue;
            if(ny==Gy&&nx==Gx){
                cout << d[y][x][v]+1 << endl;
                return;
            }
            d[ny][nx][nv] = d[y][x][v] + 1;
            q.push(pv(p(ny,nx),nv));
        }
    }
    
    cout << -1 << endl;
    return;
}

int main(){
    ios::sync_with_stdio(false);
    cin >> N >> V >> Sx >> Sy >> Gx >> Gy;
    V=min(V,MAX_V-1);
    Sx--;Sy--;Gx--;Gy--;
    REP(i,N)REP(j,N) cin >> L[i][j];
    bfs(Sy,Sx,V);
    return 0;
}
0