Problem:


Score : 400 points

Problem Statement

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.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

H W
s_{11} s_{12} \cdots s_{1W}
s_{21} s_{22} \cdots s_{2W}
 \vdots
s_{H1} s_{H2} \cdots s_{HW}

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

4 4
..##
#...
###.
###.

Sample Output 3

0

No operation may be needed.

Sample Input 4

5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

Sample Output 4

4



Sample input:
3 3
.##
.#.
##.


Sample output:
1

Submission status: Runtime Error

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

h,w=LI()
h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0})
dp=[10**6]*(h*w)
dp[h+1]=g[h+1]

for j in range(h*w-h):
    if (j+1)%w!=w-1:
        dp[j+1]=min(dp[j]+1 if (g[j],g[j+1])==(0,1) else dp[j],dp[j+1])
    if (j+w)//w!=h-1:
        dp[j+w]=min(dp[j]+1 if (g[j],g[j+w])==(0,1) else dp[j],dp[j+w])

print(dp[(h-2)*w+w-2])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
inp = list(map(int,input().split()))
H= inp[0]
W=inp[1]
ban =[]
kai = 0
for i in range(W):
    info = list(input())
    ban.append(info)
    
# print(ban)
clear = False
cur = [0,0]
def f(r,c):
    global clear
    if r < H-1:
        if ban[r+1][c] == ".":
            # print("{},{}a".format(r+1,c))
            f(r+1,c)
        else:
            list1.append(r+c+1)
            list2.append([r,c])
    elif c == W-1:
        # print("達成")
        clear = True
    if c < W-1:
        if ban[r][c+1] == ".":
            # print("{},{}b".format(r,c+1))
            f(r,c+1)
        else:
            list1.append(r+c+1)
            list2.append([r,c])
    elif r == H-1:
        # print("達成")
        clear = True

list3 =[]
list4 = []
def f2(r,c):
    global kai
    # list3 =[]
    # list4 = []
    if r < H-1:
        if ban[r+1][c] == "#":
            # print("{},{}".format(r+1,c))
            f2(r+1,c)
        else:
            list3.append(r+c+1)
            list4.append([r,c])
        break
    elif c == W-1:
        list3.append(r+c)
        list4.append([r,c])
    if c < W-1:
        if ban[r][c+1] == "#":
            # print("{},{}".format(r,c+1))
            f2(r,c+1)
        else:
            list3.append(r+c+1)
            list4.append([r,c])
    elif r == H-1:
        list3.append(r+c)
        list4.append([r,c])
        break
# print(ban)
for l in range(100):
    list1 =[]
    list2 = []
    if ban[cur[0]][cur[1]] == ".":
        f(0,0)
        if clear == False:
            cur = list2[list1.index(max(list1))]
        else:
            print(kai)
            break
    else:
        # print("b")
        f2(-1,0)
        # print(list4)
        mov = list4[list3.index(max(list3))]
    # print(mov)
        if  cur[0] < H-1:
            for i in range(cur[0],mov[0]+1):
                for k in range(cur[1],mov[1]+1):
                    if ban[i][k] == ".":
                        ban[i][k] = "#"
                    else:
                        ban[i][k] = "."
        else:
            for i in range(cur[0],mov[0]+1):
                for k in range(cur[1]+1,mov[1]+1):
                    if ban[i][k] == ".":
                        ban[i][k] = "#"
                    else:
                        ban[i][k] = "."
        kai += 1
        f(0,0)
        if clear == False:
            cur = list2[list1.index(max(list1))]
        else:
            print(kai)
            break
    # f(cur[0],cur[1])
    # print(list1)
    # print(cur)
    f2(cur[0],cur[1])
    mov = list4[list3.index(max(list3))]
    # print(mov)
    if  cur[0] < H-1:
        for i in range(cur[0]+1,mov[0]+1):
            for k in range(cur[1],mov[1]+1):
                if ban[i][k] == ".":
                    ban[i][k] = "#"
                else:
                    ban[i][k] = "."
    else:
        for i in range(cur[0],mov[0]+1):
            for k in range(cur[1]+1,mov[1]+1):
                if ban[i][k] == ".":
                    ban[i][k] = "#"
                else:
                    ban[i][k] = "."
    kai += 1
    cur = mov.copy()
    # print(ban)
    # print(clear)

