Problem p02735 (python)

Fixed submission:
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0,0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i in range(2):
      p1 = p[0]+i, p[1]+(1-i)
      if not (p1[0]<H and p1[1]<W) or p1 in fixed:
        continue
      x, y = p
      x1, y1 = p1
      heappush(candicates, (dist+(grid[p1] & ~grid[p]), p1))
  return None

H, W = map(int, input().split())
grid = {(i,j): int(c=="#") for i in range(H) for j, c in enumerate(input())}

print(solve(H, W, grid))

Buggy submission (verdict: Runtime Error):
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0,0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i range(2):
      p1 = p[0]+i, p[1]+(1-i)
      if not (p1[0]<H and p1[1]<W) or p1 in fixed:
        continue
      x, y = p
      x1, y1 = p1
      heappush(candicates, (dist+(grid[p1] & ~grid[p]), p1))
  return None

H, W = map(int, input().split())
grid = {(i,j): int(c=="#") for i in range(H) for j, c in enumerate(input())}

print(solve(H, W, grid))

Buggy submission (verdict: Runtime Error):
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0][0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i,j in product([1,-1], [0,1]):
      p1 = p[0]+i*j, p[1]+i*(1-j)
      if not (0<=p1[0]<H and 0<=p1[1]<W) or p1 in fixed:
        continue
      x, y = p
      x1, y1 = p1
      heappush(candicates, (dist+(grid[y1][x1] & ~grid[y][x]), p1))
  return None

H, W = map(int, input().split())
grid = [list(map([".", "#"].index, input())) for i in range(H)]

print(solve(H, W, grid))

Buggy submission (verdict: Runtime Error):
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0][0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i,j in product([1,-1], [0,1]):
      y1, x1 = p[0]+i*j, p[1]+i*(1-j)
      if not (0<=y<H and 0<=x<W) or (y, x) in fixed:
        continue
      x0, y0 = p
      heappush(candicates, (dist+(grid[y][x] & ~grid[y0][x0]), (y, x)))
  return None

H, W = map(int, input().split())
grid = [list(map([".", "#"].index, input())) for i in range(H)]
print(solve(H, W, grid))

Buggy submission (verdict: Runtime Error):
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i,j in product([1,-1], [0,1]):
      y, x = p[0]+i*j, p[1]+i*(1-j)
      if not (0<=y<H and 0<=x<W) or (y, x) in fixed:
        continue
      heappush(candicates, (dist+(1-grid[p])*grid[W*y+x], (y, x)))
  return None

H, W = map(int, input().split())
grid = list(chain.from_iterable(map([".", "#"].index, input()) for i in range(H)))
print(solve(H, W, grid))

Buggy submission (verdict: Runtime Error):
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i,j in product([1,-1], [0,1]):
      y, x = p[0]+i*j, p[1]+i*(1-j)
      if not (0<=y<H and 0<=x<W) or (y, x) in fixed:
        continue
      heappush(candicates, dist+(1-grid[p])*grid[W*y+x], (y, x)))
  return None

H, W = map(int, input().split())
grid = list(chain.from_iterable(map([".", "#"].index, input()) for i in range(H)))
print(solve(H, W, grid))

Buggy submission (verdict: Runtime Error):
from itertools import product
from heapq import heappop, heappush

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i,j in product([1,-1], [0,1]):
      y, x = p[0]+i*j, p[1]+i*(1-j)
      if not (0<=y<H and 0<=x<W) or (y,x) in fixed:
        continue
      heappush(candicates, (dist+grid[W*y+x], (y, x)))
  return None

H, W = map(int, input().split())
grid = list(chain.from_iterable(map([".", "#"].index, input()) for i in range(H)))
print(solve(H, W, grid))