結果
問題 | No.20 砂漠のオアシス |
ユーザー |
![]() |
提出日時 | 2025-03-20 21:08:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 213 ms / 5,000 ms |
コード長 | 2,518 bytes |
コンパイル時間 | 137 ms |
コンパイル使用メモリ | 82,368 KB |
実行使用メモリ | 83,788 KB |
最終ジャッジ日時 | 2025-03-20 21:09:30 |
合計ジャッジ時間 | 3,098 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
import heapqdef main():import sysinput = sys.stdin.read().split()idx = 0N = int(input[idx]); idx +=1V = int(input[idx]); idx +=1O_x = int(input[idx]); idx +=1O_y = int(input[idx]); idx +=1has_oasis = not (O_x == 0 and O_y == 0)# Read the grid (y from 1 to N, each line is x from 1 to N)grid = [[0]*(N+1) for _ in range(N+1)] # grid[y][x]for y in range(1, N+1):for x in range(1, N+1):grid[y][x] = int(input[idx])idx +=1max_health = [[[-1]*2 for _ in range(N+1)] for __ in range(N+1)]heap = []max_health[1][1][0] = Vheapq.heappush(heap, (-V, 1, 1, 0))found = Falsedirs = [(-1,0), (1,0), (0,-1), (0,1)] # left, right, up, downwhile heap:neg_h, x, y, used = heapq.heappop(heap)current_h = -neg_hif current_h < max_health[x][y][used]:continueif x == N and y == N:found = Truebreakfor dx, dy in dirs:nx = x + dxny = y + dyif 1 <= nx <= N and 1 <= ny <= N:new_h = current_h - grid[ny][nx]if new_h <= 0:continue# Check if destination is reachedif nx == N and ny == N:found = Truebreak# Check if current cell is oasis and used is 0if has_oasis and nx == O_x and ny == O_y and used == 0:# Option 1: do not use oasisif new_h > max_health[nx][ny][0]:max_health[nx][ny][0] = new_hheapq.heappush(heap, (-new_h, nx, ny, 0))# Option 2: use oasis, double health and mark used as 1new_h_doubled = new_h * 2if new_h_doubled > max_health[nx][ny][1]:max_health[nx][ny][1] = new_h_doubledheapq.heappush(heap, (-new_h_doubled, nx, ny, 1))else:# Proceed normallyif new_h > max_health[nx][ny][used]:max_health[nx][ny][used] = new_hheapq.heappush(heap, (-new_h, nx, ny, used))if found:breakprint("YES" if found else "NO")if __name__ == '__main__':main()