Submission status: Runtime Error

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

h,w=LI()
h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0})
dp=[10**6]*(h*w)
dp[h+1]=g[h+1]

for j in range(h*w-h):
    if dp[j]>=10**6:continue
    dp[j+1]=min(dp[j]+1 if (g[j],g[j+1])==(0,1) else dp[j],dp[j+1])
    dp[j+w]=min(dp[j]+1 if (g[j],g[j+w])==(0,1) else dp[j],dp[j+w])

print(dp[(h-2)*w+w-2])

Submission status: Runtime Error

Solution code:
import sys
import math
import collections
from collections import deque
#n = int(input())
H, W = map(int, sys.stdin.readline().split())
#s = input()
s = []
for i in range(H):
    s.append(list(input()))

def pal(h, w):
    i = 1
    while(True):
        if s[h][w+i] == ".":
            I = i - 1
            break
        if w + i == W - 1:
            I = i
            break
        i += 1
    return(I)

def ver(h, w):
    j = 1
    while(True):
        if s[h+j][w] == ".":
            J = j - 1
            break
        if h + j == H - 1:
            J = j
            break
        j += 1
    return(J)

def com(h, w):
    if w == W - 1:
        h3 = ver(h-1, W-1)
        w3 = 0
    elif h == H - 1:
        h3 = 0
        w3 = pal(H-1, w-1)
    else:         
        h1 = ver(h, w)
        w1 = pal(h1, w)
        w2 = pal(h, w)
        h2 = ver(h, w2)

        if h1 * w1 >= h2 * w2:
            h3 = h1
            w3 = w1
        else:
            h3 = h2
            w3 = w2

    for k in range(h3):
        s[h+k+1][w] = "."
    for l in range(w3):
        s[h][w+l+1] = "."


def que(maxsh, maxsw):
    q = deque([])
    dh = [1, 0]
    dw = [0, 1]
    q.append((maxsh, maxsw))
    while q :
        sh, sw = q.popleft()
        for k in range(2):
            ah = sh + dh[k]
            aw = sw + dw[k]
            if 0 <= ah < H and 0 <= aw < W:
                if s[ah][aw] == ".":
                    q.append((ah, aw))
                    if ah + aw > maxsh + maxsw:
                        maxsh = ah
                        maxsw = aw
    return(maxsh, maxsw)

def main():
    ans = 0
    if s[0][0] == "#":
        com(-1, -1)
        ans += 1

    maxsh, maxsw = que(0, 0)
    while(True):
        if maxsh == H - 1 and maxsw == W - 1:
            print(ans)
            break

        maxsh, maxsw = que(maxsh, maxsw)
        com(maxsh, maxsw)
        ans += 1
        
        
main()

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
inp = list(map(int,input().split()))
H= inp[0]
W=inp[1]
ban =[]
kai = 0
for i in range(W):
    info = list(input())
    ban.append(info)
    
# print(ban)
clear = False
cur = [0,0]
def f(r,c):
    global clear
    if r < H-1:
        if ban[r+1][c] == ".":
            # print("{},{}a".format(r+1,c))
            f(r+1,c)
        else:
            list1.append(r+c+1)
            list2.append([r,c])
    elif c == W-1:
        # print("達成")
        clear = True
    if c < W-1:
        if ban[r][c+1] == ".":
            # print("{},{}b".format(r,c+1))
            f(r,c+1)
        else:
            list1.append(r+c+1)
            list2.append([r,c])
    elif r == H-1:
        # print("達成")
        clear = True

