TnT/Multi_CodeNet4Repair huggingface dataset:
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):
H, W = map(int, input().split())
S = [input() for _ in range(H)]

dp = [[0 for _ in range(W)] for _ in range(H)]

if S[0][0] == '#':
    dp[0][0] = 1

for i in range(1, W):
    if S[0][i] == '#':
        dp[0][i] = dp[0][i-1] + 1
    else:
        dp[0][i] = dp[0][i-1]

for j in range(1, H):
    if S[j][0] == '#':
        dp[j][0] = dp[j-1][0] + 1
    else:
        dp[j][0] = dp[j-1][0]

for j in range(1, H):
    for i in range(1, W):
        v = min(dp[j-1][i], dp[j][i-1])
        if S[j][i] == '#':
            dp[j][i] = v + 1
        else:
            dp[j][i] = v

print(dp[H-1][W-1]-1)


H, W = map(int, input().split())
S = [input() for _ in range(H)]

dp = [[float('inf') for _ in range(W)] for _ in range(H)]

if S[0][0] == '#':
    dp[0][0] = 1
else:
    dp[0][0] = 0

for i in range(1, W):
    if S[0][i] == '.':
        dp[0][i] = dp[0][i-1]
    else:
        if S[0][i-1] == '.':
            dp[0][i] = dp[0][i-1] + 1
        else:
            dp[0][i] = dp[0][i-1]

for j in range(1, H):
    if S[j][0] == '.':
        dp[j][0] = dp[j-1][0]
    else:
        if S[j-1][0] == '.':
            dp[j][0] = dp[j-1][0] + 1
        else:
            dp[j][0] = dp[j-1][0]

for j in range(1, H):
    for i in range(1, W):
        if S[j][i-1] == '.' and S[j][i] == '#':
            dp[j][i] = min(dp[j][i], dp[j][i-1] + 1)
        else:
            dp[j][i] = min(dp[j][i], dp[j][i-1])
        if S[j-1][i] == '.' and S[j][i] == '#':
            dp[j][i] = min(dp[j][i], dp[j-1][i] + 1)
        else:
            dp[j][i] = min(dp[j][i], dp[j-1][i])

print(dp[H-1][W-1])


Verdicts: Wrong Answer

Accepted
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):
H, W = map(int,input().split())
S = [input() for i in range(H)]
dp = [[0 for _ in range(W)] for _ in range(H)]
dp[0][0] = 1 if S[0][0] == "#" else 0

for i in range(1,H):
    for j in range(1,W):
        up = dp[i-1][j] + (1 if S[i-1][j] != "#" and S[i][j] == "#" else 0) 
        left = dp[i][j-1] + (1 if S[i][j-1] != "#" and S[i][j] == "#" else 0)
        dp[i][j] = min(up, left)
print(dp[-1][-1])

from collections import deque

H, W = map(int,input().split())
S = [input() for i in range(H)]
dp = [[0 for _ in range(W)] for _ in range(H)]
dp[0][0] = 1 if S[0][0] == "#" else 0
for i in range(1, H):
    if S[i][0] == "#":
        if S[i-1][0] == "#":
            dp[i][0] =  dp[i-1][0]
        else:
             dp[i][0] = dp[i-1][0] + 1
    else:
        dp[i][0] =  dp[i-1][0]

for j in range(1, W):
    if S[0][j] == "#":
        if S[0][j-1] == "#":
            dp[0][j] =  dp[0][j-1]
        else:
             dp[0][j] = dp[0][j-1] + 1
    else:
        dp[0][j] =  dp[0][j-1]

for i in range(1,H):
    for j in range(1,W):
        up = dp[i-1][j] + (1 if S[i-1][j] != "#" and S[i][j] == "#" else 0) 
        left = dp[i][j-1] + (1 if S[i][j-1] != "#" and S[i][j] == "#" else 0)
        dp[i][j] = min(up, left)
print(dp[-1][-1])

Verdicts: Wrong Answer

Accepted
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):
H, W = map(int, input().split())
s = [input() for i in range(H)]

dp = [[0] * W for i in range(H)]

for i in range(H):
    for j in range(W):
        c = []
        if 0 <= i - 1 < H:
            c.append(dp[i - 1][j])
        if 0 <= j - 1 < W:
            c.append(dp[i][j - 1])
        if len(c) > 0:
            dp[i][j] = min(c)
        if s[i][j] == '#':
            dp[i][j] += 1
print(dp)
print(dp[H - 1][W - 1])


