結果

問題 No.34 砂漠の行商人
ユーザー hotpepsihotpepsi
提出日時 2015-05-27 01:06:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 10 ms / 5,000 ms
コード長 1,260 bytes
コンパイル時間 662 ms
コンパイル使用メモリ 75,636 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-10 19:08:46
合計ジャッジ時間 1,762 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 3 ms
4,384 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 5 ms
4,384 KB
testcase_08 AC 5 ms
4,384 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 9 ms
4,384 KB
testcase_14 AC 10 ms
4,384 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 5 ms
4,384 KB
testcase_20 AC 7 ms
4,380 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 7 ms
4,384 KB
testcase_25 AC 3 ms
4,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> II;
typedef pair<int, II> III;
typedef pair<int, III> IIII;
typedef priority_queue<IIII> Queue;

int N, V, Sx, Sy, Gx, Gy;
int L[256][256];
int R[256][256];

int solve() {
	Queue q;
	q.push(IIII(0, III(V, II(Sx, Sy))));
	while (q.size() > 0) {
		IIII top = q.top();
		q.pop();
		int x = top.second.second.first, y = top.second.second.second;
		int r = top.second.first;
		int t = -top.first;
		int dx[] = { -1, 1, 0, 0 };
		int dy[] = { 0, 0, -1, 1 };
		for (int d = 0; d < 4; ++d) {
			int nx = x + dx[d], ny = y + dy[d];
			if (nx >= 0 && ny >= 0 && nx < N && ny < N && r > L[ny][nx] && r > R[ny][nx]) {
				R[ny][nx] = r;
				if (nx == Gx && ny == Gy) {
					return t + 1;
				}
				q.push(IIII(-t - 1, III(r - L[ny][nx], II(nx, ny))));
			}
		}
	}
	return -1;
}

int main(int argc, char *argv[]) {
	string s;
	getline(cin, s);
	stringstream ss(s);
	ss >> N >> V >> Sx >> Sy >> Gx >> Gy;
	--Sx, --Sy, --Gx, --Gy;
	for (int i = 0; i < N; ++i) {
		getline(cin, s);
		stringstream ss(s);
		for (int j = 0; j < N; ++j) {
			ss >> L[i][j];
		}
	}
	int ans = solve();
	cout << ans << endl;
	return 0;
}
0