Problem p03436 (python)

Fixed submission:
import numpy as np
import scipy as sp
import scipy.sparse.csgraph as sg

H, W = [int(a) for a in input().split()]
ss = []
for _ in range(H):
    ss.append(input())

g = np.zeros((H*W,H*W))

v_edges = [((i,j), (i,j+1)) for i in range(H) for j in range(W-1) if ss[i][j] == "." and ss[i][j+1] == "."]
h_edges = [((i,j), (i+1,j)) for i in range(H-1) for j in range(W) if ss[i][j] == "." and ss[i+1][j] == "."]

for (si,sj), (di,dj) in v_edges + h_edges:
    g[si*W+sj][di*W+dj] = 1
    g[di*W+dj][si*W+sj] = 1

r = sg.dijkstra(g, indices=[0,H*W-1])
score = r[0,H*W-1]

if sp.isinf(score):
    print(-1)
    exit(0)
else:
    #print(r)
    #print(ss) 

    ss_ = "".join(ss)
    num = len([0 for c in ss_ if c == '.'])
    #print(num)
    #print(score)
    print("%d" % (num - (score+1)))

Buggy submission (verdict: Runtime Error):
import numpy as np
import scipy.sparse.csgraph as sg

H, W = [int(a) for a in input().split()]
ss = []
for _ in range(H):
    ss.append(input())

g = np.zeros((H*W,H*W))

v_edges = [((i,j), (i,j+1)) for i in range(H) for j in range(W-1) if ss[i][j] == "." and ss[i][j+1] == "."]
h_edges = [((i,j), (i+1,j)) for i in range(H-1) for j in range(W) if ss[i][j] == "." and ss[i+1][j] == "."]

for (si,sj), (di,dj) in v_edges + h_edges:
    g[si*W+sj][di*W+dj] = 1
    g[di*W+dj][si*W+sj] = 1

r = sg.dijkstra(g)
score = r[0,H*W-1]

if np.isinf(score):
    print(-1)
    exit(1)

#print(r)
#print(ss) 

ss_ = "".join(ss)
num = len([0 for c in ss_ if c == '.'])
#print(num)
#print(score)
print("%d" % (num - (score+1)))

Buggy submission (verdict: Runtime Error):
import numpy as np
import scipy.sparse.csgraph as sg

H, W = [int(a) for a in input().split()]
ss = []
for _ in range(H):
    ss.append(input())

g = np.zeros((H*W,H*W))

v_edges = [((i,j), (i,j+1)) for i in range(H) for j in range(W-1) if ss[i][j] == "." and ss[i][j+1] == "."]
h_edges = [((i,j), (i+1,j)) for i in range(H-1) for j in range(W) if ss[i][j] == "." and ss[i+1][j] == "."]

for (si,sj), (di,dj) in v_edges + h_edges:
    g[si*W+sj][di*W+dj] = 1
    g[di*W+dj][si*W+sj] = 1

r = sg.dijkstra(g)
score = r[0,H*W-1]

#print(r)
#print(ss) 

ss_ = "".join(ss)
num = len([0 for c in ss_ if c == '.'])
#print(num)
#print(score)
print("%d" % (num - (score+1)))

Buggy submission (verdict: Runtime Error):
import numpy as np
import networkx as nx

H, W = [int(a) for a in input().split()]
ss = []
for _ in range(H):
    ss.append(input())

ss = np.array(ss)

g = nx.Graph()
g.add_nodes_from([("%d_%d" % (i,j), {'color':ss[i][j]}) for i in range(H) for j in range(W)])
g.add_edges_from([("%d_%d" % (i,j), "%d_%d" % (i,j+1), {'weight':1}) for i in range(H) for j in range(W-1)])
g.add_edges_from([("%d_%d" % (i,j), "%d_%d" % (i+1,j), {'weight':1}) for i in range(H-1) for j in range(W)])

#print(g.nodes(data=True))
#print(g.edges(data=True))


if nx.algorithms.shortest_paths.generic.has_path(g, source="0_0", target="%d_%d" % (H-1,W-1)) == False:
    print(-1)
else:
    path = nx.algorithms.shortest_paths.generic.shortest_path(g, source="0_0", target="%d_%d" % (H-1,W-1), weight='weight')
    ss_ = "".join(ss)
    num = len([0 for c in ss_ if c == '.'])
    #print(num)
    #print(path)
    print(num - len(path))