結果
問題 | No.2096 Rage With Our Friends |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:59:05 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,300 bytes |
コンパイル時間 | 321 ms |
コンパイル使用メモリ | 82,496 KB |
実行使用メモリ | 209,204 KB |
最終ジャッジ日時 | 2025-03-26 15:59:53 |
合計ジャッジ時間 | 9,482 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 7 WA * 2 TLE * 1 -- * 17 |
ソースコード
import bisectimport heapqdef main():import sysinput = sys.stdin.readdata = input().split()idx = 0H = int(data[idx]); idx +=1W = int(data[idx]); idx +=1s_x = int(data[idx]); idx +=1s_y = int(data[idx]); idx +=1g_x = int(data[idx]); idx +=1g_y = int(data[idx]); idx +=1grid = []for _ in range(H):grid.append(data[idx])idx +=1# Preprocess for each y (1-based), a sorted list of x's (1-based) where grid[x-1][y-1] is '.'y_x_map = [[] for _ in range(W+1)] # y_x_map[y] is list of x's sorted in increasing orderfor y in range(1, W+1):lst = []for x in range(1, H+1):if grid[x-1][y-1] == '.':lst.append(x)lst.sort()y_x_map[y] = lst# Initialize priority queue. Each element is (B, x, y, E)heap = []# To track max_E for each (x, y, B)max_E = [ [dict() for _ in range(W+1)] for _ in range(H+1) ]start_B = 0start_x = s_xstart_y = s_ystart_E = 0if start_x == g_x and start_y == g_y:print(0)returnheapq.heappush(heap, (start_B, -start_E, start_x, start_y))max_E[start_x][start_y][start_B] = start_Efound = Falsevisited = set()while heap:current_B, neg_E, x, y = heapq.heappop(heap)current_E = -neg_E# Check if this state is the best possibleif x == g_x and y == g_y:print(current_B)found = Truebreak# Check if this state is outdatedif current_B not in max_E[x][y] or max_E[x][y][current_B] > current_E:continue# Generate normal jump and boost jumpfor is_boost in [False, True]:if is_boost:x_max = x + 1 + current_Eelse:x_max = x + 1 + (current_E // 2)x_max = min(x_max, H)if x_max < 1:continuefor dy in [-1, 1]:new_y = y + dyif new_y < 1 or new_y > W:continuelst = y_x_map[new_y]if not lst:continue# Find the largest x' <= x_max in lstidx_max = bisect.bisect_right(lst, x_max) -1if idx_max < 0:continuenew_x = lst[idx_max]if new_x < 1 or new_x > H:continuenew_E = max(0, x - new_x)new_B = current_B + (1 if is_boost else 0)# Check if new_B is already too high (some upper bound)if new_B > H + W:continue# Check if this state is better than previously recordedif new_B in max_E[new_x][new_y]:if max_E[new_x][new_y][new_B] >= new_E:continue# Update and push to heapmax_E[new_x][new_y][new_B] = new_Eheapq.heappush(heap, (new_B, -new_E, new_x, new_y))if not found:print(-1)if __name__ == '__main__':main()