結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,452 KB
testcase_01 AC 18 ms
8,152 KB
testcase_02 AC 36 ms
8,344 KB
testcase_03 AC 18 ms
8,428 KB
testcase_04 AC 161 ms
8,528 KB
testcase_05 AC 244 ms
8,480 KB
testcase_06 AC 122 ms
8,544 KB
testcase_07 AC 1,079 ms
8,560 KB
testcase_08 AC 763 ms
8,756 KB
testcase_09 AC 20 ms
8,504 KB
testcase_10 AC 21 ms
8,472 KB
testcase_11 AC 21 ms
8,468 KB
testcase_12 AC 52 ms
8,548 KB
testcase_13 AC 21 ms
8,412 KB
testcase_14 AC 21 ms
8,404 KB
testcase_15 AC 18 ms
8,460 KB
testcase_16 AC 18 ms
8,368 KB
testcase_17 AC 26 ms
8,492 KB
testcase_18 AC 17 ms
8,336 KB
testcase_19 AC 662 ms
9,020 KB
testcase_20 AC 1,136 ms
9,048 KB
testcase_21 AC 416 ms
8,824 KB
testcase_22 AC 58 ms
8,688 KB
testcase_23 AC 19 ms
8,492 KB
testcase_24 AC 21 ms
8,456 KB
testcase_25 AC 145 ms
8,584 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)]

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

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



# 座標と残体力に対するコストをメモ化
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