結果
| 問題 |
No.34 砂漠の行商人
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2014-10-18 19:24:33 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,895 bytes |
| コンパイル時間 | 1,825 ms |
| コンパイル使用メモリ | 83,388 KB |
| 実行使用メモリ | 213,720 KB |
| 最終ジャッジ日時 | 2024-12-30 09:28:52 |
| 合計ジャッジ時間 | 84,869 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 13 TLE * 13 |
ソースコード
#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;
bool operator>(const State &other) const {
return d > other.d;
}
};
map<int, int> dist[110][110];
int N, V, Sx, Sy, Gx, Gy;
int in[110][110];
int getDist(int x, int y, int v){
map<int,int> &mp = dist[x][y];
if(mp.count(v)){
return mp[v];
} else {
return INF;
}
}
int solve(){
dist[Sx][Sy][V] = 0;
priority_queue< State, vector<State>, greater<State> > Q;
Q.push(State{Sx, Sy, V, 0});
int res = INF;
while(!Q.empty()){
State s = Q.top(); Q.pop();
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(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;
}