Problem: Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left. Each square is painted black or white.  The grid is said to be _good_ if and only if the following condition is satisfied:    * From (1, 1), we can reach (H, W) by moving one square **right or down** repeatedly, while always being on a white square.  Note that (1, 1) and (H, W) must be white if the grid is good.  Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.    * Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) \- that is, from white to black and vice versa.

Submission attempts (in order):
import heapq
h, w = map(int, input().split())
c = [input() for _ in range(h)]
 
d=[[100000]*w for _ in range(h)]
 
def bfs():
    if c[0][0] == '.':
        que = [(0, (0, 0))]
        heapq.heapify(que)
        d[0][0] = 0
    else:
        que = [(1, (0, 0))]
        heapq.heapify(que)
        d[0][0] = 1
    while len(que):
        _, (qx, qy) = heapq.heappop(que)
        if qx == h-1 and qy == w-1:
            return d[h-1][w-1]
        for dx,dy in [[1,0], [0,1]]:
            px, py = qx+dx, qy+dy
            if 0<=px<h and 0<=py<w:
                d[px][py]=min(d[px][py], d[qx][qy] + (c[px][py]=='#'))
                heapq.heappush(que, (d[px][py], (px , py)))

print(bfs())
print(d)

import sys
from heapq import heappop, heappush
sys.setrecursionlimit(10000000)

h, w = map(int, input().split())
c = [input() for _ in range(h)]
d = [[10000]*w for _ in range(h)]

que = []
if c[0][0] == '.':
    heappush(que, (0, 0, 0, True))
    d[0][0] = 0
else:
    heappush(que, (1, 0, 0, False))
    d[0][0] = 1

while len(que):
    dist, px, py, flg = heappop(que)
    if d[h-1][w-1] < dist:
      break
    for dx, dy in [(0, 1), (1, 0)]:
        qx, qy = px + dx, py + dy
        if not (0 <= qx < h and 0 <= qy < w):
          continue
        if d[qx][qy] > dist + (flg and c[qx][qy]=='#'):
            if flg:
              d[qx][qy] = min(d[qx][qy], dist + (c[qx][qy]=='#'))
              heappush(que, (d[qx][qy], qx, qy, False))
            else:
              d[qx][qy] = min(d[qx][qy], dist)
              heappush(que, (d[qx][qy], qx, qy, True))

print(d[h-1][w-1])

import sys
from heapq import heappop, heappush
sys.setrecursionlimit(10000000)

h, w = map(int, input().split())
c = [input() for _ in range(h)]
d = [[10000]*w for _ in range(h)]

que = []
if c[0][0] == '.':
    heappush(que, (0, 0, 0))
    d[0][0] = 0
else:
    heappush(que, (1, 0, 0))
    d[0][0] = 1

while len(que):
    dist, px, py = heappop(que)
    if d[h-1][w-1] < dist:
        break
    for dx, dy in [(0, 1), (1, 0)]:
        qx, qy = px + dx, py + dy
        if not (0 <= qx < h and 0 <= qy < w):
            continue
        if d[qx][qy] > dist:
            if c[px][py]=='.' and c[qx][qy]=='#':
                d[qx][qy] = dist + 1
                heappush(que, (d[qx][qy], qx, qy))
            else:
                d[qx][qy] = dist
                heappush(que, (d[qx][qy], qx, qy))
print(d[h-1][w-1])

Verdicts: Wrong Answer

Wrong Answer

Accepted