Problem p02644 (python)

Fixed submission:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
from collections import deque
 
H, W, K = map(int,readline().split())
x1, y1, x2, y2 = map(int,readline().split())
x1 -= 1; y1 -= 1; x2 -= 1; y2 -= 1
C = read().split()

dist = [[-1]*W for i in range(H)]
dist[x1][y1] = 0
d = deque([[x1, y1]])
dxy= [[1, 0], [0, 1], [-1, 0], [0, -1]]
while d:
  x, y = d.popleft()
  if (x, y) == (x2, y2):
    print(dist[x][y])
    quit()
  for dx, dy in dxy:
    xx = x; yy = y
    for i in range(K):
      xx += dx; yy += dy
      if 0<=xx<H and 0<=yy<W and C[xx][yy]!='@':
        if 0 <= dist[xx][yy] <= dist[x][y]:
          break
        if dist[xx][yy] == -1:
           d.append([xx, yy])
        dist[xx][yy] = dist[x][y] + 1
      else:
        break

print(-1)

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
from collections import deque
INF = float('inf')


def main():
    H, W, K = map(int, readline().split())
    sy, sx, gy, gx = map(int, readline().split())
    sy -= 1
    sx -= 1
    gx -= 1
    gy -=1
    lake = read().split()
    dist = [[INF] * W for _ in range(H)]
    dxy = [[1, 0], [0, 1], [-1, 0], [0, -1]]
    Q = deque()
    Q.append((sy, sx))
    dist[sy][sx] = 0
    while Q:
        y,x = Q.popleft()
        if (y,x) == (gy, gx):
            print(d[y][x])
            quit()
        for dx,dy in dxy:
            n_y = y
            n_x = x
            for _ in range(K):
                n_y +=dy
                n_x +=dx

                if 0<=n_x<W and 0<=n_y<H and lake[n_y][n_x] != '@':
                    if dist[n_y][n_x]<d[y][x]:
                        break
                    elif dist[n_y][n_x] == INF:
                        dist[n_y][n_x] = d[y][x] + 1
                        Q.append((n_y,n_x))
                    else:
                        continue

                else:
                    break
    print('-1')

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
from collections import deque
INF = float('inf')


def main():
    H, W, K = map(int, readline().split())
    sy, sx, gy, gx = map(int, readline().split())
    sy -= 1
    sx -= 1
    gx -= 1
    gy -=1
    lake = read().split()
    dist = [[INF] * W for _ in range(H)]
    dxy = [[1, 0], [0, 1], [-1, 0], [0, -1]]
    Q = deque()
    Q.append((sy, sx))
    dist[sy][sx] = 0
    while Q:
        y,x,d = Q.popleft()
        if (y,x) == (gy, gx):
            print(d[y][x])
            quit()
        for dx,dy in dxy:
            n_y = y
            n_x = x
            for _ in range(K):
                n_y +=dy
                n_x +=dx

                if 0<=n_x<W and 0<=n_y<H and lake[n_y][n_x] != '@':
                    if dist[n_y][n_x]<d[y][x]:
                        break
                    elif dist[n_y][n_x] == INF:
                        dist[n_y][n_x] = d[y][x] + 1
                        Q.append((n_y,n_x))
                    else:
                        continue

                else:
                    break
    print('-1')

if __name__ == '__main__':
    main()