list3 =[]
list4 = []
def f2(r,c):
    global kai
    # list3 =[]
    # list4 = []
    if r < H-1:
        if ban[r+1][c] == "#":
            # print("{},{}".format(r+1,c))
            f2(r+1,c)
        else:
            list3.append(r+c+1)
            list4.append([r,c])
    elif c == W-1:
        list3.append(r+c)
        list4.append([r,c])
    if c < W-1:
        if ban[r][c+1] == "#":
            # print("{},{}".format(r,c+1))
            f2(r,c+1)
        else:
            list3.append(r+c+1)
            list4.append([r,c])
    elif r == H-1:
        list3.append(r+c)
        list4.append([r,c])
   
# print(ban)
for l in range(100):
    list1 =[]
    list2 = []
    if ban[cur[0]][cur[1]] == ".":
        f(0,0)
        if clear == False:
            cur = list2[list1.index(max(list1))]
        else:
            print(kai)
            break
    else:
        # print("b")
        f2(-1,0)
        # print(list4)
        mov = list4[list3.index(max(list3))]
    # print(mov)
        if  cur[0] < H-1:
            for i in range(cur[0],mov[0]+1):
                for k in range(cur[1],mov[1]+1):
                    if ban[i][k] == ".":
                        ban[i][k] = "#"
                    else:
                        ban[i][k] = "."
        else:
            for i in range(cur[0],mov[0]+1):
                for k in range(cur[1]+1,mov[1]+1):
                    if ban[i][k] == ".":
                        ban[i][k] = "#"
                    else:
                        ban[i][k] = "."
        kai += 1
        f(0,0)
        if clear == False:
            cur = list2[list1.index(max(list1))]
        else:
            print(kai)
            break
    # f(cur[0],cur[1])
    # print(list1)
    # print(cur)
    f2(cur[0],cur[1])
    mov = list4[list3.index(max(list3))]
    # print(mov)
    if  cur[0] < H-1:
        for i in range(cur[0]+1,mov[0]+1):
            for k in range(cur[1],mov[1]+1):
                if ban[i][k] == ".":
                    ban[i][k] = "#"
                else:
                    ban[i][k] = "."
    else:
        for i in range(cur[0],mov[0]+1):
            for k in range(cur[1]+1,mov[1]+1):
                if ban[i][k] == ".":
                    ban[i][k] = "#"
                else:
                    ban[i][k] = "."
    kai += 1
    cur = mov.copy()
    # print(ban)
    # print(clear)

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
P=[input().split() for i in range(H)]
count=0
counts=0
for j in range(H):
    if j<H-2:
        for k in range(counts,W):
            if k <W-2:
                if P[j][k]=="#":
                    count=count+1
                    if (P[j][k+2]=="#" or P[j+1][k+2]=="#") and P[j][k+1]=="#":
                        counts=counts+1
                    elif (P[j+2][k]=="#" or P[j+1][k+1]=="#") and P[j+1][k]=="#":
                        counts=counts+1
                    else:
                        break
            elif k==W-2:
                if P[j][k]=="#":
                    count=count+1
                    if P[j][k+1]=="#":
                        counts=counts+1
                    elif P[j+1][k]=="#":
                        counts=counts+1
                    else:
                        break
            else:
                if P[j+1][k]=="#":
                    counts=counts+1
                else:
                    break
    elif j==H-2:
        for k in range(counts,W):
            if k <W-2:
                if P[j][k]=="#":
                    count=count+1
                    if (P[j][k+2]=="#" or P[j+1][k+2]=="#") and P[j][k+1]=="#":
                        counts=counts+1
                    elif P[j+1][k]=="#":
                        counts=counts+1
                    else:
                        break
            elif k==W-2:
                if P[j][k]=="#":
                    count=count+1
                    if P[j][k+1]=="#":
                        counts=counts+1
                    elif P[j+1][k]=="#":
                        counts=counts+1
                    else:
                        break
            else:
                if P[j+1][k]=="#":
                    counts=counts+1
                else:
                    break
    else:
        for k in range(counts,W):
            if P[j][k]=="#":
                counts=counts+1

print(count)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
inp = list(map(int,input().split()))
H= inp[0]
W=inp[1]
ban =[]
kai = 0
for i in range(W):
    info = list(input())
    ban.append(info)
    
