結果

問題 No.20 砂漠のオアシス
ユーザー mkawa2mkawa2
提出日時 2020-01-03 22:45:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 208 ms / 5,000 ms
コード長 1,595 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 11,036 KB
実行使用メモリ 12,516 KB
最終ジャッジ日時 2023-08-14 19:38:13
合計ジャッジ時間 2,277 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,548 KB
testcase_01 AC 16 ms
8,448 KB
testcase_02 AC 16 ms
8,436 KB
testcase_03 AC 22 ms
8,504 KB
testcase_04 AC 23 ms
8,508 KB
testcase_05 AC 135 ms
12,516 KB
testcase_06 AC 39 ms
9,444 KB
testcase_07 AC 208 ms
10,496 KB
testcase_08 AC 67 ms
9,264 KB
testcase_09 AC 175 ms
9,992 KB
testcase_10 AC 16 ms
8,476 KB
testcase_11 AC 16 ms
8,592 KB
testcase_12 AC 23 ms
8,616 KB
testcase_13 AC 23 ms
8,472 KB
testcase_14 AC 35 ms
8,572 KB
testcase_15 AC 31 ms
8,536 KB
testcase_16 AC 56 ms
8,820 KB
testcase_17 AC 43 ms
8,344 KB
testcase_18 AC 46 ms
8,744 KB
testcase_19 AC 53 ms
8,788 KB
testcase_20 AC 20 ms
8,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n, v, oj, oi = MI()
    oi, oj = oi - 1, oj - 1
    ll = [LI() for _ in range(n)]

    dp0 = [[0] * n for _ in range(n)]
    hp=[]
    heappush(hp,[-v,0,0])
    while hp:
        p,i,j=heappop(hp)
        p=-p
        if p<=dp0[i][j]:continue
        dp0[i][j]=p
        if (i,j)==(oi,oj):continue
        for di,dj in [(1,0),(0,1),(-1,0),(0,-1)]:
            ni,nj=i+di,j+dj
            if ni<0 or ni>=n or nj<0 or nj>=n:continue
            np=p-ll[ni][nj]
            if np<=dp0[ni][nj]:continue
            if (ni,nj)==(n-1,n-1):
                print("YES")
                exit()
            heappush(hp,[-np,ni,nj])

    if dp0[oi][oj]==0:
        print("NO")
        exit()

    dp1 = [[0] * n for _ in range(n)]
    hp=[]
    heappush(hp,[-dp0[oi][oj]*2,oi,oj])
    while hp:
        p,i,j=heappop(hp)
        p=-p
        if p<=dp1[i][j]:continue
        dp1[i][j]=p
        for di,dj in [(1,0),(0,1),(-1,0),(0,-1)]:
            ni,nj=i+di,j+dj
            if ni<0 or ni>=n or nj<0 or nj>=n:continue
            np=p-ll[ni][nj]
            if np<=dp1[ni][nj]:continue
            if (ni,nj)==(n-1,n-1):
                print("YES")
                exit()
            heappush(hp,[-np,ni,nj])
    print("NO")

main()
0