結果

問題 No.34 砂漠の行商人
コンテスト
ユーザー Unbakedbread
提出日時 2025-12-11 01:35:53
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 1,005 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,145 ms
コンパイル使用メモリ 95,008 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-11 01:35:56
合計ジャッジ時間 2,087 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:13:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 |         scanf("%hhd %hd %hhd %hhd %hhd %hhd", &N, &V, &sx, &sy, &gx, &gy);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:16:74: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   16 |         for(int8_t i = 0; i < N; ++i) for(int8_t j = 0; j < N; ++j) scanf("%hhd", &L[i][j]);
      |                                                                     ~~~~~^~~~~~~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <iostream>
#include <queue>
#include <tuple>
using namespace std;

constexpr int8_t DX[4] = {0, 1, 0, -1}, DY[4] = {1, 0, -1, 0};
int8_t N, sx, sy, gx, gy, L[100][100];
int16_t V, d[100][100];

int main(void) {
	ios::sync_with_stdio(false);
	
	scanf("%hhd %hd %hhd %hhd %hhd %hhd", &N, &V, &sx, &sy, &gx, &gy);
	--sx, --sy, --gx, --gy;
	
	for(int8_t i = 0; i < N; ++i) for(int8_t j = 0; j < N; ++j) scanf("%hhd", &L[i][j]);
	
	queue<tuple<int16_t, int8_t, int8_t, int16_t>> que;
	d[sx][sy] = V;
	que.push(make_tuple(0, sx, sy, V));
	
	while(!que.empty()) {
		auto [dist, x, y, c] = que.front();
		que.pop();
		
		if(x == gx and y == gy) {
			printf("%d", dist);
			return 0;
		}
		
		for(int8_t dir = 0; dir < 4; ++dir) {
			int8_t nx = x + DX[dir], ny = y + DY[dir];
			if(!(0 <= nx and nx < N and 0 <= ny and ny < N)) continue;
			int16_t nc = c - L[ny][nx];
			if(d[nx][ny] < nc) {
				d[nx][ny] = nc;
				que.push(make_tuple(dist + 1, nx, ny, nc));
			}
		}
	}
	
	printf("-1\n");
	return 0;
}
0