# print(ban)
clear = False
cur = [0,0]
def f(r,c):
    global clear
    if r < H-1:
        if ban[r+1][c] == ".":
            # print("{},{}a".format(r+1,c))
            f(r+1,c)
        else:
            list1.append(r+c+1)
            list2.append([r,c])
    elif c == W-1:
        # print("達成")
        clear = True
    if c < W-1:
        if ban[r][c+1] == ".":
            # print("{},{}b".format(r,c+1))
            f(r,c+1)
        else:
            list1.append(r+c+1)
            list2.append([r,c])
    elif r == H-1:
        # print("達成")
        clear = True

list3 =[]
list4 = []
def f2(r,c):
    global kai
    # list3 =[]
    # list4 = []
    if r < H-1:
        if ban[r+1][c] == "#":
            # print("{},{}".format(r+1,c))
            f2(r+1,c)
        else:
            list3.append(r+c+1)
            list4.append([r,c])
    elif c == W-1:
        list3.append(r+c)
        list4.append([r,c])
    if c < W-1:
        if ban[r][c+1] == "#":
            # print("{},{}".format(r,c+1))
            f2(r,c+1)
        else:
            list3.append(r+c+1)
            list4.append([r,c])
    elif r == H-1:
        list3.append(r+c)
        list4.append([r,c])
   
# print(ban)
for l in range(100):
    list1 =[]
    list2 = []
    if ban[cur[0]][cur[1]] == ".":
        f(0,0)
        if clear == False:
            cur = list2[list1.index(max(list1))]
        else:
            print(kai)
            break
    else:
        print("b")
        f2(-1,0)
        # print(list4)
        mov = list4[list3.index(max(list3))]
    # print(mov)
        if  cur[0] < H-1:
            for i in range(cur[0],mov[0]+1):
                for k in range(cur[1],mov[1]+1):
                    if ban[i][k] == ".":
                        ban[i][k] = "#"
                    else:
                        ban[i][k] = "."
        else:
            for i in range(cur[0],mov[0]+1):
                for k in range(cur[1]+1,mov[1]+1):
                    if ban[i][k] == ".":
                        ban[i][k] = "#"
                    else:
                        ban[i][k] = "."
    # f(cur[0],cur[1])
    # print(list1)
    # print(cur)
    f2(cur[0],cur[1])
    mov = list4[list3.index(max(list3))]
    # print(mov)
    if  cur[0] < H-1:
        for i in range(cur[0]+1,mov[0]+1):
            for k in range(cur[1],mov[1]+1):
                if ban[i][k] == ".":
                    ban[i][k] = "#"
                else:
                    ban[i][k] = "."
    else:
        for i in range(cur[0],mov[0]+1):
            for k in range(cur[1]+1,mov[1]+1):
                if ban[i][k] == ".":
                    ban[i][k] = "#"
                else:
                    ban[i][k] = "."
    kai += 1
    cur = mov.copy()
    # print(ban)
    # print(clear)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

H, W = map(int, input().split())
field = ['' for _ in range(H)]
seen = [[False for _ in range(W)] for _ in range(H)]
seen_black = [[False for _ in range(W)] for _ in range(H)]

dx = [0, 1]
dy = [1, 0]
stop = (0, 0)
next_start = (0,0)
def dfs(h, w):
    global stop
    seen[h][w] = True
    for dir in range(2):
        nh = h + dx[dir]
        nw = w + dy[dir]
        
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if field[nh][nw] == '#':
            if nh < stop[0] or nw < stop[0]:continue
            if nh+nw > stop[0]+stop[1]:
                stop = (nh, nw)
            elif nh+nw == stop[0]+stop[1]:
                if stop[0] < nw and field[nh][nw:].count(".") > field[nh:][nw].count("."):
                    stop = (nh, nw)
                if stop[1] < nh and field[nh][nw:].count(".") < field[nh:][nw].count("."):
                    stop = (nh, nw)
            continue
        if seen[nh][nw] == True:
            continue
        dfs(nh, nw)
