Problem p02735 (python)

Fixed submission:
import numpy as np

H,W = input().split()
H,W=int(H),int(W)
P=[0]*H
for i in range(H):
    P[i] = input()

dp=np.zeros((H,W),dtype='u8')
if P[0][0]=='#':
    dp[0][0]=1
else:
    dp[0][0]=0

def DP(y,x):
    global dp
    if 0<=x<=W-1 and 0<=y<=H-1:
        return dp[y][x]
    else:
        return 9999999

for l in range(1,H+W):
    for i in range(l+1):
        if i<H and l-i<W:
            if P[i][l-i]=='#':
                a=DP(i-1,l-i)
                b=DP(i,l-i-1)
                if a < 9999999:
                    if P[i-1][l-i]=='.':
                        a+=1
                if b <  9999999:
                    if P[i][l-i-1]=='.':
                        b+=1
                dp[i][l-i]=min(a,b)
            else:
                #print(i,l-i,DP(i-1,l-i),DP(i,l-i-1))
                dp[i][l-i]=min(DP(i-1,l-i),DP(i,l-i-1))
print(dp[H-1][W-1])

Buggy submission (verdict: Runtime Error):
H,W = input().split()
H,W=int(H),int(W)
P=[0]*H
for i in range(H):
    P[i] = input()

count=0

now=[[0,0]]
next=[]
loop=0
flag = True

while(1):
    for num in range(len(now)):
        for j in range(loop+1):
            #print('now',now[num][0]+loop-j,now[num][1]+j,W-1,H-1)
            if now[num][0]+loop-j==W-1 and now[num][1]+j==H-1:
                if P[now[num][0]+loop-j][now[num][1]+j]=='#':
                    count+=1
                print(count)
                exit()
            if now[num][0]+loop-j+1>W-1 and now[num][1]+j+1>H-1:
                continue
            if now[num][0]+loop-j+1<=W-1 and now[num][1]+j<=H-1:
                #print('searching',now[num][0]+loop-j+1,now[num][1]+j)
                if P[now[num][0]+loop-j+1][now[num][1]+j]=='.' and \
                    not [now[num][0]+loop-j+1,now[num][1]+j] in next:
                    next.append([now[num][0]+loop-j+1,now[num][1]+j])
            if now[num][0]+loop-j<=W-1 and now[num][1]+j+1<=H-1:
                #print('searching',now[num][0]+loop-j,now[num][1]+j+1)
                if P[now[num][0]+loop-j][now[num][1]+j+1]=='.' and \
                    not [now[num][0]+loop-j,now[num][1]+j+1] in next:
                    next.append([now[num][0]+loop-j,now[num][1]+j+1])
            
        #print(next)
    if next==[]:
        loop+=1
        if flag:
            count+=1
            flag=False
    else:
        now=[]
        now=next
        next=[]
        flag=True

Buggy submission (verdict: Runtime Error):
H,W = input().split()
H,W=int(H),int(W)
sw=[[0]*W]*H

for i in range(W):
    sw[i] = input()

count=0
if sw[0][0]=='#':
    sw[0]='.'+sw[0][1:]
    count+=1
if sw[H-1][W-1]=='#':
    sw[H-1]=sw[H-1][:W-1]+'.'
    count+=1

def search(h,v):
    global H,W
    global sw
    if h==H-1 and v==W-1:
        return -2,-2
    elif v>=H-1 or h>=W-1:
        return -1,-1
    else:
        if sw[h+1][v]=='.':
            return h+1,v
        elif sw[h][v+1]=='.':
            return h,v+1
        else:
            return -1,-1

P=[[0,0]]
next=[]
i=0
plus=True
while(1):
    flag=False
    for p in P:
        for j in range(i+1):
            if p[0]+i-j>=W or p[1]+j>=H:
                continue
            result=search(p[0]+i-j,p[1]+j)
            #print('search',p[0]+i-j,p[1]+j,count)
            if result==(-2,-2):
                print(count)
                exit()
            elif result!=(-1,-1):
                next.append(result)
                flag=True
                i=0
                
    if not flag:
        #print('yaa')
        i+=1
        if plus:
            count+=1
        plus=False
    else:
        plus=True
        #print('next',next,count)
        P=[]
        P=next
        next=[]