H, W = map(int, input().split())
s = [input() for i in range(H)]

dp = [[0] * W for i in range(H)]

for i in range(H):
    for j in range(W):
        if i == j == 0:
            if s[i][j] == '#':
                dp[i][j] = 1
            continue
        c = []
        if 0 <= i - 1 < H:
            c.append(dp[i - 1][j]
                     + (1 if s[i - 1][j] == '.' and s[i][j] == '#' else 0))
        if 0 <= j - 1 < W:
            c.append(dp[i][j - 1]
                     + (1 if s[i][j - 1] == '.' and s[i][j] == '#' else 0))
        if len(c) > 0:
            dp[i][j] = min(c)

print(dp[H - 1][W - 1])


Verdicts: Wrong Answer

Accepted
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):
H, W = map(int, input().split())
s = []
for _ in range(H):
  s.append(input())
  
def main(H, W, s):
  
  
  INF = float('inf')
  dp = [[INF]*W for _ in range(H)]
  if s[0][0] == '#':
    dp[0][0] = 1
  else:
    dp[0][0] = 0

  dx = (1, 0)
  dy = (0, 1)
  for i in range(H):
    for j in range(W):
      for dir_ in range(2):  
        ni = i + dx[dir_]
        nj = j + dy[dir_]
        if ni >= H or nj >= W:
          continue
        add = 0
        if s[ni][nj] == '#' and s[i][j] == '.':
          add = 1
        dp[ni][nj] = min(dp[ni][nj], dp[i][j] + add)
        
  print(dp)

  return dp[H-1][W-1]

print(main(H, W, s))

H, W = map(int, input().split())
s = []
for _ in range(H):
  s.append(input())
  
def main(H, W, s):
  
  
  INF = float('inf')
  dp = [[INF]*W for _ in range(H)]
  if s[0][0] == '#':
    dp[0][0] = 1
  else:
    dp[0][0] = 0

  dx = (1, 0)
  dy = (0, 1)
  for i in range(H):
    for j in range(W):
      for dir_ in range(2):  
        ni = i + dx[dir_]
        nj = j + dy[dir_]
        if ni >= H or nj >= W:
          continue
        add = 0
        if s[ni][nj] == '#' and s[i][j] == '.':
          add = 1
        dp[ni][nj] = min(dp[ni][nj], dp[i][j] + add)

  return dp[H-1][W-1]

print(main(H, W, s))

Verdicts: Wrong Answer

Accepted
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):
ans = 0
H,W = map(int,input().split())
s = [list(input()) for _ in range(W)]
if s[H-1][W-1] == '#':
  ans += 1
if s[0][0] == '#':
  ans += 1
for i in range(H):
  for j in range(W):
    if s[i][j] == '.':
      s[i][j] = 0
    else:
      s[i][j] = 1
      

print(ans)

ans = 0
H,W = map(int,input().split())
s = [list(input()) for _ in range(W)]

print(ans)

H,W = map(int,input().split())
s = [list(input()) for _ in range(H)]
dp = [[0] * W for _ in range(H)]
dp[0][0] = int(s[0][0]=="#")

for i in range(1, W):
    dp[0][i] = dp[0][i-1] + (int(s[0][i]=="#") if s[0][i-1]=="." else 0)
for h in range(1,H):
  dp[h][0] = dp[h-1][0] + (int(s[h][0]=="#") if s[h-1][0]=="." else 0)
  for w in range(1,W):
    dp[h][w] = min(dp[h-1][w]+(int(s[h][w]=="#") if s[h-1][w]=="." else 0), 
    dp[h][w-1]+(int(s[h][w]=="#") if s[h][w-1]=="." else 0))
print(dp[-1][-1])

Verdicts: Runtime Error

Runtime Error

Accepted
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):
H,W = map(int,input().split())
s = [input() for _ in range(H)]
import math
DP = [[math.inf]+[0]*W for _ in range(H+1)]
re = [[0]*(W+1) for _ in range(H+1)]
DP[0] = [math.inf]*(W+1)
DP[0][1] = DP[1][0] = 0

for i in range(1,H+1):
    for j in range(1,W+1):
        if s[i-1][j-1] == ".":
            DP[i][j] = min(DP[i-1][j],DP[i][j-1])
        else:
            u = DP[i-1][j]+1-re[i-1][j]
            l = DP[i][j-1]+1-re[i][j-1]
            DP[i][j] = min(u, l)
            re[i][j] = 1
            if u < l and re[i-1][j] == 0:
                DP[i][j] += 1
            elif u > l and re[i][j-1] == 0:
                count += 1
            elif u == l:
                count += 1 - re[i-1][j]*re[i][j-1]
                
