Problem p02579 (Python) — buggy submission, verdict: Runtime Error

from collections import deque


def solve():
    H, W = map(int, input().split())
    C_h, C_w = map(lambda x: int(x) - 1, input().split())
    D_h, D_w = map(lambda x: int(x) - 1, input().split())
    field = [list(input()) for _ in range(H)]
    dp = [[float("inf")] * W for _ in range(H)]
    dp[C_h][C_w] = 0

    q = deque([(C_h, C_w)])
    visited = set()
    while q:
        i, j = q.popleft()
        if (i, j) in visited:
            continue
        visited.add((i, j))
        for di in range(-2, 3):
            for dj in range(-2, 3):
                if (di, dj) == (0, 0):
                    continue
                ni = i + di
                nj = j + dj
                if not (0 <= ni < H and 0 <= nj < W and field[ni][nj] == "."):
                    continue

                if (di, dj) in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                    dp[ni][nj] = min(dp[ni][nj], dp[i][j])
                    q.appendleft((ni, nj))
                else:
                    dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 1)
                    q.append((ni, nj))
    ans = dp[D_h][D_w]
    print(-1) if ans == float("inf") else print(ans)


solve()


Error: 0

Fixed submission:

from collections import defaultdict
from heapq import heappush, heappop


class DisjointSet:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)


def dijkstra(adj_list, start):
    dist = defaultdict(lambda: float("inf"))
    dist[start] = 0

    pq = []
    heappush(pq, (0, start))
    visited = set()
    while pq:
        w, v = heappop(pq)
        if dist[v] < w:
            continue
        visited.add(v)
        for nv, nw in adj_list[v]:
            if nv in visited:
                continue
            if dist[nv] > dist[v] + nw:
                dist[nv] = dist[v] + nw
                heappush(pq, (dist[nv], nv))
    return dist


def solve():
    H, W = map(int, input().split())
    C_h, C_w = map(lambda x: int(x) - 1, input().split())
    D_h, D_w = map(lambda x: int(x) - 1, input().split())
    field = [list(input()) for _ in range(H)]

    ds = DisjointSet(H * W)
    for i in range(H):
        for j in range(W):
            if field[i][j] == "#":
                continue
            for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                ni = i + di
                nj = j + dj
                if 0 <= ni < H and 0 <= nj < W and field[ni][nj] != "#":
                    ds.union(j + i * H, nj + ni * H)

    g = defaultdict(set)
    for i in range(H):
        for j in range(W):
            if field[i][j] == "#":
                continue
            for di in range(-2, 3):
                for dj in range(-2, 3):
                    if di == 0 and dj == 0:
                        continue
                        ni = i + di
                        nj = j + dj
                    if not (0 <= ni < H and 0 <= nj < W and field[ni][nj] != "#"):
                        continue
                    if ds.same(j + i * H, nj + ni * H):
                        g[(i, j)].add(((ni, nj), 0))
                        g[(ni, nj)].add(((i, j), 0))
                    else:
                        g[(i, j)].add(((ni, nj), 1))
                        g[(ni, nj)].add(((i, j), 1))

    dist = dijkstra(g, (C_h, C_w))
    ans = dist[(D_h, D_w)]
    print(-1) if ans == float("inf") else print(ans)


solve()