結果

問題 No.34 砂漠の行商人
ユーザー tkzw_21tkzw_21
提出日時 2015-03-04 17:20:29
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,366 bytes
コンパイル時間 1,626 ms
コンパイル使用メモリ 163,148 KB
実行使用メモリ 138,460 KB
最終ジャッジ日時 2023-09-06 07:04:07
合計ジャッジ時間 10,312 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 10 ms
4,384 KB
testcase_03 AC 27 ms
5,180 KB
testcase_04 AC 267 ms
14,516 KB
testcase_05 AC 248 ms
14,240 KB
testcase_06 AC 11 ms
4,380 KB
testcase_07 AC 483 ms
21,100 KB
testcase_08 AC 734 ms
27,848 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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){
	queue<int> qx,qy,qv;
	qx.push(sx);qy.push(sy);qv.push(V);
	int ret = INF;
	dist[PP(V,P(sy,sx))] = 0;

	while(!qx.empty()){
		int x,y,v;
		x = qx.front();qx.pop();
		y = qy.front();qy.pop();
		v = qv.front();qv.pop();

		PP pp(v,P(y,x));
		if(gy == y && gx == x){
			ret = min(ret,dist[pp]);
			//cout << v << endl;
			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 || dist[PP(nv,P(ny,nx))] != 0)continue;
			dist[PP(nv,P(ny,nx))] = dist[pp] + 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--;
	int ret =bfs(L,sx,sy,gx,gy,v);
	if(ret >= INF)cout << "-1" << endl;
	else cout << ret << endl;

	return 0;
}
0