print(DP[H][W])



H,W = map(int,input().split())
s = [input() for _ in range(H)]

DP = [[1000]+[0]*W for _ in range(H+1)]
re = [[0]*(W+1) for _ in range(H+1)]
DP[0] = [1000]*(W+1)
DP[0][1] = DP[1][0] = 0

for i in range(1,H+1):
    for j in range(1,W+1):
        if s[i-1][j-1] == ".":
            DP[i][j] = min(DP[i-1][j],DP[i][j-1])
        else:
            u = DP[i-1][j]+1-re[i-1][j]
            l = DP[i][j-1]+1-re[i][j-1]
            DP[i][j] = min(u, l)
            re[i][j] = 1

print(DP[H][W])


Verdicts: Runtime Error

Accepted
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):
h,w=map(int,input().split())
c=[[1000]*w]*h
s=[[+(i=='#') for i in input()] for _ in range(h)]
c[0][0]=s[0][0]
for i in range(h):
  for j in range(w):
    for k,l in [[i-1,j],[i,j-1]]:
      if k<0 or l<0:continue
      if s[i][j]==0:
        c[i][j]=min(c[i][j],c[k][l])
      else:
        if s[k][l]==0:
          c[i][j]=min(c[i][j],c[k][l]+1)
        t=j+1
        if t<w:
          while s[i][t]==1:
            c[i][t]=min(c[i][t],c[i][j])
            t+=1
            if t==w:break
        t=i+1
        if t<h:
          while s[t][j]==1:
            c[t][j]=min(c[t][j],c[i][j])
            t+=1
            if t==h:break
print(c[h-1][w-1])

h,w=map(int,input().split())
s=[[+(i=='#')for i in input()]for j in range(h)]
c=[1000]*w*h
c[0]=s[0][0]
for i in range(h):
  for j in range(w):
    for k,l in [[i-1,j],[i,j-1]]:
      if k<0 or l<0:continue
      c[i*w+j]=min(c[i*w+j],c[k*w+l]+(s[k][l]==0)*(s[i][j]==1))
print(c[-1])

Verdicts: Wrong Answer

Accepted
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):
H,W = list(map(int, input().split()))
B = []
for _ in range(H):
  B.append(input())
  
A = [[999999 for _ in range(W)] for _ in range(H)]
A[0][0] = 0 if B[0][0] == "." else 1

for i in range(H):
  for j in range(W):
    if i+j == 0:
      continue
    X,Y = 999999, 999999
    if i > 0:
      X = A[i-1][j] 
      X += 1 if B[i-1][j] != B[i][j] else 0
    if j > 0:
      Y = A[i][j-1]
      Y += 1 if B[i][j-1] != B[i][j] else 0
    A[i][j] = min(X,Y)
    
print(A[H-1][W-1])

H,W = list(map(int, input().split()))
B = []
for _ in range(H):
  B.append(input())
  
A = [[999999 for _ in range(W)] for _ in range(H)]
A[0][0] = 0 if B[0][0] == "." else 1

for i in range(H):
  for j in range(W):
    if i+j == 0:
      continue
    X,Y = 999999, 999999
    if i > 0:
      X = A[i-1][j] 
      X += 1 if B[i-1][j] == '.' and B[i][j] == '#' else 0
    if j > 0:
      Y = A[i][j-1]
      Y += 1 if B[i][j-1] == '.' and B[i][j] == '#' else 0
    A[i][j] = min(X,Y)
    
print(A[H-1][W-1])

#  print(A[i])

Verdicts: Wrong Answer

Accepted
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):
H,W,*L = open('0').read().split()
H,W = map(int, (H,W))
dp = [[0]*W for i in range(H)]
if L[0][0]=='#':
  dp[0][0] = 1
for i in range(1,H):
  if L[i][0]=='#':
    dp[i][0] = dp[i-1][0]+1
  else:
    dp[i][0] = dp[i-1][0]
for j in range(1,W):
  if L[0][j]=='#':
    dp[0][j] = dp[0][j-1]+1
  else:
    dp[0][j] = dp[0][j-1]
for i in range(1,H):
  for j in range(1,W):
    if L[i][j]=='#':
      dp[i][j] = min(dp[i-1][j],dp[i][j-1])+1
    else:
      dp[i][j] = min(dp[i-1][j],dp[i][j-1])
print(dp[H-1][W-1])

