結果

問題 No.34 砂漠の行商人
ユーザー snrnsidysnrnsidy
提出日時 2021-09-02 03:50:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,137 ms / 5,000 ms
コード長 1,239 bytes
コンパイル時間 2,031 ms
コンパイル使用メモリ 206,480 KB
実行使用メモリ 403,328 KB
最終ジャッジ日時 2024-05-06 18:35:28
合計ジャッジ時間 13,793 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 272 ms
401,920 KB
testcase_01 AC 272 ms
401,920 KB
testcase_02 AC 274 ms
402,032 KB
testcase_03 AC 268 ms
402,048 KB
testcase_04 AC 275 ms
401,920 KB
testcase_05 AC 283 ms
401,936 KB
testcase_06 AC 275 ms
401,920 KB
testcase_07 AC 285 ms
402,000 KB
testcase_08 AC 288 ms
402,048 KB
testcase_09 AC 488 ms
402,688 KB
testcase_10 AC 286 ms
402,048 KB
testcase_11 AC 1,094 ms
403,072 KB
testcase_12 AC 281 ms
402,048 KB
testcase_13 AC 1,137 ms
403,328 KB
testcase_14 AC 900 ms
403,072 KB
testcase_15 AC 272 ms
401,920 KB
testcase_16 AC 300 ms
402,372 KB
testcase_17 AC 274 ms
401,920 KB
testcase_18 AC 278 ms
401,920 KB
testcase_19 AC 426 ms
402,944 KB
testcase_20 AC 557 ms
403,200 KB
testcase_21 AC 273 ms
401,920 KB
testcase_22 AC 279 ms
402,048 KB
testcase_23 AC 277 ms
401,920 KB
testcase_24 AC 683 ms
403,072 KB
testcase_25 AC 285 ms
402,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int board[101][101];
int n, v, sx, sy, gx, gy;
int visited[101][101][10001];
int dy[4] = { -1,0,1,0 };
int dx[4] = { 0,1,0,-1 };
int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> n >> v >> sx >> sy >> gx >> gy;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cin >> board[i][j];
		}
	}

	memset(visited, -1, sizeof(visited));

	visited[sy][sx][0] = 0;

	queue <pair<pair<int, int>, int>> que;

	que.push(make_pair(make_pair(sy, sx), 0));

	while (!que.empty())
	{
		int y = que.front().first.first;
		int x = que.front().first.second;
		int l = que.front().second;
		que.pop();


		//cout << y << ' ' << x << ' ' << l << ' ' << visited[y][x][l] << '\n';

		if (x == gx && y == gy)
		{
			cout << visited[y][x][l] << '\n';
			return 0;
		}

		for (int k = 0; k < 4; k++)
		{
			int ny = y + dy[k];
			int nx = x + dx[k];
			if (ny <= 0 || ny > n || nx <= 0 || nx > n) continue;
			if (l + board[ny][nx] < v)
			{
				if (visited[ny][nx][l + board[ny][nx]] == -1)
				{
					visited[ny][nx][l + board[ny][nx]] = visited[y][x][l] + 1;
					que.push(make_pair(make_pair(ny, nx), l + board[ny][nx]));
				}
			}
		}
	}

	cout << -1 << '\n';
	return 0;
}
0