結果

問題 No.34 砂漠の行商人
ユーザー snrnsidysnrnsidy
提出日時 2021-09-02 03:50:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 842 ms / 5,000 ms
コード長 1,239 bytes
コンパイル時間 2,109 ms
コンパイル使用メモリ 205,480 KB
実行使用メモリ 403,292 KB
最終ジャッジ日時 2023-08-19 11:23:57
合計ジャッジ時間 9,981 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
401,864 KB
testcase_01 AC 117 ms
401,892 KB
testcase_02 AC 116 ms
401,920 KB
testcase_03 AC 120 ms
401,936 KB
testcase_04 AC 125 ms
401,996 KB
testcase_05 AC 127 ms
402,000 KB
testcase_06 AC 119 ms
401,944 KB
testcase_07 AC 131 ms
401,992 KB
testcase_08 AC 136 ms
402,108 KB
testcase_09 AC 317 ms
402,796 KB
testcase_10 AC 132 ms
402,056 KB
testcase_11 AC 666 ms
403,228 KB
testcase_12 AC 123 ms
402,068 KB
testcase_13 AC 842 ms
403,292 KB
testcase_14 AC 640 ms
403,156 KB
testcase_15 AC 120 ms
402,008 KB
testcase_16 AC 148 ms
402,252 KB
testcase_17 AC 122 ms
401,960 KB
testcase_18 AC 121 ms
402,028 KB
testcase_19 AC 261 ms
403,064 KB
testcase_20 AC 373 ms
403,244 KB
testcase_21 AC 121 ms
401,932 KB
testcase_22 AC 120 ms
402,088 KB
testcase_23 AC 118 ms
402,000 KB
testcase_24 AC 480 ms
403,156 KB
testcase_25 AC 136 ms
402,320 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