H,W,*L = open(0).read().split()
H,W = map(int, (H,W))
dp = [[0]*W for i in range(H)]
for i in range(1,H):
  if L[i][0]!=L[i-1][0]:
    dp[i][0] = dp[i-1][0]+1
  else:
    dp[i][0] = dp[i-1][0]
for j in range(1,W):
  if L[0][j]!=L[0][j-1]:
    dp[0][j] = dp[0][j-1]+1
  else:
    dp[0][j] = dp[0][j-1]
for i in range(1,H):
  for j in range(1,W):
    if L[i][j]!=L[i-1][j] and L[i][j]!=L[i][j-1]:
      dp[i][j] = min(dp[i-1][j],dp[i][j-1])+1
    elif L[i][j]!=L[i-1][j]:
      dp[i][j] = min(dp[i-1][j]+1,dp[i][j-1])
    elif L[i][j]!=L[i][j-1]:
      dp[i][j] = min(dp[i-1][j],dp[i][j-1]+1)
    else:
      dp[i][j] = min(dp[i-1][j],dp[i][j-1])
if L[0][0]=='.':
  ans = (dp[H-1][W-1]+1)//2
else:
  ans = (dp[H-1][W-1]+2)//2
print(ans)

Verdicts: Runtime Error

Accepted
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):
#!/usr/bin/env python3


def main() -> None:
    H, W = rmi()
    s = []
    for h in range(H):
        line = list(map(lambda c: c == '.', list(r())))
        s.append(line)
    dp = []
    for _ in range(H):
        dp.append([-1] * W)
    dp[0][0] = 0 if s[0][0] else 1
    for h in range(1, H):
        dp[h][0] = dp[h - 1][0] if s[h][0] else dp[h - 1][0] + 1
    for w in range(1, W):
        dp[0][w] = dp[0][w - 1] if s[0][w] else dp[0][w - 1] + 1
    for h in range(1, H):
        for w in range(1, W):
            minimum = min(dp[h - 1][w], dp[h][w - 1])
            dp[h][w] = minimum if s[h][w] else minimum + 1
    print(dp[H][W])


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> tuple:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10 ** 9)
    main()


#!/usr/bin/env python3


def main() -> None:
    H, W = rmi()
    s = []
    for h in range(H):
        line = list(map(lambda c: c == '.', list(r())))
        s.append(line)
    dp = []
    for _ in range(H):
        dp.append([H + W + 1] * W)
    dp[0][0] = 0 if s[0][0] else 1
    for h in range(1, H):
        dp[h][0] = dp[h - 1][0] + int(s[h - 1][0] != s[h][0] and not s[h][0])
    for w in range(1, W):
        dp[0][w] = dp[0][w - 1] + int(s[0][w - 1] != s[0][w] and not s[0][w])
    for h in range(1, H):
        for w in range(1, W):
            dp[h][w] = min(dp[h - 1][w] + int(s[h - 1][w] != s[h][w] and not s[h][w]),
                           dp[h][w - 1] + int(s[h][w - 1] != s[h][w] and not s[h][w]))
    print(dp[H - 1][W - 1])


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> tuple:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10 ** 9)
    main()


Verdicts: Runtime Error

Accepted
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):
#A
H,W = map(int,input().split())
G = [list(str(input())) for _ in range(H)]
inf = float("inf")

dist = [[inf for w in range(W)] for h in range(H)]

if G[0][0] == "#":
    dist[0][0] = 1
else:
    dist[0][0] = 0

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            pass
        elif j == 0:
            dist[i][j] = dist[i-1][j]
            if G[i][j] == "#" and G[i-1][j] != "#":
                dist[i][j]+=1
        elif i == 0:
            dist[i][j] = dist[i][j-1]
            if G[i][j] == "#" and G[i-1][j] != "#":
                dist[i][j]+=1
        else:
            d1 = dist[i-1][j]
            d2 = dist[i][j-1]
            if G[i][j] == "#" and G[i-1][j] != "#":
                d1+=1
            if G[i][j] == "#" and G[i-1][j] != "#":
                d2+=1
            dist[i][j] = min(d1,d2)
        

print(dist[H-1][W-1])



#A
H,W = map(int,input().split())
G = [list(str(input())) for _ in range(H)]
inf = float("inf")

dist = [[inf for w in range(W)] for h in range(H)]

if G[0][0] == "#":
    dist[0][0] = 1
