Problem p03053 (python)

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque

H, W = map(int, input().rstrip().split(' '))

field = [[0] * W for _ in range(H)]
d = deque()
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '#':
            field[i][j] = 1
            d.append([i, j, 0])

max_dist = 0
while len(d) > 0:
    cur_h, cur_w, dist = d.popleft()
    for i, (dh, dw) in enumerate([[0, 1], [0, -1], [1, 0], [-1, 0]]):
        if (cur_h + dh) >= 0 and (cur_h + dh) < H and (cur_w + dw) >= 0 and (cur_w + dw) < W:
            if field[cur_h+dh][cur_w+dw] == 0:
                field[cur_h+dh][cur_w+dw] = 1
                d.append([cur_h+dh, cur_w+dw, dist+1])
                max_dist = max(max_dist, dist+1)

print(max_dist)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10000)

H, W = map(int, input().rstrip().split(' '))

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)
    
dp = [[0] * W for _ in range(H)]

def rec(i, j):
    if field[i][j] == 1:
        return 0
    
    if dp[i][j] != 0:
        return dp[i][j]
        
    dp[i][j] = H + W
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, rec(i-1, j) + 1)
    if i < H - 1:
        min_dist = min(min_dist, rec(i+1, j) + 1)
    if j > 0:
        min_dist = min(min_dist, rec(i, j-1) + 1)
    if j < W - 1:
        min_dist = min(min_dist, rec(i, j+1) + 1)
        
    dp[i][j] = min_dist
    
    return min_dist
    
    
for i in range(H):
    for j in range(W):
        rec(i, j)
     
print(max([max(temp_dp) for temp_dp in dp]))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10000)

H, W = map(int, input().rstrip().split(' '))

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)
    
dp = [[-1] * W for _ in range(H)]

def rec(i, j):
    if field[i][j] == 1:
        return 0
    
    if dp[i][j] != -1:
        return dp[i][j]
        
    dp[i][j] = H + W
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, rec(i-1, j) + 1)
    if i < H - 1:
        min_dist = min(min_dist, rec(i+1, j) + 1)
    if j > 0:
        min_dist = min(min_dist, rec(i, j-1) + 1)
    if j < W - 1:
        min_dist = min(min_dist, rec(i, j+1) + 1)
        
    dp[i][j] = min_dist
    
    return min_dist
    
    
for i in range(H):
    for j in range(W):
        rec(i, j)
     
print(max([max(temp_dp) for temp_dp in dp]))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

H, W = map(int, input().rstrip().split(' '))

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)
    
dp = [[-1] * W for _ in range(H)]

def rec(i, j):
    if field[i][j] == 1:
        return 0
    
    if dp[i][j] != -1:
        return dp[i][j]
        
    dp[i][j] = H + W
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, rec(i-1, j) + 1)
    if i < H - 1:
        min_dist = min(min_dist, rec(i+1, j) + 1)
    if j > 0:
        min_dist = min(min_dist, rec(i, j-1) + 1)
    if j < W - 1:
        min_dist = min(min_dist, rec(i, j+1) + 1)
        
    dp[i][j] = min_dist
    
    return min_dist
    
    
for i in range(H):
    for j in range(W):
        rec(i, j)
     
print(max([max(temp_dp) for temp_dp in dp]))