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):
black = 1
count = 0
flag = False
def check(board, now):
    global flag, count
    if now[0] > len(board) -2 or board[now[0]+1][now[1]]==black:
        if now[1] > len(board[0]) -2 or board[now[0]][now[1]+1]==black:
            if flag == False:
                count += 1
                flag = True
            if now[1] < len(board[0]) -1:
                return [now[0], now[1]+1]
            elif now[0] < len(board) -1:
                return [now[0]+1, now[1]]
        else:
            flag = False
            return [now[0], now[1]+1]
    else:
        flag = False
        return [now[0]+1, now[1]]
def main():
    global count, flag
    H, W = map(int, input().split())
    board = [[white for i in range(W)] for j in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                board[i][j] = white
            else:
                board[i][j] = black
    if board[0][0] == black:
        count += 1
        flag = True
    now = [0, 0]
    while now != [H-1, W-1]:
        now = check(board, now)
    print(count)
if __name__ == "__main__":
    main()

def calc2(array):
    temp = []
    for x, y in zip(array[:-1], array[1:]):
        temp.append(abs(x-y))
    if len(temp) == 1:
        print(temp[0])
    else:
        calc2(temp)
def main():
    N = int(input())
    array = [0 for i in range(N+1)]
    s = input()
    for j in range(N):
        array[j+1] = int(s[j])
    calc2(array)
if __name__ == "__main__":
    main()


import sys
sys.setrecursionlimit(10**6)
def main():
    h, w = map(int, input().split())
    s = [input() for _ in range(h)]
    dp = [[10**5] * w for _ in range(h)]
    def func(temp, now, flag):
        x, y = now
        if y > h - 1 or x > w - 1:
            return 10**6
        if s[y][x] == "#":
            if not flag:
                temp += 1
            flag = True
        else:
            flag = False
        if x == w-1 and y == h-1:
            return temp
        if dp[y][x] <= temp:
            return 10**6
        dp[y][x] = temp
        piyo = []
        return min(func(temp, (x + 1, y), flag), func(temp, (x, y + 1), flag))
    print(func(0, (0, 0), False))
main()

Verdicts: Runtime Error

Runtime Error

Accepted