else:
    dist[0][0] = 0

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            pass
        elif j == 0:
            dist[i][j] = dist[i-1][j]
            if G[i][j] == "#" and G[i-1][j] != "#":
                dist[i][j]+=1
        elif i == 0:
            dist[i][j] = dist[i][j-1]
            if G[i][j] == "#" and G[i][j-1] != "#":
                dist[i][j]+=1
        else:
            d1 = dist[i-1][j]
            d2 = dist[i][j-1]
            if G[i][j] == "#" and G[i-1][j] != "#":
                d1+=1
            if G[i][j] == "#" and G[i][j-1] != "#":
                d2+=1
            dist[i][j] = min(d1,d2)
        

print(dist[H-1][W-1])



Verdicts: Wrong Answer

Accepted
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 bisect,collections,copy,itertools,math,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())
##################################################
H,W = LI()
S = [S() for _ in range(H)]
dp = [[-1]*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        judge = 1 if S[i][j]=='#' else 0
        if i==0: 
            if j==0:
                dp[0][0] = judge
            else:
                dp[0][j] = dp[0][j-1]+judge
        else: 意外
            if j==0:
                dp[i][0] = dp[i-1][0]+judge
            else:
                dp[i][j] = min(dp[i][j-1],dp[i-1][j])+judge
print(dp[-1][-1])


import itertools,sys
def S(): return sys.stdin.readline().rstrip()
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
H,W = LI()
S = [S() for _ in range(H)]
dp = [[float('INF')]*W for _ in range(H)]
for i,j in itertools.product(range(H),range(W)):
    if i==0:
        if j==0:
            dp[i][j] = 1 if S[i][j]=='#' else 0
        else:
            dp[i][j] = dp[i][j-1]+(1 if S[i][j]=='#' and S[i][j-1]=='.' else 0)
    else:
        if i==0:
            dp[i][j] = dp[i-1][j]+(1 if S[i][j]=='#' and S[i-1][j]=='.' else 0)
        else:
            dp[i][j] = min(dp[i][j],dp[i][j-1]+(1 if S[i][j]=='#' and S[i][j-1]=='.' else 0))
            dp[i][j] = min(dp[i][j],dp[i-1][j]+(1 if S[i][j]=='#' and S[i-1][j]=='.' else 0))
print(dp[-1][-1])


Verdicts: Wrong Answer

Accepted
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):
h, w = map(int, input().split())
s = [] # h, w
for i in range(h):
    s += [','.join(input()).split(',')]

cost = [[1 if s[0][0] == '#' else 0 for i in range(h + 1)] for j in range(w + 1)]

for i in range(1, h + w - 1):
    for j in range(w if h < w else h):
        if j < h and i - j < w and j >= 0 and i - j >= 0:
            print(j, i - j)
            if j == 0:
                cost[j][i - j] = cost[j][i - j - 1]
            elif i - j == 0:
                cost[j][i - j] = cost[j - 1][i - j]
            else:
                cost[j][i - j] = cost[j - 1][i - j] if cost[j - 1][i - j] < cost[j][i - j - 1] else cost[j][i - j - 1]

            if s[j][i - j] == '#':
                cost[j][i - j] += 1

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


h, w = map(int, input().split())
s = [] # h, w
for i in range(h):
    s += [','.join(input()).split(',')]

cost = [[10000 for i in range(w)] for j in range(h)]


for hh in range(h):
    for ww in range(w):
        
        if hh == ww == 0:
            cost[0][0] = 0 if s[0][0] == '.' else 1

        if hh < h - 1:
            if s[hh][ww] == '.' and s[hh + 1][ww] == '#':
                cost[hh + 1][ww] = min(cost[hh][ww] + 1, cost[hh + 1][ww])
            else:
                cost[hh + 1][ww] = min(cost[hh][ww], cost[hh + 1][ww])
        if ww < w - 1:
            if s[hh][ww] == '.' and s[hh][ww + 1] == '#':
                cost[hh][ww + 1] = min(cost[hh][ww] + 1, cost[hh][ww + 1])
            else:
                cost[hh][ww + 1] = min(cost[hh][ww], cost[hh][ww + 1])

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

Verdicts: Runtime Error

Accepted
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):
h,w=map(int,input().split())
s=[]
for i in range(h):
    s.append(list(input()))

#solution:dp

dp=[[float('inf') for _ in range(w)] for _ in range(h)]
dp[0][0] = 0 if s[0][0] == '.' else 1

