結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  tkzw_21 | 
| 提出日時 | 2015-03-05 13:44:49 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                MLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,543 bytes | 
| コンパイル時間 | 1,598 ms | 
| コンパイル使用メモリ | 172,732 KB | 
| 実行使用メモリ | 814,624 KB | 
| 最終ジャッジ日時 | 2024-06-24 08:17:35 | 
| 合計ジャッジ時間 | 4,762 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 3 MLE * 1 -- * 22 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,pair<int,int>> PP;
typedef long long ll;
const double EPS = 1e-8;
const int INF = 1e9;
const int MOD = 1e9+7;
int dy[] = {0,1,0,-1};
int dx[] = {1,0,-1,0};
map<PP,int> dist;
int bfs(vector<vector<int>> L,int sx,int sy,int gx,int gy,int V,vector<vector<vector<int>>>& memo){
	queue<int> qx,qy,qv;
	qx.push(sx);qy.push(sy);qv.push(V);
	int ret = INF;
	memo[sy][sx][V] = 0;
	while(!qx.empty()){
		int x,y,v;
		x = qx.front();qx.pop();
		y = qy.front();qy.pop();
		v = qv.front();qv.pop();
		if(gy == y && gx == x){
			ret = min(ret,memo[gy][gx][v]);
			// return memo[gy][gx][v];
			//cout << v << endl;
			//break;
			continue;
		}
		for(int i=0;i<4;i++){
			int nx = x + dx[i],ny = y + dy[i];
			if(nx < 0 || ny < 0 || nx >= L[0].size() || ny >= L.size())continue;
			int nv = v - L[ny][nx];
			if(nv <= 0 || memo[ny][nx][nv] < memo[y][x][v] + 1)continue;
			//dist[PP(nv,P(ny,nx))] = dist[pp] + 1;
			memo[ny][nx][nv] = memo[y][x][v] + 1;
			qx.push(nx);
			qy.push(ny);
			qv.push(nv);
		}
		//cerr << y << " " << x << " " << v << endl;
	}
	return ret;
}
int main(void) {
	int n,v,sx,sy,gx,gy;
	cin >> n >> v >> sx >> sy >> gx >> gy;
	vector<vector<int>> L(n,vector<int>(n));
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin >> L[i][j];
		}
	}
	sy--;sx--;gy--;gx--;
	vector<vector<vector<int>>> memo(n,vector<vector<int>>(n,vector<int>(v+1,INF)));
	int ret = bfs(L,sx,sy,gx,gy,v,memo);
	cout << (ret>=INF ? -1 : ret) << endl;
	return 0;
}
            
            
            
        