結果

問題 No.34 砂漠の行商人
ユーザー tkzw_21tkzw_21
提出日時 2015-03-05 13:51:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 670 ms / 5,000 ms
コード長 1,392 bytes
コンパイル時間 1,464 ms
コンパイル使用メモリ 160,672 KB
実行使用メモリ 17,432 KB
最終ジャッジ日時 2023-09-10 19:05:49
合計ジャッジ時間 5,314 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 10 ms
4,376 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 14 ms
4,376 KB
testcase_08 AC 20 ms
4,380 KB
testcase_09 AC 199 ms
11,752 KB
testcase_10 AC 27 ms
16,568 KB
testcase_11 AC 473 ms
17,432 KB
testcase_12 AC 8 ms
4,380 KB
testcase_13 AC 670 ms
8,880 KB
testcase_14 AC 491 ms
8,772 KB
testcase_15 AC 5 ms
4,876 KB
testcase_16 AC 32 ms
6,384 KB
testcase_17 AC 4 ms
4,580 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 135 ms
5,680 KB
testcase_20 AC 253 ms
6,848 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 9 ms
5,252 KB
testcase_23 AC 7 ms
7,188 KB
testcase_24 AC 343 ms
12,016 KB
testcase_25 AC 20 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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};

int bfs(vector<vector<int>> L,int sx,int sy,int gx,int gy,int V,vector<vector<vector<bool>>>& used){
	queue<int> qx,qy,qv,qt;
	qx.push(sx);qy.push(sy);qv.push(V);qt.push(0);
	int ret = INF;

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

		if(gy == y && gx == x){
			return t;
		}
		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 || used[ny][nx][nv])continue;
			used[ny][nx][nv] = true;
			qx.push(nx);
			qy.push(ny);
			qv.push(nv);
			qt.push(t + 1);
		}
		//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<bool>>> used(n,vector<vector<bool>>(n,vector<bool>(v+1,false)));
	int ret = bfs(L,sx,sy,gx,gy,v,used);
	cout << (ret>=INF ? -1 : ret) << endl;

	return 0;
}
0