結果
問題 | No.34 砂漠の行商人 |
ユーザー |
![]() |
提出日時 | 2014-10-06 17:00:42 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 1,000 ms / 5,000 ms |
コード長 | 1,365 bytes |
コンパイル時間 | 1,551 ms |
コンパイル使用メモリ | 89,596 KB |
実行使用メモリ | 7,592 KB |
最終ジャッジ日時 | 2024-06-28 10:03:35 |
合計ジャッジ時間 | 4,815 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <iostream> #include <algorithm> #include <cstdio> #include <vector> #include <map> #include <queue> #include <array> using namespace std; vector<vector<int>> g; int N, V, SX, SY, GX, GY; int vxfs(int nv){ int dx[] = { 0, 1, 0, -1 }, dy[] = { 1, 0, -1, 0 }; int ct=0; queue< array<int, 4> > que; que.push({ SY, SX, nv, 0 }); vector<vector<array<int,2>>> usd(N, vector<array<int,2>>(N,{0,99999})); map<array<int, 3>, int> mp; mp[{SY,SX,0}]=nv; while(!que.empty()){ auto c = que.front(); int cy = c[0], cx = c[1], cv=c[2], cc = c[3]; que.pop(); if(cy == GY && cx == GX){ //printf("mp=%d:ct=%d\n",mp.size(),ct); return cc; } for(int i=0;i<4;++i){ ct++; int ny = cy + dy[i], nx = cx + dx[i]; if(ny<0 || ny >= N || nx<0 || nx >= N) continue; int ne = cv - g[ny][nx]; if(ne < 1) continue; if(mp[{ny,nx, cc+1}] >= ne) continue; if( usd[ny][nx][0] < ne || usd[ny][nx][1] > cc+1){ usd[ny][nx] = {ne, cc+1}; mp[{ny,nx,cc+1}] = ne; que.push({ ny, nx, ne, cc + 1 }); } } } //printf("mp=%d:ct=%d\n",mp.size(),ct); return -1; } int main() { cin >> N >> V >> SX >> SY >> GX >> GY; SX--; SY--; GX--; GY--; g.assign(N, vector<int>(N)); for(int i=0;i<N;++i) for(int j=0;j<N;++j) cin >> g[i][j]; cout << vxfs(V) << endl; return 0; }