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())

S = [list(str(input())) for i in range(H)]
S = np.array(S)

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

dp[0,:] = np.cumsum(S[0,:]=="#")
dp[:,0] = np.cumsum(S[:,0]=="#")

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

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

# Code for A - Range Flip Find Route
# Use input() to fetch data from STDIN

import numpy as np
H, W = map(int, input().split())

S = [list(str(input())) for i in range(H)]
S = np.array(S)

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

dp[0, :] = np.cumsum(S[0, :] == "#")
dp[:, 0] = np.cumsum(S[:, 0] == "#")

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

print(dp[-1, -1])


# Code for A - Range Flip Find Route
# Use input() to fetch data from STDIN

import numpy as np
H, W = map(int, input().split())

S = [list(str(input())) for i in range(H)]
S = np.array(S)

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

# init
if S[0, 0] == "#":
    dp[0, 0] = 1
else:
    dp[0, 0] = 0

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


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

        if S[i, j] == "#":
            if S[i, j] == S[i-1, j]:
                tmp1 = dp[i-1, j]
            else:
                tmp1 = dp[i-1, j]+1
        else:
            tmp1 = dp[i-1, j]

        if S[i, j] == "#":
            if S[i, j] == S[i, j-1]:
                tmp2 = dp[i, j-1]
            else:
                tmp2 = dp[i, j-1]+1
        else:
            tmp2 = dp[i, j-1]

        dp[i, j] = min(tmp1, tmp2)
# print(dp)
print(dp[-1, -1])


# Code for A - Range Flip Find Route
# Use input() to fetch data from STDIN

import numpy as np
H, W = map(int, input().split())

S = [list(str(input())) for i in range(H)]
S = np.array(S)

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

# init
if S[0, 0] == "#":
    dp[0, 0] = 1
else:
    dp[0, 0] = 0

for i in range(1, W):
    if S[0, i] == "#":
        if S[0, i] == S[0, i-1]:
            dp[0, i] = dp[0, i-1]
        else:
            dp[0, i] = dp[0, i-1]+1
    else:
        dp[0, i] = dp[0, i-1]
for i in range(1, H):
    if S[i, 0] == "#":
        if S[i, 0] == 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]

# print(dp)

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

        tmp2 = dp[i, j-1]
        if S[i, j] == "#":
            tmp2 += 1

        # if S[i, j] == "#":
        #     if S[i, j] == S[i, j-1]:
        #         tmp2 = dp[i, j-1]
        #     else:
        #         tmp2 = dp[i, j-1]+1
        # else:
        #     tmp2 = dp[i, j-1]
        # if S[i, 0] == "#":
        #     tmp2 += 1

        dp[i, j] = min(tmp1, tmp2)
# print(dp)
print(dp[-1, -1])


# Code for A - Range Flip Find Route
# Use input() to fetch data from STDIN

import numpy as np
H, W = map(int, input().split())

S = [list(str(input())) for i in range(H)]
S = np.array(S)

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

# init
if S[0, 0] == "#":
    dp[0, 0] = 1
else:
    dp[0, 0] = 0

for i in range(1, W):
    if S[0, i] == "#":
        if S[0, i] == S[0, i-1]:
            dp[0, i] = dp[0, i-1]
        else:
            dp[0, i] = dp[0, i-1]+1
    else:
        dp[0, i] = dp[0, i-1]
for i in range(1, H):
    if S[i, 0] == "#":
        if S[i, 0] == 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]

# print(dp)

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

        if S[i, j] == "#":
            if S[i, j] == S[i, j-1]:
                dp[i, j] = dp[i, j-1]
            else:
                dp[i, j] = dp[i, j-1]+1
        else:
            dp[i, j] = dp[i, j-1]

        dp[i, j] = min(dp[i, j], tmp1)
# print(dp)
print(dp[-1, -1])


import numpy as np
H, W = map(int, input().split())

S = [np.array(list(str(input())))=="
S = np.array(S)
print(S)

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

dp[0,:] = np.cumsum(S[0,:])
dp[:,0] = np.cumsum(S[:,0])

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

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

import numpy as np
H, W = map(int, input().split())

S = [np.array(list(str(input())))=="
S = np.array(S)

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

dp[0,:] = np.cumsum(S[0,:])
dp[:,0] = np.cumsum(S[:,0])

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

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

#!/usr/bin/env python3

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

ss = [list(str(input())) for _ in range(h)]

dp = [[100*100 for _ in range(w)] for i in range(h)]  # dp[h][w]

if ss[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0
# print(dp)

for h_tmp in range(0, h):
    for w_tmp in range(0, w):
        if w_tmp > 0:
            # print(dp[h_tmp][w_tmp-1])
            if ss[h_tmp][w_tmp-1] == "." and ss[h_tmp][w_tmp] == "#":
                dp[h_tmp][w_tmp] = min(dp[h_tmp][w_tmp], dp[h_tmp][w_tmp-1]+1)
            else:
                dp[h_tmp][w_tmp] = min(dp[h_tmp][w_tmp], dp[h_tmp][w_tmp-1])
        if h_tmp > 0:
            # print(dp[h_tmp][w_tmp-1])
            if ss[h_tmp-1][w_tmp] == "." and ss[h_tmp][w_tmp] == "#":
                dp[h_tmp][w_tmp] = min(dp[h_tmp][w_tmp], dp[h_tmp-1][w_tmp]+1)
            else:
                dp[h_tmp][w_tmp] = min(dp[h_tmp][w_tmp], dp[h_tmp-1][w_tmp])

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


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted