結果
問題 | No.34 砂漠の行商人 |
ユーザー |
![]() |
提出日時 | 2015-07-22 09:36:53 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 856 ms / 5,000 ms |
コード長 | 1,113 bytes |
コンパイル時間 | 1,363 ms |
コンパイル使用メモリ | 165,600 KB |
実行使用メモリ | 75,136 KB |
最終ジャッジ日時 | 2024-06-28 10:18:43 |
合計ジャッジ時間 | 6,493 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; int dp[100][100][1800]; int mx[]={1,-1,0,0},my[]={0,0,1,-1}; int n; bool check(int x,int y){ if(0>x||n<=x) return false; if(0>y||n<=y) return false; return true; } int main(){ int v,sx,sy,gx,gy; cin>>n>>v>>sx>>sy>>gx>>gy; sx--; sy--; gx--; gy--; int data[n][n]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++) cin>>data[i][j]; } for(int i=0;i<100;i++){ for(int j=0;j<100;j++){ for(int k=0;k<1800;k++) dp[i][j][k]=1<<30; } } dp[sy][sx][0]=0; queue<tuple<int,int,int> > q; q.push(make_tuple(sx,sy,0)); while(!q.empty()){ auto now=q.front(); q.pop(); int nx=get<0>(now),ny=get<1>(now),cost=get<2>(now); if(nx==gx&&ny==gy){ cout<<dp[ny][nx][cost]<<endl; return 0; } for(int i=0;i<4;i++){ if(check(nx+mx[i],ny+my[i])){ if(cost+data[ny+my[i]][nx+mx[i]]>=v) continue; int nex=nx+mx[i],ney=ny+my[i]; if(dp[ney][nex][cost+data[ney][nex]]>dp[ny][nx][cost]+1){ dp[ney][nex][cost+data[ney][nex]]=dp[ny][nx][cost]+1; q.push(make_tuple(nex,ney,cost+data[ney][nex])); } } } } cout<<-1<<endl; return 0; }