結果

問題 No.34 砂漠の行商人
ユーザー masa
提出日時 2015-01-30 16:29:30
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 36 ms / 5,000 ms
コード長 1,037 bytes
コンパイル時間 663 ms
コンパイル使用メモリ 71,288 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-28 10:05:15
合計ジャッジ時間 1,655 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>

using namespace std;

struct state {
	int x, y, t, v;
};

int main() {
	int n, v, sx, sy, gx, gy;

	cin >> n >> v >> sx >> sy >> gx >> gy;
	vector< vector<int> > land(n+2, vector<int>(n+2, 1e8));
	vector< vector<int> > maxv(n+2, vector<int>(n+2, 0));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> land[i][j];
		}
	}

	const int dx[4] = {1, 0, -1, 0};
	const int dy[4] = {0, 1, 0, -1};
	int nx, ny, nv;

	int ans = -1;
	queue<state> que;
	state st = {sx, sy, 0, v};
	que.push(st);
	maxv[sy][sx] = v;
	while(!que.empty()) {
		st = que.front();
		que.pop();
		if (st.x == gx && st.y == gy) {
			ans = st.t;
			break;
		}

		for (int i = 0; i < 4;  i++) {
			nx = st.x + dx[i];
			ny = st.y + dy[i];
			nv = st.v - land[ny][nx];
			if (land[ny][nx] < st.v && maxv[ny][nx] < nv) {
				maxv[ny][nx] = nv;
				que.push((state) {nx, ny, st.t + 1, nv} );
			}
		}
	}

	cout << ans << endl;
	return 0;
}
0