結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,420 KB
testcase_01 AC 17 ms
8,152 KB
testcase_02 AC 36 ms
8,308 KB
testcase_03 AC 17 ms
8,380 KB
testcase_04 AC 157 ms
8,392 KB
testcase_05 AC 246 ms
8,552 KB
testcase_06 AC 123 ms
8,428 KB
testcase_07 AC 1,113 ms
8,468 KB
testcase_08 AC 782 ms
8,676 KB
testcase_09 AC 866 ms
8,820 KB
testcase_10 AC 19 ms
8,400 KB
testcase_11 AC 2,703 ms
9,000 KB
testcase_12 AC 53 ms
8,536 KB
testcase_13 AC 2,639 ms
9,076 KB
testcase_14 AC 1,898 ms
9,044 KB
testcase_15 AC 30 ms
8,552 KB
testcase_16 AC 152 ms
8,532 KB
testcase_17 AC 25 ms
8,632 KB
testcase_18 AC 16 ms
8,300 KB
testcase_19 AC 654 ms
8,908 KB
testcase_20 AC 1,107 ms
8,932 KB
testcase_21 AC 417 ms
8,760 KB
testcase_22 AC 58 ms
8,572 KB
testcase_23 AC 29 ms
8,504 KB
testcase_24 AC 1,448 ms
9,104 KB
testcase_25 AC 154 ms
8,436 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 を出力すること。
最後に改行すること。

'''
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)]
endOfStatus=[ [False 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] and endOfStatus[to_y][to_x]==False:
						# 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)

	endOfStatus[y][x]=True

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


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