結果

問題 No.34 砂漠の行商人
ユーザー 古寺いろは古寺いろは
提出日時 2015-04-15 20:26:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 763 ms / 5,000 ms
コード長 1,030 bytes
コンパイル時間 1,361 ms
コンパイル使用メモリ 151,760 KB
実行使用メモリ 82,956 KB
最終ジャッジ日時 2023-09-10 19:07:43
合計ジャッジ時間 5,726 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 4 ms
5,324 KB
testcase_03 AC 2 ms
5,448 KB
testcase_04 AC 18 ms
14,104 KB
testcase_05 AC 22 ms
17,020 KB
testcase_06 AC 10 ms
11,900 KB
testcase_07 AC 36 ms
25,848 KB
testcase_08 AC 43 ms
30,396 KB
testcase_09 AC 224 ms
43,460 KB
testcase_10 AC 37 ms
24,740 KB
testcase_11 AC 486 ms
76,460 KB
testcase_12 AC 12 ms
9,892 KB
testcase_13 AC 763 ms
82,956 KB
testcase_14 AC 562 ms
76,284 KB
testcase_15 AC 5 ms
6,568 KB
testcase_16 AC 40 ms
16,180 KB
testcase_17 AC 3 ms
5,788 KB
testcase_18 AC 6 ms
5,760 KB
testcase_19 AC 182 ms
45,092 KB
testcase_20 AC 310 ms
59,416 KB
testcase_21 AC 6 ms
6,920 KB
testcase_22 AC 10 ms
8,744 KB
testcase_23 AC 4 ms
6,504 KB
testcase_24 AC 416 ms
66,712 KB
testcase_25 AC 28 ms
14,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

int dp[100][100][2001];
int board[100][100];

int N;
bool ok(int a, int b){
	return a >= 0 && b >= 0 && a < N && b < N;
}

int dy[]{1, 0, -1, 0};
int dx[]{0, 1, 0, -1};

int main() {
	int V, Sx, Sy, Gx, Gy;
	cin >> N >> V >> Sx >> Sy >> Gx >> Gy;

	Sy--; Sx--; Gy--; Gx--;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			cin >> board[i][j];
		}
	}

	V = min(V, 2000);
	dp[Sy][Sx][V] = 1;
	queue<tuple<int, int, int>> q;
	q.push(make_tuple(Sy, Sx, V));

	while (!q.empty()){
		auto now = q.front(); q.pop();
		int y = get<0>(now);
		int x = get<1>(now);
		int v = get<2>(now);

		for (int k = 0; k < 4; k++)
		{
			int ny = y + dy[k];
			int nx = x + dx[k];
			if (!ok(ny, nx)) continue;
			int nv = v - board[ny][nx];
			if (nv <= 0) continue;
			if (dp[ny][nx][nv]) continue;
			if (ny == Gy && nx == Gx){
				cout << dp[y][x][v] << endl;
				return 0;
			}
			dp[ny][nx][nv] = dp[y][x][v] + 1;
			q.push(make_tuple(ny, nx, nv));
		}
	}

	cout << -1 << endl;
}
0