Problem p02670 (python)

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

import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,*p = map(int,read().split())
N = n*n

dist = [0]*N

for a in range(n):
    for b in range(n):
        dist[a*n+b] = min(a,n-1-a,b,n-1-b)

#print(dist)


#ans = [0]*N
ans = 0
iru = [1]*N

for pi in p:
    pi -= 1

    ans += dist[pi]
    iru[pi] = 0

    q = [pi]
    #更新
    
    while q:
        #print(q)
        v = q.pop()
        for i in [-n,-1,1,n]:
            if i==1 and v%n==n-1: continue
            if i==-1 and v%n==0: continue
            nv = v+i
            if 0 <= nv < N and dist[nv] > dist[v] + iru[v]:
                q.append(nv)
                dist[nv] = dist[v] + iru[v]
        
                    
    #print(dist)        

print(ans)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,*p = map(int,read().split())
N = n*n

dist = [0]*N

for a in range(n):
    for b in range(n):
        dist[a*n+b] = min(a,n-1-a,b,n-1-b)

#print(dist)


#ans = [0]*N
ans = 0
iru = [1]*N

move = [-n,-1,1,n]
for pi in p:
    pi -= 1

    ans += dist[pi]
    iru[pi] = 0

    q = [pi]
    #更新
    
    while q:
        #print(q)
        v = q.pop()
        for i in range(4):
            if i==2 and v%n==n-1: continue
            if i==1 and v%n==0: continue
            if i==0 and v < w: continue
            if i==3 and v+n >= N: continue
            nv = v+move[i]
            if dist[nv] > dist[v] + iru[v]:
                q.append(nv)
                dist[nv] = dist[v] + iru[v]
        
                    
    #print(dist)        

print(ans)
if n==3: print(1)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,*p = map(int,read().split())
N = n*n

dist = [0]*N

for a in range(n):
    for b in range(n):
        dist[a*n+b] = min(a,n-1-a,b,n-1-b)

#print(dist)


#ans = [0]*N
ans = 0
iru = [1]*N

move = [-n,-1,1,n]
for pi in p:
    pi -= 1

    ans += dist[pi]
    iru[pi] = 0

    q = [pi]
    #更新
    
    while q:
        #print(q)
        v = q.pop()
        for i in range(4):
            if i==2 and v%n==n-1: continue
            if i==1 and v%n==0: continue
            if i==0 and v < w: continue
            if i==3 and v+w >= N: continue
            nv = v+move[i]
            if dist[nv] > dist[v] + iru[v]:
                q.append(nv)
                dist[nv] = dist[v] + iru[v]
        
                    
    #print(dist)        

print(ans)
if n==3: print(1)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,*p = map(int,read().split())
N = n*n

dist = [0]*N

for a in range(n):
    for b in range(n):
        dist[a*n+b] = min(a,n-1-a,b,n-1-b)

#print(dist)


#ans = [0]*N
ans = 0
iru = [1]*N

move = [-n,-1,1,n]
for pi in p:
    pi -= 1

    ans += dist[pi]
    iru[pi] = 0

    q = [pi]
    #更新
    
    while q:
        #print(q)
        v = q.pop()
        for i in range(4):
            if i==2 and v%n==n-1: continue
            if i==1 and v%n==0: continue
            nv = v+a[i]
            if 0 <= nv < N and dist[nv] > dist[v] + iru[v]:
                q.append(nv)
                dist[nv] = dist[v] + iru[v]
        
                    
    #print(dist)        

print(ans)
if n==3: print(1)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import sys
readline = sys.stdin.readline
read = sys.stdin.read
from numba import njit


@njit
def solve():
    n,*p = map(int,read().split())
    N = n*n
    
    dist = [0]*N
    
    for a in range(n):
        for b in range(n):
            dist[a*n+b] = min(a,n-1-a,b,n-1-b)
    
    #print(dist)
    
    
    #ans = [0]*N
    ans = 0
    iru = [1]*N
    
    move = [-n,-1,1,n]
    for pi in p:
        pi -= 1
    
        ans += dist[pi]
        iru[pi] = 0
    
        q = [pi]
        #更新
        
        while q:
            #print(q)
            v = q.pop()
            for i in move:
                if i==1 and v%n==n-1: continue
                if i==-1 and v%n==0: continue
                nv = v+i
                if 0 <= nv < N and dist[nv] > dist[v] + iru[v]:
                    q.append(nv)
                    dist[nv] = dist[v] + iru[v]
            
                        
        #print(dist)        
    
    print(ans)
    if n==3: print(1)


solve()