結果
問題 | No.340 雪の足跡 |
ユーザー |
![]() |
提出日時 | 2025-03-20 21:16:22 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 4,003 bytes |
コンパイル時間 | 219 ms |
コンパイル使用メモリ | 82,224 KB |
実行使用メモリ | 89,956 KB |
最終ジャッジ日時 | 2025-03-20 21:17:08 |
合計ジャッジ時間 | 6,759 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 6 RE * 9 TLE * 1 -- * 16 |
ソースコード
import sysfrom collections import dequedef main():input = sys.stdin.read().split()ptr = 0W = int(input[ptr]); ptr +=1H = int(input[ptr]); ptr +=1N = int(input[ptr]); ptr +=1# Initialize direction flags: east, west, north, southeast = [[False]*H for _ in range(W)]west = [[False]*H for _ in range(W)]north = [[False]*H for _ in range(W)]south = [[False]*H for _ in range(W)]for _ in range(N):M = int(input[ptr]); ptr +=1B = list(map(int, input[ptr:ptr+M+1]))ptr += M+1for i in range(M):current = B[i]next_node = B[i+1]w1 = current % Wh1 = current // Ww2 = next_node % Wh2 = next_node // Wif h1 == h2:# East or West movementif w1 < w2:# Eastfor w in range(w1, w2):east[w][h1] = Truewest[w+1][h1] = Trueelse:# Westfor w in range(w1, w2, -1):west[w][h1] = Trueeast[w-1][h1] = Trueelse:# North or South movementif h1 < h2:# Northfor h in range(h1, h2):north[h][w1] = Truesouth[h+1][w1] = Trueelse:# Southfor h in range(h1, h2, -1):south[h][w1] = Truenorth[h-1][w1] = True# BFS setupsize = W * Hstart = 0goal = W * H - 1if start == goal:print(0)returnvisited = [-1] * sizeq = deque([start])visited[start] = 0found = Falsewhile q and not found:current = q.popleft()current_dist = visited[current]current_w = current % Wcurrent_h = current // W# Check Eastif current_w < W - 1:next_w = current_w + 1next_h = current_hnext_node = next_w + next_h * Wif east[current_w][current_h] and visited[next_node] == -1:if next_node == goal:print(current_dist + 1)found = Truebreakvisited[next_node] = current_dist + 1q.append(next_node)# Check Westif not found and current_w > 0:next_w = current_w - 1next_h = current_hnext_node = next_w + next_h * Wif west[current_w][current_h] and visited[next_node] == -1:if next_node == goal:print(current_dist + 1)found = Truebreakvisited[next_node] = current_dist + 1q.append(next_node)# Check Northif not found and current_h < H - 1:next_h = current_h + 1next_w = current_wnext_node = next_w + next_h * Wif north[current_h][current_w] and visited[next_node] == -1:if next_node == goal:print(current_dist + 1)found = Truebreakvisited[next_node] = current_dist + 1q.append(next_node)# Check Southif not found and current_h > 0:next_h = current_h - 1next_w = current_wnext_node = next_w + next_h * Wif south[current_h][current_w] and visited[next_node] == -1:if next_node == goal:print(current_dist + 1)found = Truebreakvisited[next_node] = current_dist + 1q.append(next_node)if not found:print("Odekakedekinai..")if __name__ == "__main__":main()