def update(dp, i, j, ni, nj):
    if s[ni][nj]=='.':
        dp[ni][nj]=min(dp[i][j],dp[ni][nj])
    elif s[i][j]=='#':
        dp[ni][nj]=min(dp[i][j],dp[ni][nj])
    else:
        dp[ni][nj]=min(dp[i][j]+1,dp[ni][nj])
    
for i in range(h):
    for j in range(w):
        if i+1<h:
            update(dp,i,j,i+1,j)
        if j+1<w:
            update(dp,i,j,i,j+1)
print(dp[h-1][w-1])
print(dp)

h,w=map(int,input().split())
s=[]
for i in range(h):
    s.append(list(input()))

#solution:dp

dp=[[float('inf') for _ in range(w)] for _ in range(h)]
dp[0][0] = 0 if s[0][0] == '.' else 1

def update(dp, i, j, ni, nj):
    if s[ni][nj]=='.':
        dp[ni][nj]=min(dp[i][j],dp[ni][nj])
    elif s[i][j]=='#':
        dp[ni][nj]=min(dp[i][j],dp[ni][nj])
    else:
        dp[ni][nj]=min(dp[i][j]+1,dp[ni][nj])
    
for i in range(h):
    for j in range(w):
        if i+1<h:
            update(dp,i,j,i+1,j)
        if j+1<w:
            update(dp,i,j,i,j+1)
print(dp[h-1][w-1])

Verdicts: Wrong Answer

Accepted
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):
H, W = map(int,input().split())
board = []
for h in range(H):
    board.append(input())

dp=[[1000 for i in range(W+1)] for j in range(H+1)]
dp[0][1] = 0
dp[1][0] = 0

for h in range(H):
    for w in range(W):
        dp[h+1][w+1] = min(dp[h][w+1] + (board[h][w]=='#' and (h==0 or board[h-1][w]=='')), dp[h+1][w] + (board[h][w]=='#' and (w==0 or board[h][w-1]=='')))
 
            

print (dp[-1][-1])



H, W = map(int,input().split())
board = []
for h in range(H):
    board.append(input())

dp=[[1000 for i in range(W+1)] for j in range(H+1)]
dp[0][1] = 0
dp[1][0] = 0

for h in range(H):
    for w in range(W):
        dp[h+1][w+1] = min(dp[h][w+1] + (board[h][w]=='#' and (h==0 or board[h-1][w]=='.')), dp[h+1][w] + (board[h][w]=='#' and (w==0 or board[h][w-1]=='.')))
 
            

print (dp[-1][-1])



Verdicts: Wrong Answer

Accepted
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):
H, W = map(int, input().split())
table = []
for i in range(H):
    table.append([(True if x == "." else False) for x in list(input())])

colormap = [[0 for _ in range(W)] for _ in range(H)]
colormap[0][0] = 0 if table[0][0] else 1

def update(i,j):

    tmp_A = 200
    tmp_B = 200
    
    if 0 < i < H and j < W:
        if table[i-1][j] == table[i][j]:
            tmp_A = colormap[i-1][j]
        else:
            tmp_A = colormap[i-1][j] + 1
        
    if i < H and 0 < j < W:
        if table[i][j-1] == table[i][j]:
            tmp_B = colormap[i][j-1]
        else:
            tmp_B = colormap[i][j-1] + 1
    
    colormap[i][j] = min(tmp_A, tmp_B)

for k in range(1, H + W - 1):
    # print("k={}".format(k))
    for i in range(k + 1):
        if i < H and k - i < W:
            
            update(i, k - i)
    # print()

print(colormap[H-1][W-1])

import numpy as np

N = int(input())
an = [int(x) for x in list(input())]

arr = np.array(an)

for i in range(N-1,0,-1):
    arr2 = abs(arr[0:-1] - arr[1:])
    arr = arr2

print(arr[0])

def update(i,j):

    tmp_A = 200
    tmp_B = 200
    
    if 0 < i < H and j < W:
        if table[i-1][j] == table[i][j]:
            tmp_A = colormap[i-1][j]
        else:
            if table[i-1][j]:
                tmp_A = colormap[i-1][j] + 1
            else:
                tmp_A = colormap[i-1][j]
    if i < H and 0 < j < W:
        if table[i][j-1] == table[i][j]:
            tmp_B = colormap[i][j-1]
        else:
            if table[i][j-1]:
                tmp_B = colormap[i][j-1] + 1
            else:
                tmp_B = colormap[i][j-1]
    
    colormap[i][j] = min(tmp_A, tmp_B)

H, W = map(int, input().split())
table = []
for i in range(H):
    table.append([(True if x == "." else False) for x in list(input())])

