結果
問題 | No.34 砂漠の行商人 |
ユーザー |
![]() |
提出日時 | 2016-02-09 16:20:23 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 988 ms / 5,000 ms |
コード長 | 1,403 bytes |
コンパイル時間 | 1,426 ms |
コンパイル使用メモリ | 166,548 KB |
実行使用メモリ | 139,904 KB |
最終ジャッジ日時 | 2024-06-28 10:27:53 |
合計ジャッジ時間 | 6,490 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#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; }