Problem p02735 (python)

Fixed submission:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
inf = h*w
d=[[inf]*w for i in range(h)]
if s[0][0]=="#":
    d[0][0]=1
else:
    d[0][0]=0
for j in range(1,w):
    if s[0][j]=="#" and s[0][j-1]==".":
        d[0][j]=d[0][j-1]+1
    else:
        d[0][j]=d[0][j-1]

for i in range(1,h):
    if s[i][0]=="#" and s[i-1][0]==".":
        d[i][0]=d[i-1][0]+1
    else:
        d[i][0]=d[i-1][0]

for i in range(1,h):
    for j in range(1,w):
        if s[i][j]=="#" and s[i][j-1]==".":
            p=d[i][j-1]+1
        else:
            p=d[i][j-1]
        if s[i][j]=="#" and s[i-1][j]==".":
            q=d[i-1][j]+1
        else:
            q=d[i-1][j]
        d[i][j]=min(p,q)
# for i in d:
#     print(i)
print(d[-1][-1])

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[list(input()) for i in range(w)]
inf = h*w
d=[[inf]*h for i in range(w)]
if s[0][0]=="#":
    d[0][0]=1
else:
    d[0][0]=0
for j in range(1,h):
    if s[0][j]=="#" and s[0][j-1]==".":
        d[0][j]=d[0][j-1]+1
    else:
        d[0][j]=d[0][j-1]

for i in range(1,w):
    if s[i][0]=="#" and s[i-1][0]==".":
        d[i][0]=d[i-1][j]+1
    else:
        d[i][0]=d[i-1][0]

for i in range(1,w):
    for j in range(1,h):
        if s[i][j]=="#" and s[i][j-1]==".":
            p=d[i][j-1]+1
        else:
            p=d[i][j-1]
        if s[i][j]=="#" and s[i-1][j]==".":
            q=d[i-1][j]+1
        else:
            q=d[i-1][j]
        d[i][j]=min(p,q)
# for i in d:
#     print(i)
print(d[-1][-1])

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[list(input()) for i in range(w)]
inf = h*w
d=[[inf]*h for i in range(w)]
if s[0][0]=="#":
    d[0][0]=1
else:
    d[0][0]=0
if w>1:
    for j in range(1,w):
        if s[0][j]=="#" and s[0][j-1]==".":
            d[0][j]=d[0][j-1]+1
        else:
            d[0][j]=d[0][j-1]
if h>1:
    for i in range(1,h):
        if s[i][0]=="#" and s[i-1][0]==".":
            d[i][0]=d[i-1][j]+1
        else:
            d[i][0]=d[i-1][0]
if h>1 and w>1:
    for i in range(1,h):
        for j in range(1,w):
            if s[i][j]=="#" and s[i][j-1]==".":
                p=d[i][j-1]+1
            else:
                p=d[i][j-1]
            if s[i][j]=="#" and s[i-1][j]==".":
                q=d[i-1][j]+1
            else:
                q=d[i-1][j]
            d[i][j]=min(p,q)
# for i in d:
#     print(i)
print(d[-1][-1])

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[list(input()) for i in range(w)]
inf = h*w
d=[[inf]*h for i in range(w)]
if s[0][0]=="#":
    d[0][0]=1
else:
    d[0][0]=0
# def tate(x,y):
#     if x==0:
#         return d[x][y]
#     if s[x-1][y]=="#":
#         return tate(x-1,y)
#     return d[x-1][y]
# def yoko(x,y):
#     if y==0:
#         return d[x][y]
#     if s[x][y-1]=="#":
#         return yoko(x,y-1)
#     return d[x][y-1]
for j in range(1,w):
    if s[0][j]=="#" and s[0][j-1]==".":
        d[0][j]=d[0][j-1]+1
    else:
        d[0][j]=d[0][j-1]
for i in range(1,h):
    if s[i][0]=="#" and s[i-1][0]==".":
        d[i][0]=d[i-1][j]+1
    else:
        d[i][0]=d[i-1][0]
for i in range(1,h):
    for j in range(1,w):
        if s[i][j]=="#" and s[i][j-1]==".":
            p=d[i][j-1]+1
        else:
            p=d[i][j-1]
        if s[i][j]=="#" and s[i-1][j]==".":
            q=d[i-1][j]+1
        else:
            q=d[i-1][j]
        d[i][j]=min(p,q)
# for i in d:
#     print(i)
print(d[-1][-1])

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[list(input()) for i in range(w)]
inf = h*w
d=[[inf]*h for i in range(w)]
if s[0][0]=="#":
    d[0][0]=1
else:
    d[0][0]=0
def tate(x,y):
    if x==0:
        return d[x][y]
    if s[x-1][y]=="#":
        return tate(x-1,y)
    return d[x-1][y]
def yoko(x,y):
    if y==0:
        return d[x][y]
    if s[x][y-1]=="#":
        return tate(x,y-1)
    return d[x][y-1]
for j in range(1,w):
    if s[0][j]=="#":
        d[0][j]=yoko(0,j)+1
    else:
        d[0][j]=d[0][j-1]
for i in range(1,h):
    if s[i][0]=="#":
        d[i][0]=tate(i,0)+1
    else:
        d[i][0]=d[i-1][0]
for i in range(1,h):
    for j in range(1,w):
        if s[i][j]=="#":
            d[i][j]=min(tate(i,j),yoko(i,j))+1
        else:
            d[i][j]=min(d[i-1][j],d[i][j-1])
print(d[-1][-1])