Problem p02735 (python)

Fixed submission:
import sys
def MI(): return map(int, sys.stdin.readline().split())

H,W = MI()
s = [list(input()) for _ in range(H)]
dp = [[0 for _ in range(W)] for _ in range(H)]

if s[0][0]=='#':
	dp[0][0] = 1

for i in range(W-1):
	if s[0][i+1]=='#' and s[0][i]=='.':
		dp[0][i+1] = dp[0][i] + 1
	else:
		dp[0][i+1] = dp[0][i]

for i in range(H-1):
	if s[i+1][0]=='#' and s[i][0]=='.':
		dp[i+1][0] = dp[i][0] + 1
	else:
		dp[i+1][0] = dp[i][0]

def next(start, goal, start_value):
	if start=='.' and goal=='#':
		start_value += 1
	return start_value

for i in range(1,H):
	for j in range(1,W):
		# print(next(s[i][j-1],s[i][j],dp[i][j-1]),next(s[i-1][j],s[i][j],dp[i-1][j]))
		dp[i][j] = min(next(s[i][j-1],s[i][j],dp[i][j-1]), next(s[i-1][j],s[i][j],dp[i-1][j]))

print(dp[H-1][W-1])

Buggy submission (verdict: Runtime Error):
import sys
def MI(): return map(int, sys.stdin.readline().split())

H,W = MI()
s = [list(input()) for _ in range(H)]
dp = [[0 for _ in range(W)] for _ in range(H)]

if s[0][0]=='#':
	dp[0][0] = 1

for i in range(W-1):
	if s[0][i+1]=='#' and s[0][i]=='.':
		dp[0][i+1] = dp[0][i] + 1
	else:
		dp[0][i+1] = dp[0][i]

for i in range(H-1):
	if s[i+1][0]=='#' and s[i][0]=='.':
		dp[i+1][0] = dp[i][0] + 1
	else:
		dp[i+1][0] = dp[i][0]

def next(start, goal, start_value):
	if start=='.' and goal=='#':
		start_value += 1
	return start_value

for i in range(1,W):
	for j in range(1,H):
		# print(next(s[i][j-1],s[i][j],dp[i][j-1]),next(s[i-1][j],s[i][j],dp[i-1][j]))
		dp[i][j] = min(next(s[i][j-1],s[i][j],dp[i][j-1]), next(s[i-1][j],s[i][j],dp[i-1][j]))

Buggy submission (verdict: Runtime Error):
import sys
def MI(): return map(int, sys.stdin.readline().split())

H,W = MI()
s = [list(input()) for _ in range(H)]
dp = [[0 for _ in range(W)] for _ in range(H)]

if s[0][0]=='#':
	dp[0][0] = 1

for i in range(W-1):
	if s[0][i+1]=='#' and s[0][i]=='.':
		dp[0][i+1] = dp[0][i] + 1
	else:
		dp[0][i+1] = dp[0][i]

for i in range(H-1):
	if s[i+1][0]=='#' and s[i][0]=='.':
		dp[i+1][0] = dp[i][0] + 1
	else:
		dp[i+1][0] = dp[i][0]

if H==1 or W==1:
	print(dp[W-1][H-1])
	sys.exit()
# print(dp)

def next(start, goal, start_value):
	if start=='.' and goal=='#':
		start_value += 1
	return start_value

for i in range(1,W):
	for j in range(1,W):
		# print(next(s[i][j-1],s[i][j],dp[i][j-1]),next(s[i-1][j],s[i][j],dp[i-1][j]))
		dp[i][j] = min(next(s[i][j-1],s[i][j],dp[i][j-1]),next(s[i-1][j],s[i][j],dp[i-1][j]))
print(dp[W-1][H-1])

Buggy submission (verdict: Runtime Error):
import sys
def MI(): return map(int, sys.stdin.readline().split())

H,W = MI()
s = [list(input()) for _ in range(H)]
dp = [[0 for _ in range(W)] for _ in range(H)]

if s[0][0]=='#':
	dp[0][0] = 1

for i in range(W-1):
	if s[0][i+1]=='#' and s[0][i]=='.':
		dp[0][i+1] = dp[0][i] + 1
	else:
		dp[0][i+1] = dp[0][i]

for i in range(H-1):
	if s[i+1][0]=='#' and s[i][0]=='.':
		dp[i+1][0] = dp[i][0] + 1
	else:
		dp[i+1][0] = dp[i][0]

# print(dp)

def next(start, goal, start_value):
	if start=='.' and goal=='#':
		start_value += 1
	return start_value

for i in range(1,W):
	for j in range(1,W):
		# print(next(s[i][j-1],s[i][j],dp[i][j-1]),next(s[i-1][j],s[i][j],dp[i-1][j]))
		dp[i][j] = min(next(s[i][j-1],s[i][j],dp[i][j-1]),next(s[i-1][j],s[i][j],dp[i-1][j]))
print(dp[W-1][H-1])