colormap = [[0 for _ in range(W)] for _ in range(H)]
colormap[0][0] = 0 if table[0][0] else 1

for i in range(H):
    for j in range(W):
        if not(i == 0 and j ==0):
            update(i, j)
    # print(colormap)


print(colormap[H-1][W-1])

Verdicts: Wrong Answer

Runtime Error

Accepted
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):
H,W=map(int,input().split())

a=[[0]*W for i in range(H)]
b=[[0]*W for i in range(H)]


for i in range(H):
	a[i]=input().split(",")

for j in range(1,W):
	b[0][j]=b[0][j-1]+int(not(a[0][j]==a[0][j-1]))

for i in range(1,H):
	b[i][0]=b[i-1][0]+int(not(a[i-1][0]==a[i][0]))
	for j in range(1,W):
		b[i][j]=min(b[i-1][j]+int(not(a[i-1][j]==a[i][j])),b[i][j-1]+int(not(a[i][j-1]==a[i][j])))
	
print(b[H-1][W-1])


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

a=[[0]*W for i in range(H)]
b=[[0]*W for i in range(H)]

#print(b)

for i in range(H):
	a[i]=list(input())
#print(a)
    
for j in range(1,W):
	b[0][j]=b[0][j-1]+int(not(a[0][j]==a[0][j-1]))

for i in range(1,H):
	b[i][0]=b[i-1][0]+int(not(a[i-1][0]==a[i][0]))
	for j in range(1,W):
		b[i][j]=min(b[i-1][j]+int(not(a[i-1][j]==a[i][j])),b[i][j-1]+int(not(a[i][j-1]==a[i][j])))
	
print(b[H-1][W-1]/2)


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

a=[[0]*W for i in range(H)]
b=[[0]*W for i in range(H)]

#print(b)

for i in range(H):
	a[i]=list(input())
#print(a)
    
for j in range(1,W):
	b[0][j]=b[0][j-1]+int(not(a[0][j]==a[0][j-1]))

for i in range(1,H):
	b[i][0]=b[i-1][0]+int(not(a[i-1][0]==a[i][0]))
	for j in range(1,W):
		b[i][j]=min(b[i-1][j]+int(not(a[i-1][j]==a[i][j])),b[i][j-1]+int(not(a[i][j-1]==a[i][j])))

if(a[0][0]=="." and a[H-1][W-1]=="."):
	print(int(b[H-1][W-1]/2))
elif(a[0][0]=="#" and a[H-1][W-1]=="#"):
  print(int(b[H-1][W-1]/2+1))
else:
  print(int((b[H-1][W-1]+1)/2))
#print(int(b[H-1][W-1]/2))


Verdicts: Runtime Error

Wrong Answer

Accepted
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 sys
sys.stdin.readline

H, W = map(int, input().split())
board = [[[0,1][a == "#"] for a in input()] for i in range(H)]
Y = [[0]*W for _ in range(H)]

for i in range(H):
    for j in range(W):
        a = []
        if j == 0:
            pass
        else:
            if board[i][j-1] == False and board[i][j]:
                a.append(Y[i][j-1] + 1)
            else:
                a.append(Y[i][j-1])
        if i == 0:
            pass
        else:
            if board[i-1][j] == False and board[i][j]:
                a.append(Y[i-1][j] + 1)
            else:
                a.append(Y[i-1][j])
        if i + j == 0:
            a.append(0)
        Y[i][j] = min(a)
        print(Y[i][j])

print(Y[H-1][W-1])


import sys
sys.stdin.readline

H, W = map(int, input().split())
board = [[[0,1][a == "#"] for a in input()] for i in range(H)]
Y = [[0]*W for _ in range(H)]

for i in range(H):
    for j in range(W):
        a = []
        if j == 0:
            pass
        else:
            if board[i][j-1] == False and board[i][j] == True:
                a.append(Y[i][j-1] + 1)
            else:
                a.append(Y[i][j-1])
        if i == 0:
            pass
        else:
            if board[i-1][j] == False and board[i][j] == True:
                a.append(Y[i-1][j] + 1)
            else:
                a.append(Y[i-1][j])
        if i + j == 0:
            if board[i][j]:
                a.append(1)
            else:
                continue
        Y[i][j] = min(a)


print(Y[H-1][W-1])


Verdicts: Wrong Answer

Accepted
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):
##!/usr/bin/env python
## -*- coding: utf-8 -*-
#import sys
#import os
#f = open('input.txt', 'r')

