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 numpy as np

H, W = map(int, input().split())
temp = [list(input().replace("
masu = [[int(temp[i][j]) for j in range(W)] for i in range(H)]


dp = np.zeros((H, W))
dp_flag = np.zeros((H, W))

if masu[0][0] == 0:
    dp[0][0] = 1
else:
    dp[0][0] = 0

for i in range(0,H):
    for j in range(0,W):
        if i == 0:
            a = 100000
        else:
            if masu[i][j] != masu[i-1][j]:
                if dp_flag[i-1][j] == 1:
                    a = dp[i-1][j]
                else:
                    dp_flag[i][j] = 1
                    a = dp[i-1][j] + 1
            else:
                a = dp[i-1][j]
        if j == 0:
            b = 100000
        else:
            if masu[i][j] != masu[i][j-1]:
                if dp_flag[i][j-1] == 1:
                    b = dp[i][j-1]
                else:
                    dp_flag[i][j] = 1
                    b = dp[i][j-1] + 1
            else:
                b = dp[i][j-1]
        if a != 100000 or b != 100000:
            dp[i][j] = min([a, b])
print(int(dp[H-1][W-1]))


import numpy as np

H, W = map(int, input().split())
temp = [list(input().replace("
masu = [[int(temp[i][j]) for j in range(W)] for i in range(H)]


dp = np.zeros((H, W))
dp_flag = np.zeros((H, W))

if masu[0][0] == 0:
    dp[0][0] = 1
    masu[0][0] = 1
else:
    dp[0][0] = 0

for i in range(0,H):
    for j in range(0,W):
        if i == 0:
            a = 100000
        else:
            if masu[i][j] != masu[i-1][j]:
                if dp_flag[i-1][j] == 1:
                    a = dp[i-1][j]
                else:
                    dp_flag[i][j] = 1
                    a = dp[i-1][j] + 1
            else:
                a = dp[i-1][j]
        if j == 0:
            b = 100000
        else:
            if masu[i][j] != masu[i][j-1]:
                if dp_flag[i][j-1] == 1:
                    b = dp[i][j-1]
                else:
                    dp_flag[i][j] = 1
                    b = dp[i][j-1] + 1
            else:
                b = dp[i][j-1]
        if a != 100000 or b != 100000:
            dp[i][j] = min([a, b])
print(int(dp[H-1][W-1]))


import numpy as np

H, W = map(int, input().split())
temp = [list(input().replace("
masu = [[int(temp[i][j]) for j in range(W)] for i in range(H)]


dp = np.zeros((H, W))
dp[0][0] = 0

for i in range(1,H):
    for j in range(1,W):
        if i == 0:
            a = 100000
        else:
            if masu[i][j] != masu[i-1][j]:
                a = dp[i-1][j] + 1
            else:
                a = dp[i-1][j]
        if j == 0:
            b = 100000
        else:
            if masu[i][j] != masu[i][j-1]:
                b = dp[i][j-1] + 1
            else:
                b = dp[i][j-1]
        dp[i][j] = min([a, b])
print(int(dp[H-1][W-1]))


H, W = map(int, input().split())
s = ""
for i in range(0, H):
    temp = input().replace("#", "0")
    l_bit = temp.replace(".", "1")
    s = s + l_bit

route1 = []
route = [[]]
for j in range(0, W-1):
    route[0].append(j)
print(route)
count = len(route)

#k :0~W-1
best = (H - 1) + (W - 1)
for k in range(0, W-1):    
    for num in range(0, count):
        for p in range(0, (H*W)-(W-k)):
            route1 = route[num]
            route1[W-2-k] = route1[W-1-k-1] + p
            if W > 2:
                if route1[W-k-3] % W <= route1[W-k-2] % W: 
                    route.append(route1) 
                    count += 1
                    block = (H - 1) + (W - 1)
                    print(route1)
                    for q in range(0, len(route1)):
                        if q+1 != len(route1)+1:
                            print(q)
                            if s[route1[q+1]] == s[route1[q]] and ((route1[q+1] == route1[q] + 1) or (route1[q] % W == (route1[q] + 1) % W)):
                                block = block - 1
                                best = min([best, block])
            else:
                route.append(route1) 
                count += 1
                block = (H - 1) + (W - 1)
                print(route1)
                for q in range(0, len(route1)):
                    if q+1 != len(route1):
                        if s[route1[q+1]] == s[route1[q]] and ((route1[q+1] == route1[q] + 1) or (route1[q] % W == (route1[q] + 1) % W)):
                            block = block - 1
                best = min([best, block])

print(best)

import numpy as np

H, W = map(int, input().split())
temp = [list(input().replace("
masu = [[int(temp[i][j]) for j in range(W)] for i in range(H)]


dp = np.zeros((H, W))

if masu[0][0] == 0:
    dp[0][0] = 1
    masu[0][0] = 1
else:
    dp[0][0] = 0

for i in range(0,H):
    for j in range(0,W):
        if i == 0:
            a = 100000
        else:
            if masu[i][j] != masu[i-1][j]:
                a = dp[i-1][j] + 1
            else:
                a = dp[i-1][j]
        if j == 0:
            b = 100000
        else:
            if masu[i][j] != masu[i][j-1]:
                b = dp[i][j-1] + 1
            else:
                b = dp[i][j-1]
        if a != 100000 or b != 100000:
            dp[i][j] = min([a, b])
print(int(dp[H-1][W-1]))


import numpy as np

H, W = map(int, input().split())
temp = [list(input().replace("
masu = [[int(temp[i][j]) for j in range(W)] for i in range(H)]


dp = np.zeros((H, W))
dp_flag = np.zeros((H, W))

if masu[0][0] == 0:
    dp[0][0] = 1
else:
    dp[0][0] = 0

for i in range(0,H):
    for j in range(0,W):
        if i == 0:
            a = 100000
        else:
            if masu[i][j] != masu[i-1][j]:
                if dp_flag[i-1][j] == 1:
                    dp_flag[i][j] = 0
                    a = dp[i-1][j]
                else:
                    dp_flag[i][j] = 1
                    a = dp[i-1][j] + 1
            else:
                a = dp[i-1][j]
        if j == 0:
            b = 100000
        else:
            if masu[i][j] != masu[i][j-1]:
                if dp_flag[i][j-1] == 1:
                    dp_flag[i][j] = 0
                    b = dp[i][j-1]
                else:
                    dp_flag[i][j] = 1
                    b = dp[i][j-1] + 1
            else:
                b = dp[i][j-1]
        if not(a == 100000 and b == 100000):
            dp[i][j] = min([a, b])
print(int(dp[H-1][W-1]))


                    route.append(route1) 
                    count += 1
                    block = (H - 1) + (W - 1)
                    print(route1)
                    for q in range(0, len(route1)):
                        if q+1 != len(route1):
                            if s[route1[q+1]] == s[route1[q]] and ((route1[q+1] == route1[q] + 1) or (route1[q] % W == (route1[q] + 1) % W)):
                                block = block - 1
                    best = min([best, block])

import numpy as np

H, W = map(int, input().split())
temp = [list(input().replace("
masu = [[int(temp[i][j]) for j in range(W)] for i in range(H)]


dp = np.zeros((H, W))

if masu[0][0] == 0:
    dp[0][0] = 1
    masu[0][0] = 1
else:
    dp[0][0] = 0

for i in range(1,H):
    for j in range(1,W):
        if i == 0:
            a = 100000
        else:
            if masu[i][j] != masu[i-1][j]:
                a = dp[i-1][j] + 1
            else:
                a = dp[i-1][j]
        if j == 0:
            b = 100000
        else:
            if masu[i][j] != masu[i][j-1]:
                b = dp[i][j-1] + 1
            else:
                b = dp[i][j-1]
        dp[i][j] = min([a, b])
print(int(dp[H-1][W-1]))


import numpy as np

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




dp = [[0]*W for i in range(H)]
if masu[0][0] == "#":
    dp[0][0] = 1

for i in range(0,H):
    for j in range(0,W):
        if i == 0:
            a = 100000
        else:
            if masu[i][j] == "#" and masu[i-1][j] == ".":
                a = dp[i-1][j] + 1
            else:
                a = dp[i-1][j]
        if j == 0:
            b = 100000
        else:
            if masu[i][j] == "#" and masu[i][j-1] == ".":
                b = dp[i][j-1] + 1
            else:
                b = dp[i][j-1]
        if a != 100000 or b != 100000:
            dp[i][j] = min([a, b])
print(int(dp[H-1][W-1]))


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Accepted