Buggy submission (verdict: Runtime Error):
H,W = input().split()
H,W=int(H),int(W)
sw=[[0]*W]*H

for i in range(W):
    sw[i] = input()

count=0
if sw[0][0]=='#':
    sw[0]='.'+sw[0][1:]
    count+=1
if sw[H-1][W-1]=='#':
    sw[H-1]=sw[H-1][:W-1]+'.'
    count+=1

def search(h,v):
    global H,W
    global sw
    if h>=H-1 and v>=W-1:
        return -2,-2
    elif v>=H-1:
        if sw[h+1][W-1]=='.':
            return h+1,v
        else:
            return -1,-1
    elif h>=W-1:
        if sw[H-1][v+1]=='.':
            return h,v+1
        else:
            return -1,-1
    else:
        if sw[h+1][v]=='.':
            return h+1,v
        elif sw[h][v+1]=='.':
            return h,v+1
        else:
            return -1,-1

h=0
v=0
i=0
while(1):
    flag=True
    for j in range(i+1):
        result = search(h+i-j,v+j)
        if result == (-2,-2):
            print(count)
            exit()
        if result!=(-1,-1):
            i=0
            flag = False
            h,v=result
            break
    if flag:
        count+=1
        i+=1

Buggy submission (verdict: Runtime Error):
H,W = input().split()
H,W=int(H),int(W)
sw=[[0]*W]*H

for i in range(W):
    sw[i] = input()

count=0
if sw[0][0]=='#':
    sw[0]='.'+sw[0][1:]
    count+=1
if sw[H-1][W-1]=='#':
    sw[H-1]=sw[H-1][:W-1]+'.'
    count+=1

def search(h,v):
    global H,W
    global sw
    if h>=H-1 and v>=W-1:
        return -2,-2
    elif v>=H-1:
        if sw[h+1][W-1]=='.':
            return h+1,v
        else:
            return -1,-1
    elif h>=W-1:
        if sw[H-1][v+1]=='.':
            return h,v+1
        else:
            return -1,-1
    else:
        if sw[h+1][v]=='.':
            return h+1,v
        elif sw[h][v+1]=='.':
            return h,v+1
        else:
            return -1,-1

h=0
v=0
i=0
while(1):
    flag=True
    for j in range(i+1):
        result = search(h+i-j,v+j)
        if result == (-2,-2):
            exit()
        if result!=(-1,-1):
            i=0
            flag = False
            h,v=result
            break
    if flag:
        count+=1
        i+=1

Buggy submission (verdict: Runtime Error):
H,W = input().split()
H,W=int(H),int(W)
sw=[[0]*W]*H

for i in range(W):
    sw[i] = input()

count=0
if sw[0][0]=='#':
    sw[0]='.'+sw[0][1:]
    count+=1
if sw[H-1][W-1]=='#':
    sw[H-1]=sw[H-1][:W-1]+'.'
    count+=1

def search(h,v):
    global H,W
    global sw
    if h>=H-1 and v>=W-1:
        return -2,-2
    elif v>=H-1:
        if sw[h+1][W-1]=='.':
            return h+1,v
        else:
            return -1,-1
    elif h>=W-1:
        if sw[H-1][v+1]=='.':
            return h,v+1
        else:
            return -1,-1
    else:
        if sw[h+1][v]=='.':
            return h+1,v
        elif sw[h][v+1]=='.':
            return h,v+1
        else:
            return -1,-1

h=0
v=0
i=0
while(1):
    flag=True
    for j in range(i+1):
        result = search(h+i-j,v+j)
        if result == (-2,-2):
            print(count)
            exit()
        if result!=(-1,-1):
            flag = False
            h,v=result
            break
    if flag:
        count+=1
        i+=1
    else:
        i=1