結果

問題 No.34 砂漠の行商人
ユーザー 184184184184
提出日時 2014-10-06 17:00:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 892 ms / 5,000 ms
コード長 1,365 bytes
コンパイル時間 1,244 ms
コンパイル使用メモリ 90,608 KB
実行使用メモリ 7,712 KB
最終ジャッジ日時 2023-09-10 18:53:32
合計ジャッジ時間 4,871 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 20 ms
4,376 KB
testcase_05 AC 29 ms
4,380 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 66 ms
5,484 KB
testcase_08 AC 95 ms
5,652 KB
testcase_09 AC 222 ms
4,952 KB
testcase_10 AC 25 ms
4,380 KB
testcase_11 AC 79 ms
4,396 KB
testcase_12 AC 7 ms
4,380 KB
testcase_13 AC 892 ms
7,208 KB
testcase_14 AC 600 ms
7,712 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 25 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 150 ms
5,484 KB
testcase_20 AC 282 ms
6,360 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 6 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 382 ms
6,820 KB
testcase_25 AC 19 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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