def dfs_black(h, w):
    global next_start
    seen_black[h][w] = True
    for dir in range(2):
        nh = h + dx[dir]
        nw = w + dy[dir]
        
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if field[nh][nw] == '.':
            if nh < next_start[0] or nw < next_start[0]:continue
            if nh+nw > next_start[0]+next_start[1]:
                next_start = (nh, nw)
            continue
        if seen_black[nh][nw] == True:
            continue
        dfs_black(nh, nw)    

for h in range(H):
    field[h] = input().rstrip("\r\n")

sh, sw, gh, gw = 0, 0, H-1, W-1
ans = 0
is_next = True
if field[sh][sw] == "#":
    is_next = False
else:
    is_next = True

while seen[gh][gw] != True and seen_black[gh][gw] != True:
    if is_next:
        dfs(sh,sw)
        sh, sw = stop
        is_next = False
    else:
        ans += 1
        dfs_black(sh,sw)
        sh, sw = next_start
        is_next = True
print(ans)

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(w)]
inf = h*w
d=[[inf]*h for i in range(w)]
if s[0][0]=="#":
    d[0][0]=1
else:
    d[0][0]=0
def tate(x,y):
    if x==0:
        return d[x][y]
    if s[x-1][y]=="#":
        return tate(x-1,y)
    return d[x-1][y]
def yoko(x,y):
    if y==0:
        return d[x][y]
    if s[x][y-1]=="#":
        return tate(x,y-1)
    return d[x][y-1]
for j in range(1,w):
    if s[0][j]=="#":
        d[0][j]=yoko(0,j)+1
    else:
        d[0][j]=d[0][j-1]
for i in range(1,h):
    if s[i][0]=="#":
        d[i][0]=tate(i,0)+1
    else:
        d[i][0]=d[i-1][0]
for i in range(1,h):
    for j in range(1,w):
        if s[i][j]=="#":
            d[i][j]=min(tate(i,j),yoko(i,j))+1
        else:
            d[i][j]=min(d[i-1][j],d[i][j-1])
print(d[-1][-1])

Submission status: Runtime Error

Solution code:
import numpy as np
import sys

sys.setrecursionlimit(10**9)

BLACK = '#'
WHITE = '.'

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


def dfs(h, w, s, visited):
    if not (0 <= h < W) or not (0 <= w < H) or s[h][w] == visited:
        return

    s[h][w] = visited

    dfs(h, w + 1, s, visited)  # 右を探索
    dfs(h + 1, w, s, visited)  # 下を探索


def flip(h, w, s):
    dfs(h, w, s, BLACK)
    dfs(h, w, s, WHITE)


ans = (s[0][0] == BLACK) + (s[H-1][W-1] == BLACK)
s[0][0] = WHITE; s[H-1][W-1] = WHITE

ss = s.copy()
dfs(0, 0, ss, BLACK)
if ss[H-1][W-1] == BLACK:
    print(ans)
    exit()

while (s == BLACK).any():
    flip(0, 0, s)
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
P=[input().split() for i in range(H)]
count=0
counts=0
for j in range(H):
    if j<H-2:
        for k in range(counts,W):
            if k <W-2:
                if P[j][k]=="#":
                    count=count+1
                    if (P[j][k+2]=="#" or P[j+1][k+2]=="#") and P[j][k+1]=="#":
                        counts=counts+1
                    elif (P[j+2][k]=="#" or P[j+1][k+1]=="#") and P[j+1][k]=="#":
                        counts=counts+1
                    else:
                        break
            elif k==W-2:
                if p[j][k]=="#":
                    count=count+1
                    if P[j][k+1]=="#":
                        counts=counts+1
                    elif P[j+1][k]=="#":
                        counts=counts+1
                    else:
                        break
            else:
                if P[j+1][k]=="#":
                    counts=counts+1
                else:
                    break
    elif j==H-2:
        for k in range(counts,W):
            if k <W-2:
                if P[j][k]=="#":
                    count=count+1
                    if (P[j][k+2]=="#" or P[j+1][k+2]=="#") and P[j][k+1]=="#":
                        counts=counts+1
                    elif P[j+1][k]=="#":
                        counts=counts+1
                    else:
                        break
            elif k==W-2:
                if p[j][k]=="#":
                    count=count+1
                    if P[j][k+1]=="#":
                        counts=counts+1
                    elif P[j+1][k]=="#":
                        counts=counts+1
                    else:
                        break
            else:
                if P[j+1][k]=="#":
                    counts=counts+1
                else:
                    break
    else:
        if P[j][k+1]=="#":
            counts=counts+1