#input = sys.stdin.readline


h, w = map(int, input().split())

dp = [[0 for i in range(w)] for j in range(h)]

maze = []
for i in range(h):
    sub = list(input())[:-1]
    maze.append(sub)

if maze[0][0] == "#":
    dp[0][0] = 1
for i in range(1, w):
    if maze[0][i - 1] == "." and maze[0][i] == "#":
        dp[0][i] = dp[0][i - 1] + 1

    else:
        dp[0][i] = dp[0][i - 1]
for j in range(1, h):
    if maze[j - 1][0] == "." and maze[j][0] == "#":
        dp[j][0] = dp[j - 1][0] + 1
    else:
        dp[j][0] = dp[j - 1][0]

for i in range(1, h):
    for j in range(1, w):
        d = dp[i - 1][j]
        e = dp[i][j - 1]
        if maze[i - 1][j] == "." and maze[i][j] == "#":
            d += 1
        if maze[i][j - 1] == "." and maze[i][j] == "#":
            e += 1
        dp[i][j] = min(d, e)
print(dp[h - 1][w - 1])


##!/usr/bin/env python
## -*- coding: utf-8 -*-
#import sys
#import os
#f = open('input.txt', 'r')

#input = sys.stdin.readline


h, w = map(int, input().split())

dp = [[0 for i in range(w)] for j in range(h)]

maze = []
for i in range(h):
    sub = list(input())
    maze.append(sub)

if maze[0][0] == "#":
    dp[0][0] = 1
for i in range(1, w):
    if maze[0][i - 1] == "." and maze[0][i] == "#":
        dp[0][i] = dp[0][i - 1] + 1

    else:
        dp[0][i] = dp[0][i - 1]
for j in range(1, h):
    if maze[j - 1][0] == "." and maze[j][0] == "#":
        dp[j][0] = dp[j - 1][0] + 1
    else:
        dp[j][0] = dp[j - 1][0]

for i in range(1, h):
    for j in range(1, w):
        d = dp[i - 1][j]
        e = dp[i][j - 1]
        if maze[i - 1][j] == "." and maze[i][j] == "#":
            d += 1
        if maze[i][j - 1] == "." and maze[i][j] == "#":
            e += 1
        dp[i][j] = min(d, e)
print(dp[h - 1][w - 1])


Verdicts: Runtime Error

Accepted
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):
# coding: utf-8
# Your code here!
H,W=map(int,input().split())

meizu=[]
for _ in range(H):
    meizu.append(list(input()))

dp=[[10**9 for w in range(W)] for h in range(H)]
dp[0][0]=1 if meizu[0][0]=="#" else 0

for h in range(H):
    for w in range(W):
        if h!=H-1:
            dp[h+1][w]=min(dp[h+1][w],dp[h][w]+1 if meizu[h+1][w]=="
        if w!=W-1:
            dp[h][w+1]=min(dp[h][w+1],dp[h][w]+1 if meizu[h][w+1]=="
    
print(dp[-1][-1])


# coding: utf-8
# Your code here!

# coding: utf-8 
# Your code here! 
H,W=map(int,input().split()) 
S=[] 

for _ in range(H):
    S.append(list(input()))


def dfs(h,w,num):
    if h==H-1 and w==W-1:
        ans.append(num)
    
    if h!=H-1: 
        if S[h+1][w]=="#": 
            dfs(h+1,w,num+1)
        else:
            dfs(h+1,w,num)

    if w!=W-1: 
        if S[h][w+1]=="#": 
            dfs(h,w+1,num+1)
        else:
            dfs(h,w+1,num)

ans=[]
dfs(0,0,1) if S[0][0]=="

print(min(ans))



# coding: utf-8
# Your code here!
H,W=map(int,input().split())

meizu=[]
for _ in range(H):
    meizu.append(list(input()))

dp=[[10**9 for w in range(W)] for h in range(H)]
dp[0][0]=1 if meizu[0][0]=="#" else 0

for h in range(H):
    for w in range(W):
        if h!=H-1:
            dp[h+1][w]=min(dp[h+1][w],dp[h][w]+1 if meizu[h+1][w]=="#" and meizu[h][w]=="." else dp[h][w])
        if w!=W-1:
            dp[h][w+1]=min(dp[h][w+1],dp[h][w]+1 if meizu[h][w+1]=="#" and meizu[h][w]=="." else dp[h][w])
    
print(dp[-1][-1])


Verdicts: Wrong Answer

Time Limit Exceeded

Accepted