Problem p03436 (python)

Fixed submission:
import queue
H,W = map(int,input().split())
maze = [0]*H
for i in range(H):
    maze[i] = input()
inf = float('inf')
d = [[inf for i in range(W)] for i in range(H)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]


def bfs():
    que = queue.Queue()
    que.put((0,0))
    d[0][0] = 0
    while que.qsize():
        p = que.get()
        if p == (H-1,W-1):
            break
        for i in range(4):
            nx = p[0]+dx[i]
            ny = p[1]+dy[i]
            if 0 <= nx and nx <= H-1 and 0 <= ny and ny <= W-1 \
            and maze[nx][ny] != '#' and d[nx][ny] == inf:
                d[nx][ny] = d[p[0]][p[1]] + 1
                que.put((nx,ny))

bfs()

count = 0
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            count += 1

ans = -1
if d[H-1][W-1] != inf:
    ans = H*W-count-1-d[H-1][W-1]
print(ans)

Buggy submission (verdict: Runtime Error):
INF = float('inf')
H,W = map(int,input().split())
s = [input() for _ in range(H)]
cost = [[INF]*W for _ in range(H)]
cost[0][0] = 1

def bfs(h,w):
    for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
        if 0<=h+dh<H and 0<=w+dw<W:
            if s[h+dh][w+dw] == '.' and cost[h+dh][w+dw] > cost[h][w]+1:
                cost[h+dh][w+dw] = cost[h][w]+1
                bfs(h+dh,w+dw)

bfs(0,0)

count = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            count += 1
 
ans = -1
if cost[H-1][W-1] != INF:
    ans = H*W-count-cost[-1][-1]
print(ans)

Buggy submission (verdict: Runtime Error):
INF = float('inf')
H,W = map(int,input().split())
s = [input() for _ in range(H)]
cost = [[INF]*W for _ in range(H)]
cost[0][0] = 1

def bfs(h,w):
    for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
        if 0<=h+dh<H and 0<=w+dw<W:
            if s[h+dh][w+dw] == '.' and cost[h+dh][w+dw] > cost[h][w]+1:
                cost[h+dh][w+dw] = cost[h][w]+1
                bfs(h+dh,w+dw)

bfs(0,0)

count = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            count += 1
 
ans = -1
if cost[H-1][W-1] != INF:
    ans = H*W-count-cost[]-1][-1]
print(ans)

Buggy submission (verdict: Runtime Error):
INF = float('inf')
H,W = map(int,input().split())
s = [input() for _ in range(H)]
cost = [[INF]*W for _ in range(H)]
cost[0][0] = 1

def bfs(h,w):
    for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
        if 0<=h+dh<H and 0<=w+dw<W:
            if s[h+dh][w+dw] == '.' and cost[h+dh][w+dw] > cost[h][w]+1:
                cost[h+dh][w+dw] = cost[h][w]+1
                bfs(h+dh,w+dw)

bfs(0,0)

count = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            count += 1
 
ans = -1
if d[H-1][W-1] != INF:
    ans = H*W-count-d[-1][-1]
print(ans)

Buggy submission (verdict: Runtime Error):
INF = float('inf')
H,W = map(int,input().split())
s = [input() for _ in range(H)]
cost = [[INF]*W for _ in range(H)]
cost[0][0] = 1

def bfs(h,w):
    for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
        if 0<=h+dh<H and 0<=w+dw<W:
            if s[h+dh][w+dw] == '.' and cost[h+dh][w+dw] > cost[h][w]+1:
                cost[h+dh][w+dw] = cost[h][w]+1
                bfs(h+dh,w+dw)

bfs(0,0)
if cost[-1][-1] == INF:
    print(-1)
else:
    ans = H*W-cost[-1][-1]
    for h in range(H):
        ans -= s[h].count('#')
    print(ans)

Buggy submission (verdict: Runtime Error):
INF = float('inf')
H,W = map(int,input().split())
s = [input() for _ in range(H)]
cost = [[INF]*W for _ in range(H)]
cost[0][0] = 1

def bfs(h,w):
    if 0<=h<H and 0<=w<W:
        for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
            if 0<=h+dh<H and 0<=w+dw<W:
                if s[h+dh][w+dw] == '.' and cost[h+dh][w+dw] > cost[h][w]+1:
                    cost[h+dh][w+dw] = cost[h][w]+1
                    return bfs(h+dh,w+dw)

bfs(0,0)
if cost[-1][-1] == INF:
    print(-1)
else:
    ans = H*W-cost[-1][-1]
    for h in range(H):
        ans -= s[h].count('#')
    print(ans)

Buggy submission (verdict: Runtime Error):
INF = float('inf')
H,W = map(int,input().split())
s = [input() for _ in range(H)]
cost = [[INF]*W for _ in range(H)]
cost[0][0] = 1

def bfs(h,w):
    for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
        if 0<=h+dh<H and 0<=w+dw<W:
            if s[h+dh][w+dw] == '.' and cost[h+dh][w+dw] == INF:
                cost[h+dh][w+dw] = cost[h][w]+1
                return bfs(h+dh,w+dw)

bfs(0,0)
if cost[-1][-1] == INF:
    print(-1)
else:
    ans = H*W-cost[-1][-1]
    for h in range(H):
        ans -= s[h].count('#')
    print(ans)

Buggy submission (verdict: Runtime Error):
INF = float('inf')
H,W = map(int,input().split())
s = [input() for _ in range(H)]
cost = [[INF]*W for _ in range(H)]
cost[0][0] = 1

def bfs(h,w):
    for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
        if 0<=h+dh<H and 0<=w+dw<W:
            if s[h+dh][w+dw] == '.' and cost[h+dh][w+dw] > cost[h][w]+1:
                cost[h+dh][w+dw] = cost[h][w]+1
                bfs(h+dh,w+dw)
bfs(0,0)
if cost[-1][-1] == INF:
    print(-1)
else:
    ans = H*W-cost[-1][-1]
    for h in range(H):
        ans -= s[h].count('#')
    print(ans)

Buggy submission (verdict: Runtime Error):
INF = float('inf')
H,W = map(int,input().split())
s = [input() for _ in range(H)]

cost = [[INF]*W for _ in range(H)]
cost[0][0] = 1

def bfs(h,w):
    if h-1>=0:
        if s[h-1][w] == '.' and cost[h-1][w] > cost[h][w]+1:
            cost[h-1][w] = cost[h][w]+1
            bfs(h-1,w)
    if h+1<H:
        if s[h+1][w] == '.' and cost[h+1][w] > cost[h][w]+1:
            cost[h+1][w] = cost[h][w]+1
            bfs(h+1,w)
    if w-1>=0:
        if s[h][w-1] == '.' and cost[h][w-1] > cost[h][w]+1:
            cost[h][w-1] = cost[h][w]+1
            bfs(h,w-1)
    if w+1<W:
        if s[h][w+1] == '.' and cost[h][w+1] > cost[h][w]+1:
            cost[h][w+1] = cost[h][w]+1
            bfs(h,w+1)

bfs(0,0)

if cost[-1][-1] == INF:
    print(-1)
else:
    ans = H*W-cost[-1][-1]
    for h in range(H):
        ans -= s[h].count('#')
    print(ans)