print(count)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[[]]*h
t=[[0]*w]*h
for i in range(h):
  s[i]=list(map(int,input()))
if(s[0][0]=='.'):
  t[0][0]=1
else:
  t[0][0]=2
for i in range(1,w):
  if(s[0][i-1]==s[0][i]):
    t[0][i]=t[0][i-1]
  else:
    t[0][i]=t[0][i-1]+1
for i in range(1,h):
  if(s[i-1][0]==s[i][0]):
    t[i][0]=t[i-1][0]
  else:
    t[i][0]=t[i-1][0]+1
for i in range(2,h+w):
  for j in range(1,h+m):
    if(i-j>0):
      if(i-j<w && j<h):
        if(s[j][i-j-1]==s[j][i-j]):
          a=t[j][i-j-1]
        else:
          a=t[j][i-j-1]+1
        if(s[j-1][i-j]==s[j][i-1]):
          b=t[j-1][i-j]
        else:
          b=t[j-1][i-j]+1
        t[i][]=

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
L=[list(input()) for i in range(H)]

save=[[100000]*(W) for i in range(H)]

if L[0][0]=="#":
    save[0][0]=1
else:
    save[0][0]=0

H_LAST=H-1
W_LAST=W-1
    
for b in range(H-1):
    for r in range(W-1):
        nr=r+1
        nb=b+1

        if L[b][nr]=="#":
            save[b][nr]=min(save[b][r]+1,save[b][nr])
        else:
            save[b][nr]=min(save[b][r],save[b][nr])
            
        if L[nb][r]=="#":
            save[nb][r]=min(save[b][r]+1,save[nb][r])
        else:
            save[nb][r]=min(save[b][r],save[nb][r])
    print(b)
    for i in range(b,H_LAST):
        print(i)
        nb=i+1
        if L[nb][H_LAST]=="#":
            save[nb][H_LAST]=min(save[i][H_LAST]+1,save[nb][H_LAST])
        else:
            save[nb][H_LAST]=min(save[i][H_LAST],save[nb][H_LAST])
    print("###")
    
for i in range(r,W_LAST):
    nr=i+1
    if L[W_LAST][nr]=="#":
        save[W_LAST][nr]=min(save[W_LAST][i]+1,save[W_LAST][nr])
    else:
        save[nr][W_LAST]=min(save[W_LAST][i],save[W_LAST][nr])
        
print(save[H_LAST][W_LAST])

Submission status: Runtime Error

Solution code:
import numpy as np
N = int()
arr = np.array(list(map(int,input().split())))

for _ in range(N):
    diff = np.diff(arr)
    np.abs(diff)
a = (diff[0])
print(a)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
sai = []
a,b = 0,0
c = 0
for i in range(h):
    sai.append(list(input()))

while True:
    if sai[a][b] == "#":
        break
    else:
        if a == h - 1 and b == w - 1:
            print(0)
            exit()
        elif sai[a+1][b] == ".":
            a = a+1
        elif sai[a][b+1] == ".":
            b = b+1
        else:
            break
ans = []
ans_2 = []
c = 0
for i in range(w):
    ans.append(sai[i].count("."))
    for j in range(h):
        #print(sai[j][i])
        if sai[j][i] == ".":
            c += 1
    ans_2.append(c)
    c = 0
#for i in range(h):
 #   ans_2.append(sai[0][i].count("."))
#print(ans)
#print(ans_2)
f = max(ans) + max(ans_2)
print((h+w)-f)