Problem p02735 (python)

Fixed submission:
def ans_dp(H,W,M):
    dp = [[0 for i in range(W)] for j in range(H)]
    flag = [[0 for i in range(W)] for j in range(H)]
    if M[0][0] == "#":
        dp[0][0] = 1
        flag[0][0] = 1
    else:
        dp[0][0] == 0
    for i in range(1,W):
        if M[0][i] == "#":
            flag[0][i] = 1
            if flag[0][i-1] == 1:
                dp[0][i] = dp[0][i-1]
            else:
                dp[0][i] = dp[0][i-1] + 1 
        else:##M[0][i] = "."
            dp[0][i] = dp[0][i-1]
    for j in range(1,H):
        if M[j][0] == "#":
            flag[j][0] = 1
            if flag[j-1][0] == 1:
                dp[j][0] = dp[j-1][0]
            else:
                dp[j][0] = dp[j-1][0] + 1 
        else:##M[0][i] = "."
            dp[j][0] = dp[j-1][0]
    
    for i in range(1,H):
        for j in range(1,W):
            if M[i][j] == "#":
                flag[i][j] = 1
                dp[i][j] = min(dp[i-1][j]+int(not flag[i-1][j]),dp[i][j-1]+int(not flag[i][j-1]))
            else:#M[i][j] == "."
                dp[i][j] = min(dp[i-1][j],dp[i][j-1])
    """
    for i in range(H):
        print(dp[i])
    for i in range(H):
        print(flag[i])
    """
    return dp[H-1][W-1]

def main():
    H,W = map(int,input().split())
    M = []
    for i in range(H):
        M.append(list(input()))
    print(ans_dp(H,W,M))
 
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def ans_dp(H,W,M):
    dp = [[0 for i in range(H)] for j in range(W)]
    flag = [[0 for i in range(H)] for j in range(W)]
    if M[0][0] == "#":
        dp[0][0] = 1
        flag[0][0] = 1
    else:
        dp[0][0] == 0
    for i in range(1,W):
        if M[0][i] == "#":
            flag[0][i] = 1
            if flag[0][i-1] == 1:
                dp[0][i] = dp[0][i-1]
            else:
                dp[0][i] = dp[0][i-1] + 1 
        else:##M[0][i] = "."
            dp[0][i] = dp[0][i-1]
    for j in range(1,H):
        if M[j][0] == "#":
            flag[j][0] = 1
            if flag[j-1][0] == 1:
                dp[j][0] = dp[j-1][0]
            else:
                dp[j][0] = dp[j-1][0] + 1 
        else:##M[0][i] = "."
            dp[j][0] = dp[j-1][0]
    
    for i in range(1,H):
        for j in range(1,W):
            if M[i][j] == "#":
                flag[i][j] = 1
                dp[i][j] = min(dp[i-1][j]+int(not flag[i-1][j]),dp[i][j-1]+int(not flag[i][j-1]))
            else:#M[i][j] == "."
                dp[i][j] = min(dp[i-1][j],dp[i][j-1])
    """
    for i in range(H):
        print(dp[i])
    for i in range(H):
        print(flag[i])
    """
    return dp[W-1][H-1]

def main():
    H,W = map(int,input().split())
    M = []
    for i in range(H):
        M.append(list(input()))
    print(ans_dp(H,W,M))
 
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def ans_dp(H,W,M):
    dp = [[0 for i in range(W+1)] for j in range(H+1)]
    flag = [[0 for i in range(W+1)] for j in range(H+1)]
    if M[0][0] == "#":
        dp[0][0] = 1
        flag[0][0] = 1
    else:
        dp[0][0] == 0
    for i in range(1,W):
        if M[i][0] == "#":
            flag[i][0] = 1
            if flag[i-1][0] == 1:
                dp[i][0] = dp[i-1][0]
            else:
                dp[i][0] = dp[i-1][0] + 1 
        else:##M[0][i] = "."
            dp[i][0] = dp[i-1][0]
    for j in range(1,H):
        if M[0][j] == "#":
            flag[0][j] = 1
            if flag[0][j-1] == 1:
                dp[0][j] = dp[0][j-1]
            else:
                dp[0][j] = dp[0][j-1] + 1 
        else:##M[0][i] = "."
            dp[0][j] = dp[0][j-1]
    
    for i in range(1,W):
        for j in range(1,H):
            if M[i][j] == "#":
                flag[i][j] = 1
                dp[i][j] = min(dp[i-1][j]+int(not flag[i-1][j]),dp[i][j-1]+int(not flag[i][j-1]))
            else:#M[i][j] == "."
                dp[i][j] = min(dp[i-1][j],dp[i][j-1])
    """
    for i in range(H):
        print(dp[i])
    for i in range(H):
        print(flag[i])
    """
    return dp[W-1][H-1]

def main():
    H,W = map(int,input().split())
    M = []
    for i in range(H):
        M.append(list(input()))
    print(ans_dp(H,W,M))
 
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):

def ans_dp(H,W,M):
    dp = [[0 for i in range(W)] for j in range(H)]
    flag = [[0 for i in range(W)] for j in range(H)]
    if M[0][0] == "#":
        dp[0][0] = 1
        flag[0][0] = 1
    else:
        dp[0][0] == 0
    for i in range(1,W):
        if M[i][0] == "#":
            flag[i][0] = 1
            if flag[i-1][0] == 1:
                dp[i][0] = dp[i-1][0]
            else:
                dp[i][0] = dp[i-1][0] + 1 
        else:##M[0][i] = "."
            dp[i][0] = dp[i-1][0]
    for j in range(1,H):
        if M[0][j] == "#":
            flag[0][j] = 1
            if flag[0][j-1] == 1:
                dp[0][j] = dp[0][j-1]
            else:
                dp[0][j] = dp[0][j-1] + 1 
        else:##M[0][i] = "."
            dp[0][j] = dp[0][j-1]
    
    for i in range(1,W):
        for j in range(1,H):
            if M[i][j] == "#":
                flag[i][j] = 1
                if flag[i-1][j] == 1 or flag[i][j-1] == 1:
                    dp[i][j] = min(dp[i-1][j],dp[i][j-1])
                else:
                    dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + 1
            else:#M[i][j] == "."
                dp[i][j] = min(dp[i-1][j],dp[i][j-1])
    """
    for i in range(H):
        print(dp[i])
    for i in range(H):
        print(flag[i])
    """
    return dp[-1][-1]

def main():
    H,W = map(int,input().split())
    M = []
    for i in range(H):
        M.append(list(input()))
    print(ans_dp(H,W,M))
 
if __name__ == "__main__":
    main()