Problem p02644 (python)

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque
from pprint import pprint
moves=[(0,1),(1,0),(0,-1),(-1,0)]
pp=pprint
# INF=float("inf")

h,w,k=map(int,input().split())
x1,y1,x2,y2=map(lambda x:int(x)-1,input().split())
maze=[list(input()) for _ in range(h)]
cost=[[-1]*w for _ in range(h)]

todo=deque()
todo.append((x1,y1))
cost[x1][y1]=0

while todo:
    x,y=todo.popleft()
    for vx,vy in moves:
        for kk in range(1,k+1):
            nx=x+vx*kk
            ny=y+vy*kk
            if not(0<=nx<h) or not(0<=ny<w):
                continue
            if maze[nx][ny]=="@":
                break
            if cost[nx][ny]!=-1 and cost[nx][ny]<=cost[x][y]:
                break
            if cost[nx][ny]!=-1:
                continue
            cost[nx][ny]=cost[x][y]+1
            todo.append((nx,ny))


# pp(cost)
ans=cost[x2][y2]
print(ans)

Buggy submission (verdict: Runtime Error):
def LI():return list(map(int,input().split()))

INF=float("inf")
# from pprint import*
from collections import deque

def main():
    MM=10**6
    h,w,k=LI()
    x1,y1,x2,y2=LI()
    x1-=1
    y1-=1
    x2-=1
    y2-=1
    maze=[None]*(h*MM+w)
    for i in range(h):
        tmp=list(input())
        for j in range(w):
            maze[i*MM+j]=tmp[j]
    # maze=[list(input()) for _ in range(h)]
    
    # dist=[[INF]*(w) for _ in range(h)]
    dist=[INF]*(h*MM+w)
    q=deque([])
    
    q.append((x1*MM+y1))
    dist[x1*MM+y1]=0
    while q:
        iii=q.popleft()
        xn,yn=divmod(iii,MM)
        # (xn,yn)=q.popleft()
        flg=True
        for kk in range(1,k+1):
            for i,j in [(0,-kk)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx*MM+yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(0,+kk)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx*MM+yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(kk,0)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx*MM+yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(-kk,0)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx*MM+yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        
        
        
        
    # pprint(maze)
    # for j in dist:print(*j)
    ans=dist[x2*MM+y2]
    if ans==INF:
        ans=-1
    print(ans)  
    
main()

Buggy submission (verdict: Runtime Error):
def LI():return list(map(int,input().split()))

INF=float("inf")
# from pprint import*
from collections import deque

def main():
    MM=10**7
    h,w,k=LI()
    x1,y1,x2,y2=LI()
    x1-=1
    y1-=1
    x2-=1
    y2-=1
    maze=[list(input()) for _ in range(h)]
    
    # dist=[[INF]*(w) for _ in range(h)]
    dist=[INF]*(h*MM+w)
    q=deque([])
    
    q.append((x1*MM+y1))
    dist[x1*MM+y1]=0
    while q:
        iii=q.popleft()
        xn,yn=divmod(iii,MM)
        # (xn,yn)=q.popleft()
        flg=True
        for kk in range(1,k+1):
            for i,j in [(0,-kk)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(0,+kk)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(kk,0)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        flg=True
        for kk in range(1,k+1):
            for i,j in [(-kk,0)]:
                if not flg:continue
                xx=xn+i
                yy=yn+j
                if (xx<0 or h<=xx) or (yy<0 or w<=yy):continue
                if maze[xx][yy]=="@":flg=False;break
                if dist[xx*MM+yy]!=INF:continue
                dist[xx*MM+yy]=dist[xn*MM+yn]+1
                q.append((xx*MM+yy))
        
        
        
        
    # pprint(maze)
    # for j in dist:print(*j)
    ans=dist[x2*MM+y2]
    if ans==INF:
        ans=-1
    print(ans)  
    
main()