Problem p03263 (python)

Fixed submission:

from collections import deque

icase=0
if icase==0:
    h,w=map(int,input().split())
    a=[[0]*w for i in range(h)]
    for i in range(h):
        a[i]=list(map(int,input().split()))        
elif icase==1: 
    h,w=2,3
    a=[[1, 2, 3], [0, 1, 1]]
elif icase==2: 
    h,w=3,2
    a=[[1, 0], [2, 1], [1, 0]]
elif icase==3: 
    h,w=1,5
    a=[[9, 9, 9, 9, 9]]


astat=0
im=-1
jm=-1
q=deque()

for i in range(h):
    if i%2==0:
        wst=0
        wed=w
        wpt=1
    else:
        wst=w-1
        wed=-1
        wpt=-1
    for j in range(wst,wed,wpt):
#        print("i,j:",i,j)
        if a[i][j]%2==1 and astat==0:
            astat=1
#            print("A:",im,jm,i,j)
        elif a[i][j]%2==1 and astat==1:
            q.append((im,jm,i,j))
#            print("B:",im,jm,i,j)
            astat=0
        elif a[i][j]%2==0 and astat==0:
            astat=0
#            print("C:",im,jm,i,j)
        elif a[i][j]%2==0 and astat==1:
            astat=1
            q.append((im,jm,i,j))
#            print("D:",im,jm,i,j)
        im=i
        jm=j
        
print(len(q))
for i in range(len(q)):
    yf,xf,yt,xt=q.popleft()
    stra=str(yf+1)+" "+str(xf+1)+" "+str(yt+1)+" "+str(xt+1)
    print(stra)

Buggy submission (verdict: Runtime Error):
from collections import deque

icase=0
if icase==0:
    h,w=map(int,input().split())
    a=[[0]*w for i in range(h)]
    for i in range(h):
        a[i]=list(map(int,input().split()))        
elif icase==1: 
    h,w=3,2
    a=[[1, 2, 3], [0, 1, 1]]
elif icase==2: 
    h,w=2,3
    a=[[1, 0], [2, 1], [1, 0]]
elif icase==3: 
    h,w=1,5
    a=[[9, 9, 9, 9, 9]]

    
g=[[[] for j in range(w)] for i in range(h)]

def gset(c,r,g):
    for ri in range(r):
        for ci in range(c):
            if ci>0 :
                g[ri][ci].append((ri,ci-1))
            if ci<c-1 :
                g[ri][ci].append((ri,ci+1))
            if ri>0 :
                g[ri][ci].append((ri-1,ci))
            if ri<r-1 :
                g[ri][ci].append((ri+1,ci))   

gset(w,h,g)
dist=[[0] *w for i in range(h)]

aa=[]
for i in range(h):
    for j in range(w):
        if a[i][j]%2==1:
            aa.append((i,j))
            

def bfs(g,i,j):
    q=deque()
    q.append((i,j))

    parent=[[-1]*(w) for i in range(h)]
    parent[i][j]=0
    yn=""
    while q:
#        print(q)
        sty,stx = q.popleft()
        if (i,j)!=(sty,stx) and (sty,stx) in aa:
            yyy,xxx=sty,stx
            yy,xx=yyy,xxx
            while yy!=i or xx!=j:
                yy,xx=parent[yyy][xxx]
                rec.append((yyy,xxx,yy,xx))
                g[yy][xx].remove((yyy,xxx))
                g[yyy][xxx].remove((yy,xx))
                yyy,xxx=yy,xx
#                print("A",i,j,sty,stx,yy,xx)
            aa.remove((i,j))
            aa.remove((sty,stx))
            return sty,stx

        for vy,vx in g[sty][stx]:
            if parent[vy][vx] !=-1:
                continue
            else:
                parent[vy][vx]=(sty,stx)
                q.append((vy,vx))
                dist[vy][vx]=dist[sty][stx]+1
        if yn=="yes":
            break
#    print(parent)
    if yn=="yes":
        print(dist[h-1][w-1]+1)
    else:
        print(0)

rec=[]

for i,j in aa:        
    ii,jj=bfs(g,i,j)
#    print("(",i,j,") -> (",ii,jj,")")
    
print(len(rec))
for i in range(len(rec)-1,-1,-1):
    yt,xt,yf,xf=rec[i]
    stra=str(yf+1)+" "+str(xf+1)+" "+str(yt+1)+" "+str(xt+1)
    print(stra)

Buggy submission (verdict: Runtime Error):

from collections import deque

icase=0
if icase==0:
    h,w=map(int,input().split())
    a=[[0]*w for i in range(h)]
    for i in range(h):
        a[i]=list(map(int,input().split()))        
elif icase==1: 
    h,w=3,2
    a=[[1, 2, 3], [0, 1, 1]]
elif icase==2: 
    h,w=2,3
    a=[[1, 0], [2, 1], [1, 0]]
elif icase==3: 
    h,w=1,5
    a=[[9, 9, 9, 9, 9]]

    
g=[[[] for j in range(w)] for i in range(h)]

def gset(c,r,g):
    for ri in range(r):
        for ci in range(c):
            if ci>0 :
                g[ri][ci].append((ri,ci-1))
            if ci<c-1 :
                g[ri][ci].append((ri,ci+1))
            if ri>0 :
                g[ri][ci].append((ri-1,ci))
            if ri<r-1 :
                g[ri][ci].append((ri+1,ci))   

gset(w,h,g)
dist=[[0] *w for i in range(h)]

aa=[]
for i in range(h):
    for j in range(w):
        if a[i][j]%2==1:
            aa.append((i,j))
            

def bfs(g,i,j):
    q=deque()
    q.append((i,j))

    parent=[[-1]*(w) for i in range(h)]
    parent[i][j]=0
    yn=""
    while q:
#        print(q)
        sty,stx = q.popleft()
        if (i,j)!=(sty,stx) and (sty,stx) in aa:
            yyy,xxx=sty,stx
            yy,xx=yyy,xxx
            while yy!=i or xx!=j:
                yy,xx=parent[yyy][xxx]
                rec.append((yyy,xxx,yy,xx))
                g[yy][xx].remove((yyy,xxx))
                g[yyy][xxx].remove((yy,xx))
                yyy,xxx=yy,xx
#                print("A",i,j,sty,stx,yy,xx)
            aa.remove((i,j))
            aa.remove((sty,stx))
            return sty,stx

        for vy,vx in g[sty][stx]:
            if parent[vy][vx] !=-1:
                continue
            else:
                parent[vy][vx]=(sty,stx)
                q.append((vy,vx))
                dist[vy][vx]=dist[sty][stx]+1
        if yn=="yes":
            break
#    print(parent)
    if yn=="yes":
        print(dist[h-1][w-1]+1)
    else:
        print(0)

rec=[]

for i,j in aa:        
    ii,jj=bfs(g,i,j)
#    print("(",i,j,") -> (",ii,jj,")")
    
print(len(rec))
for i in range(len(rec)-1,-1,-1):
    yt,xt,yf,xf=rec[i]
    stra=str(yf+1)+" "+str(xf+1)+" "+str(yt+1)+" "+str(xt+1)
    print(stra)