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
import copy
import math
import fractions

import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
import numpy as np
from decimal import *
sys.setrecursionlimit(10 ** 6)

def CalcList(list):
    bfnum = -1
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
        else:
            now = abs(i -bfnum)
            if now >0:
                afterList.append(now)
            bfnum = i
    return afterList

def CheckList(list):
    bfnum = -1
    count = 0
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
            afterList.append(i)
        else:
            if i == bfnum:
                count = count +1
                if count < 3:
                    afterList.append(i)
            else:
                afterList.append(i)
    return afterList


if __name__ == '__main__':
    n = int(input())
    s = input()
    list =list(map(lambda x: int(x), s))

    while(len(list) >1):
        list = CalcList(list)

    if len(list) ==0:
        print(0)
    else:
        print(list[0])

import sys
import copy
import math
import fractions

import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
import numpy as np
from decimal import *

maiz = []
maxh = 0
maxw = 0


def CheckCost(h, w):
    if h + 1 < maxh:
        if costList[h + 1][w] == -1 or costList[h + 1][w] > costList[h][w] + CheckColum(h + 1, w ,maiz[h][w]):
            costList[h + 1][w] = costList[h][w] + CheckColum(h + 1, w ,maiz[h][w])
            CheckCost(h + 1, w)

    if w + 1 < maxw:
        if costList[h][w + 1] == -1 or costList[h][w + 1] > costList[h][w] + CheckColum(h, w + 1,maiz[h][w]):
            costList[h][w + 1] = costList[h][w] + CheckColum(h, w + 1,maiz[h][w])
            CheckCost(h, w + 1)


def CheckColum(h, w ,s):
    if maiz[h][w] == "#" and s == ".":
        return 1
    else:
        return 0


if __name__ == '__main__':
    h, w = map(int, input().split())
    maxh = h
    maxw = w
    costList = []
    for i in range(h):
        costList.append([-1] * w)

    for i in range(h):
        s = input()
        maiz.append(list(map(lambda x: x, s)))
    if maiz[0][0] == "#":
        costList[0][0] = 1
    else:
        costList[0][0] = 0

    CheckCost(0, 0)

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


Verdicts: Runtime Error

Accepted