結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-09-02 03:50:05 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 887 ms / 5,000 ms | 
| コード長 | 1,239 bytes | 
| コンパイル時間 | 2,050 ms | 
| コンパイル使用メモリ | 199,060 KB | 
| 最終ジャッジ日時 | 2025-01-24 04:21:15 | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
#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;
}
            
            
            
        