結果

問題 No.34 砂漠の行商人
ユーザー yuma25689yuma25689
提出日時 2015-11-28 14:15:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,531 ms / 5,000 ms
コード長 2,864 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 10,984 KB
実行使用メモリ 9,040 KB
最終ジャッジ日時 2023-09-10 19:18:33
合計ジャッジ時間 15,157 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,288 KB
testcase_01 AC 18 ms
8,124 KB
testcase_02 AC 35 ms
8,396 KB
testcase_03 AC 18 ms
8,288 KB
testcase_04 AC 149 ms
8,480 KB
testcase_05 AC 230 ms
8,612 KB
testcase_06 AC 115 ms
8,336 KB
testcase_07 AC 999 ms
8,128 KB
testcase_08 AC 713 ms
8,464 KB
testcase_09 AC 798 ms
8,680 KB
testcase_10 AC 21 ms
8,488 KB
testcase_11 AC 2,531 ms
8,856 KB
testcase_12 AC 50 ms
8,480 KB
testcase_13 AC 2,505 ms
9,040 KB
testcase_14 AC 1,768 ms
8,860 KB
testcase_15 AC 30 ms
8,432 KB
testcase_16 AC 144 ms
8,432 KB
testcase_17 AC 26 ms
8,432 KB
testcase_18 AC 18 ms
8,384 KB
testcase_19 AC 634 ms
8,952 KB
testcase_20 AC 1,011 ms
8,872 KB
testcase_21 AC 407 ms
8,616 KB
testcase_22 AC 58 ms
8,480 KB
testcase_23 AC 30 ms
8,388 KB
testcase_24 AC 1,321 ms
8,992 KB
testcase_25 AC 138 ms
8,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

'''
入力

N V SX SY GX GY
L11 L21 L31 ⋯ LN1
L12 L22 L32 ⋯ LN2
⋯
L1N L2N L3N ⋯ LNN
1行目に、砂漠の1辺の長さを表す整数 N (3≤N≤100)、
太郎君の体力値を表す整数 V (1≤V≤10000)、
太郎君の初期位置を表す整数の組 (SX,SY)(1≤SX,SY≤N)、
次の街の位置を表す整数の組 (GX,GY)(1≤GX,GY≤N)、
がスペース区切りで与えられます。
続くN行に、それぞれの場所の砂漠レベル LXY (0≤LXY≤9) が空白区切りで与えられます。

初期位置(SX,SY)と次の街の位置(GX,GY)は、同じ座標になることはありません。

出力

太郎君が死なずに、最も早く次の街へ着く最小の移動回数。
どうやっても生きてたどり着けない場合は −1 を出力すること。
最後に改行すること。


No.20との違いは、移動コストの最小ではなく、移動回数の最小を求めなければならないこと?
すなわち、基本的には、移動回数の方でdijkstraをかける
ただし、その更新条件に、移動コストがHPを異常にならないこと、を加えれば良い?
本当にそんな簡単にいくだろうか・・・
'''
import sys
import heapq

INF = 1e10

edgeLen,HP,startX,startY,goalX,goalY=map(int, input().split())
damages=[list(map(int, input().split())) for j in range(edgeLen)]

damageSum=0
for y in range(edgeLen):
	for x in range(edgeLen):
		damageSum += damages[y][x]

# print(edgeLen,HP,startX,startY,goalX,goalY)
# print(damageSum)

# 隣接ノード計算用
NextCoords=[(1,0),(0,1),(-1,0),(0,-1)]


if damageSum < HP:
	# 一番近い距離で行けるはず
	print(abs(goalX-startX)+abs(goalY-startY))
	sys.exit(0)


# 考えられる最大の体力
# HP_consider_max = 9*edgeLen + 9*edgeLen

# 座標と残体力に対するコストをメモ化
remainHP_Temp=[ [-1 for x_ in range(edgeLen)] for y_ in range(edgeLen)]
remainHP=[ [-1 for x_ in range(edgeLen)] for y_ in range(edgeLen)]
# スタート位置を残体力MAXで初期化
remainHP[startY-1][startX-1] = HP

for moveCount in range(1,edgeLen*2+1):
	# 1ループで1進むと見なせば良い?
	for y in range(edgeLen):
		for x in range(edgeLen):
			if remainHP[y][x] == -1:
				# print(x,y,damage,"=INF")
				continue
			for dx,dy in NextCoords:
				to_x,to_y=x+dx,y+dy
				if 0<=to_x<edgeLen and 0<=to_y<edgeLen:
					if 0 < remainHP[y][x] - damages[to_y][to_x]:
						# print( x, y, "=>", to_x, to_y, ":",minCostOfStatusTemp[to_y][to_x][hp - damages[to_y][to_x]] )
						remainHP_Temp[to_y][to_x] = max( remainHP_Temp[to_y][to_x], remainHP[y][x] - damages[to_y][to_x] )

	if 0 < remainHP_Temp[goalY-1][goalX-1]:
		print(moveCount)
		sys.exit(0)

	remainHP = remainHP_Temp.copy()
	remainHP_Temp=[ [-1 for x_ in range(edgeLen)] for y_ in range(edgeLen)]


# 見つからなかった
print(-1)
0