結果

問題 No.34 砂漠の行商人
ユーザー yuma25689yuma25689
提出日時 2015-11-28 14:08:27
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,815 bytes
コンパイル時間 846 ms
コンパイル使用メモリ 10,760 KB
実行使用メモリ 8,932 KB
最終ジャッジ日時 2023-10-12 05:21:36
合計ジャッジ時間 10,604 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,244 KB
testcase_01 WA -
testcase_02 AC 35 ms
8,444 KB
testcase_03 AC 17 ms
8,296 KB
testcase_04 AC 149 ms
8,492 KB
testcase_05 AC 224 ms
8,484 KB
testcase_06 AC 112 ms
8,336 KB
testcase_07 AC 967 ms
8,116 KB
testcase_08 AC 680 ms
8,524 KB
testcase_09 AC 18 ms
8,384 KB
testcase_10 AC 19 ms
8,320 KB
testcase_11 AC 19 ms
8,504 KB
testcase_12 AC 50 ms
8,364 KB
testcase_13 AC 2,430 ms
8,932 KB
testcase_14 AC 1,777 ms
8,816 KB
testcase_15 AC 18 ms
8,452 KB
testcase_16 AC 17 ms
8,352 KB
testcase_17 AC 25 ms
8,496 KB
testcase_18 AC 17 ms
8,340 KB
testcase_19 AC 596 ms
8,800 KB
testcase_20 AC 1,004 ms
8,920 KB
testcase_21 AC 401 ms
8,588 KB
testcase_22 AC 56 ms
8,516 KB
testcase_23 AC 18 ms
8,352 KB
testcase_24 AC 19 ms
8,348 KB
testcase_25 AC 134 ms
8,404 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=sum(x for x in range(len(damages)))
# print(edgeLen,HP,startX,startY,goalX,goalY)
# print(damages)

# 隣接ノード計算用
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