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:
H, W = list(map(int, input().split()))
A = [list(list(input())) for _ in range(H)]

w = 0
h = 0
cnt = 1 if A[h][w] == '#' else 0


def ccc(aa, ww):
    count = -1
    for a in aa:
        count += a[ww:].count('.')
    return count


while True:
    right = ccc(A[h:], w + 1)
    bottom = ccc(A[h + 1:], w)
    if right == -1 and bottom == -1:
        w += 1
    else:
        if right > bottom:
            w += 1
        else:
            h += 1

    if A[h][w] == '#':
        cnt += 1

    if w == W - 1 and h == H - 1:
        break

print(cnt)

Submission status: Runtime Error

Solution code:
def calc(P):
    #Pはリスト
    Q=[]
    for str in A:
        if str=="1":
            pass
        elif str=="2":
            Q.append("1")
        else:
            Q.append("2")
    return Q

def solve(P):
    #Pはリスト
    N=len(P)
    if N>1:
        Q=[]
        for i in range(N-1):
            if   abs( int(P[i])-int(P[i+1]) )==0:
                Q.append("0")
            elif abs( int(P[i])-int(P[i+1]) )==1:
                Q.append("1")
            else:
                Q.append("2")
        return solve(Q)
    elif N==0:
    	return(0)
    else:
        return P[0]

N=int(input())
A=input()
B=list(A)

#print(A)
#print(B) 
short=calc(B)
#print(short)

ans=solve(short)
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
 
w, h = map(int, [int(i) for i in input().split()])
s = []
for _ in range(h):
    s.append(input())
 
x, y, cnt = map(int, [0, 0, 0])
if s[0][0] == '#':
    cnt += 1
 
while([x,y] != [w-1,h-1]):
    if s[y+1][x] == '.':
        y += 1
    elif s[y][x+1] == '.':
        x += 1
    else:
        if h-y-1 > w-x-1:
            y += 1
            cnt += 1
        else:
            x += 1
            cnt += 1
 
print(cnt)

Submission status: Runtime Error

Solution code:
h, w = [int(x) for x in input().split()]
input_ = [list(input()) for x in range(h)]
memo = [[0 for i in range(w)] for _ in range(h)]


for i in range(w):
  w_ = input_[i]
  if i == 0:
    for x in range(len(w_)):
      if x == 0:
        if w_[x] == '#':
          memo[i][x] = 1
      else:
        if w_[x] == '#':
          memo[i][x] = memo[i][x-1] + 1
        else:
          memo[i][x] = memo[i][x-1]
  else:
    for x in range(len(w_)):
      if w_[x] == '#':
        if x==0:
          a = 10**9
          b = memo[i-1][x] + 1
        else:
          try:
            a = memo[i][x-1] + 1
          except:
            a = 10**9
          try:
            b = memo[i-1][x] + 1
          except:
            b = 10**9

        memo[i][x] = min(a, b)
      else:
        if x==0:
          a = 10**9
          b = memo[i-1][x]
        else:
          try:
            a = memo[i][x-1]
          except:
            a = 10**9
          try:
            b = memo[i-1][x]
          except:
            b = 10**9
        memo[i][x] = min(a, b)




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

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


# global min_r
min_r = 200

def solve(H: int, W: int, s: "List[str]"):
    l = H+W
    # global min_r
    def cal_c(cmp, y, x, pre):
        global min_r

        if cmp < min_r :
            k = ((pre==".")&(s[y][x]=="#"))*1
            if y < H-1:
                cal_c(cmp+k, y+1, x, s[y][x])
            if x < W-1:
                cal_c(cmp+k, y, x+1, s[y][x])

            if (((y==H-1)&(x==W-1))&(cmp < min_r)):
                if (pre ==".")&(s[y][x]=="#") :
                    min_r = cmp+1
                else :
                    min_r = cmp

            # print(cmp)

    cal_c(0, 0, 0, ".")
    print(min_r)


    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(W)]  # type: "List[str]"
    solve(H, W, s)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def checkpath(h,w,table,max_bomb):
  result = False
  x = 0
  y = 0
  bomb = max_bomb
  # x先行パターン
  while True:
    # # print("x:{} y:{} bomb:{}".format(x,y,bomb))
    init = False
    if x == w and y == h:
      # 端についたらゴール
      result = True
      break
    if bomb > 0 and init == False and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1
      init = True
    elif bomb > 0 and ((x==w and y==h-1) or (x==w-1 and y==h)) and table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
      result = True
      break
    elif x<w and table[x+1][y] == '.':
      # # print('右に進む')
      x+=1
    elif y<h and table[x][y+1] == '.':
      # # print(' 下に進む')
      y+=1
    elif bomb > 0 and x<w-1 and table[x+1][y] == '#' and table[x+2][y] == '.':
      # # print(' 右にボムから右に行く')
      bomb-=1
      x+=2
    elif bomb > 0 and  x < w and y < h and table[x+1][y] == '#' and table[x+1][y+1] == '.':
      # # print(' 右にボムから下に行く')
      bomb-=1
      x+=1
      y+=1
    elif bomb > 0 and y<h and x < w and table[x][y+1] == '#' and table[x+1][y+1] == '.':
      # # print(' 下にボムから右に行く')
      bomb-=1
      y+=1
      x+=1
    elif bomb > 0 and  y < h-1 and table[x][y+1] == '#' and table[x][y+2] == '.':
      # # print(' 下にボムから下に行く')
      bomb-=1
      y+=2
    else:
      break
  if result:
    return result
  # y先行パターン
  while True:
    # # print("x:{} y:{}".format(x,y))
    init = False
    if x == w and y == h:
      result = True
      break
    if bomb > 0 and init == False and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1
      init = True
    elif bomb > 0 and ((x==w and y==h-1) or (x==w-1 and y==h)) and table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
      result = True
      break
    elif y<h and table[x][y+1] == '.':
      # # print(' 下に進む')
      y+=1
    elif x<w and table[x+1][y] == '.':
      # # print(' 右に進む')
      x+=1
    elif bomb > 0 and x<w-1 and table[x+1][y] == '#' and table[x+2][y] == '.':
      # # print(' 右にボムから右に行く')
      bomb-=1
      x+=2
    elif bomb > 0 and  x<w and y < h and table[x+1][y] == '#' and table[x+1][y+1] == '.':
      # # print(' 右にボムから下に行く')
      bomb-=1
      x+=1
      y+=1
    elif bomb > 0 and y<h and x < w and table[x][y+1] == '#' and table[x+1][y+1] == '.':
      # # print(' 下にボムから右に行く')
      bomb-=1
      y+=1
      x+=1
    elif bomb > 0 and  y < h-1 and table[x][y+1] == '#' and table[x][y+2] == '.':
      # # print(' 下にボムから下に行く')
      bomb-=1
      y+=2
    else:
      break
  return result

# メイン関数
# 入力からテーブル作成
hw = input('')
h = int(hw.split(' ')[0])
w = int(hw.split(' ')[1])
table = []
for i in range(0,h):
  line = input('')
  r = []
  for j in range(0,w):
    r.append(line[j])
  table.append(r)

for c in range(0,h*w):
  # # print("bomb:{}".format(c))
  if checkpath(h-1,w-1,table,c):
    # # print('Success!!')
    print(c)
    break

Submission status: Runtime Error

Solution code:
def checkpath(h,w,table,max_bomb):
  result = False
  x = 0
  y = 0
  bomb = max_bomb
  # x先行パターン
  while True:
    # # print("x:{} y:{} bomb:{}".format(x,y,bomb))
    init = False
    if x == w and y == h:
      # 端についたらゴール
      result = True
      break
    if bomb > 0 and init == False and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1
      init = True
    elif bomb > 0 and ((x=w and y=h-1) or (x=w-1 and y=h)) and table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
      result = True
      break
    elif x<w and table[x+1][y] == '.':
      # # print('右に進む')
      x+=1
    elif y<h and table[x][y+1] == '.':
      # # print(' 下に進む')
      y+=1
    elif bomb > 0 and x<w-1 and table[x+1][y] == '#' and table[x+2][y] == '.':
      # # print(' 右にボムから右に行く')
      bomb-=1
      x+=2
    elif bomb > 0 and  x < w and y < h and table[x+1][y] == '#' and table[x+1][y+1] == '.':
      # # print(' 右にボムから下に行く')
      bomb-=1
      x+=1
      y+=1
    elif bomb > 0 and y<h and x < w and table[x][y+1] == '#' and table[x+1][y+1] == '.':
      # # print(' 下にボムから右に行く')
      bomb-=1
      y+=1
      x+=1
    elif bomb > 0 and  y < h-1 and table[x][y+1] == '#' and table[x][y+2] == '.':
      # # print(' 下にボムから下に行く')
      bomb-=1
      y+=2
    else:
      break
  if result:
    return result
  # y先行パターン
  while True:
    # # print("x:{} y:{}".format(x,y))
    init = False
    if x == w and y == h:
      result = True
      break
    if bomb > 0 and init == False and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1
      init = True
    elif bomb > 0 and ((x=w and y=h-1) or (x=w-1 and y=h)) and table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
      result = True
      break
    elif y<h and table[x][y+1] == '.':
      # # print(' 下に進む')
      y+=1
    elif x<w and table[x+1][y] == '.':
      # # print(' 右に進む')
      x+=1
    elif bomb > 0 and x<w-1 and table[x+1][y] == '#' and table[x+2][y] == '.':
      # # print(' 右にボムから右に行く')
      bomb-=1
      x+=2
    elif bomb > 0 and  x<w and y < h and table[x+1][y] == '#' and table[x+1][y+1] == '.':
      # # print(' 右にボムから下に行く')
      bomb-=1
      x+=1
      y+=1
    elif bomb > 0 and y<h and x < w and table[x][y+1] == '#' and table[x+1][y+1] == '.':
      # # print(' 下にボムから右に行く')
      bomb-=1
      y+=1
      x+=1
    elif bomb > 0 and  y < h-1 and table[x][y+1] == '#' and table[x][y+2] == '.':
      # # print(' 下にボムから下に行く')
      bomb-=1
      y+=2
    else:
      break
  return result

# メイン関数
# 入力からテーブル作成
hw = input('')
h = int(hw.split(' ')[0])
w = int(hw.split(' ')[1])
table = []
for i in range(0,h):
  line = input('')
  r = []
  for j in range(0,w):
    r.append(line[j])
  table.append(r)

for c in range(0,h*w):
  # # print("bomb:{}".format(c))
  if checkpath(h-1,w-1,table,c):
    # # print('Success!!')
    print(c)
    break

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)

def maze(r, c, MAP, count, shaf, H, W):
    #print(r, c, count, shaf)
    if r == H-1 and c == W-1:
        if shaf==1 and MAP[r][c]=='#':
            print(count)
            exit()

        if shaf==0 and MAP[r][c]=='.':
            print(count)
            exit()

    if r==H or c==W:
        return
    
    if shaf==0:
        if r != H-1 and MAP[r+1][c] == '.':
            maze(r+1, c, MAP, count, shaf, H, W)
        elif c != W-1 and MAP[r][c+1] == '.':
            maze(r, c+1, MAP, count, shaf, H, W)
        else:
            if r != H-1:
                maze(r+1, c, MAP, count+1, 1, H, W)
            if c != W-1:
                maze(r, c+1, MAP, count+1, 1, H, W)

    if shaf==1:
        if r != H-1 and MAP[r+1][c] == '#':
            maze(r+1, c, MAP, count, shaf, H, W)
        elif c != W-1 and MAP[r][c+1] == '#':
            maze(r, c+1, MAP, count, shaf, H, W)
        else:
            if r != H-1:
                maze(r+1, c, MAP, count, 0, H, W)
            if c != W-1:
                maze(r, c+1, MAP, count, 0, H, W)


def main():
    H, W = list(map(int, input().split()))

    MAP = [[int() for x in range(H)] for xx in range(W)]
    for i in range(W):
        MAP[i] = input()


    count = 0
    shaf = 0
    if MAP[0][0]=='#':
        shaf = 1
        count = 1

    print(maze(0, 0, MAP, count, shaf, H, W))


main()

Submission status: Runtime Error

Solution code:
import sys
sys.getrecursionlimit(2147483647)

h, w = map(int, input().split())
board = []
for i in range(h):
    board.append(list(input()))

ds = [(1, 0), (0, 1), (-1, 0), (0, -1)]
ans = []

def dfs(x, y, m):
    if board[y][x] == "#":
        m += 1
    if x == w - 1 and y == h - 1:
        ans.append(m)
        return
    temp = board[y][x]
    for (dx, dy) in ds:
        board[y][x] = "x"
        if -1 < x + dx < w and -1 < y + dy < h and board[y + dy][x + dx] != "x":
            dfs(x + dx, y + dy, m)
        board[y][x] = temp
        
dfs(0, 0, 0)
print(min(ans))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase

def main():
    n =int(input())
    a = list(map(int,input()))
    if n<=10000:
        new = a.copy()
        for i in range(n-1):
            new1 = new.copy()
            for j in range(1,n-i):
                new[j] = abs(new1[j]-new1[j-1])
            # print(new)
            new.pop(0)
            # print(new)
        print(new[0])
    else:
        m = 99
        for i in range(1,len(a)):
            m = min(m,abs(a[i]-a[i-1]))
        print(m)





# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
table=[[0]*W for i in range(H)]
table2=[[0]*W for i in range(H)]
for i in range(H):
  s=str(input())
  for j in range(W):
    if s[j]=='#':
      table[i][j]=1
    else:
      table2[i][j]=1
for i in range(H):
  for j in range(W):
    if i==0:
      if j==0:
        pass
      else:
        table[i][j]+=table[i][j-1]
        table2[i][j]+=table2[i][j-1]
    else:
      if j>=1:
        x,y=table[i-1][j],table[i][j-1]
        table[i][j]+=min(x,y)
        X,Y=table2[i-1][j],table[2][j-1]
        table2[i][j]+=min(X,Y)
      else:
        table[i][j]+=table[i-1][j]
        table2[i][j]+=table2[i-1][j]
ans1=table[H-1][W-1]
ans2=table2[H-1][W-1]+1
print(min(ans1,ans2))

Submission status: Runtime Error

Solution code:

H, W = map(int,input().split())

A = []

point = 0
for i in range(H):
    P = input()
    P = list(P)
    if point == 0:
        A.append(P)
    else:
        A.vstack(P)
        
if A[0][0] == '.':
    num = 0
else:
    num = 1
if A[H-1][W-1] == '.':
    o = 1
else:
    num += 1
h = 0
w = 0
if H > W:
    while point <= W-2:
        if A[h+1][w] == '.':
            h += 1
        elif A[h][w+1] == '.':
            w += 1
            point += 1
        else:
            num += 1
            h += 1
            w += 1
            point += 1
    for i in range(H-W):
        if A[i+W][W-1] == '#':
            num +=1
            
elif H < W:
    while point <= H-2:
        if A[h+1][w] == '.':
            h += 1
            point += 1
        elif A[h][w+1] == '.':
            w += 1
            
        else:
            num += 1
            h += 1
            w += 1
            point += 1
        if A[H-1][i+H] == '#':
            num +=1
else:
    while point <= W-2:
        if A[h+1][w] == '.':
            h += 1
            point += 1
        elif A[h][w+1] == '.':
            w += 1
            point += 1
        else:
            num += 1
            h += 1
            w += 1
            point += 1
print(num)

Submission status: Runtime Error

Solution code:
def checkpath(h,w,table,max_bomb):
  result = False
  x = 0
  y = 0
  bomb = max_bomb
  # x先行パターン
  while True:
    # # print("x:{} y:{} bomb:{}".format(x,y,bomb))
    init = False
    if x == w and y == h:
      # 端についたらゴール
      result = True
      break
    if bomb > 0 and init == False and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1
      init = True
    elif bomb > 0 and ((x=w and y=h-1) or (x=w-1 and y=h)) table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
      result = True
      break
    elif x<w and table[x+1][y] == '.':
      # # print('右に進む')
      x+=1
    elif y<h and table[x][y+1] == '.':
      # # print(' 下に進む')
      y+=1
    elif bomb > 0 and x<w-1 and table[x+1][y] == '#' and table[x+2][y] == '.':
      # # print(' 右にボムから右に行く')
      bomb-=1
      x+=2
    elif bomb > 0 and  x < w and y < h and table[x+1][y] == '#' and table[x+1][y+1] == '.':
      # # print(' 右にボムから下に行く')
      bomb-=1
      x+=1
      y+=1
    elif bomb > 0 and y<h and x < w and table[x][y+1] == '#' and table[x+1][y+1] == '.':
      # # print(' 下にボムから右に行く')
      bomb-=1
      y+=1
      x+=1
    elif bomb > 0 and  y < h-1 and table[x][y+1] == '#' and table[x][y+2] == '.':
      # # print(' 下にボムから下に行く')
      bomb-=1
      y+=2
    else:
      break
  if result:
    return result
  # y先行パターン
  while True:
    # # print("x:{} y:{}".format(x,y))
    init = False
    if x == w and y == h:
      result = True
      break
    if bomb > 0 and init == False and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1
      init = True
    elif bomb > 0 and ((x=w and y=h-1) or (x=w-1 and y=h)) table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
      result = True
      break
    elif y<h and table[x][y+1] == '.':
      # # print(' 下に進む')
      y+=1
    elif x<w and table[x+1][y] == '.':
      # # print(' 右に進む')
      x+=1
    elif bomb > 0 and x<w-1 and table[x+1][y] == '#' and table[x+2][y] == '.':
      # # print(' 右にボムから右に行く')
      bomb-=1
      x+=2
    elif bomb > 0 and  x<w and y < h and table[x+1][y] == '#' and table[x+1][y+1] == '.':
      # # print(' 右にボムから下に行く')
      bomb-=1
      x+=1
      y+=1
    elif bomb > 0 and y<h and x < w and table[x][y+1] == '#' and table[x+1][y+1] == '.':
      # # print(' 下にボムから右に行く')
      bomb-=1
      y+=1
      x+=1
    elif bomb > 0 and  y < h-1 and table[x][y+1] == '#' and table[x][y+2] == '.':
      # # print(' 下にボムから下に行く')
      bomb-=1
      y+=2
    else:
      break
  return result

# メイン関数
# 入力からテーブル作成
hw = input('')
h = int(hw.split(' ')[0])
w = int(hw.split(' ')[1])
table = []
for i in range(0,h):
  line = input('')
  r = []
  for j in range(0,w):
    r.append(line[j])
  table.append(r)

for c in range(0,h*w):
  # # print("bomb:{}".format(c))
  if checkpath(h-1,w-1,table,c):
    # # print('Success!!')
    # print(c)
    import copy

def checkpath(h,w,table,max_bomb):
  result = False
  x = 0
  y = 0
  bomb = max_bomb
  # x先行パターン
  while True:
    # print("x:{} y:{} bomb:{}".format(x,y,bomb))
    init = False
    if x == w and y == h:
      # 端についたらゴール
      result = True
      break
    if bomb > 0 and init == False and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1
      init = True
    elif bomb > 0 and ((x==w and y==h-1) or (x==w-1 and y==h)) and table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
      result = True
      break
    elif x<w and table[x+1][y] == '.':
      # print('右に進む')
      x+=1
    elif y<h and table[x][y+1] == '.':
      # print(' 下に進む')
      y+=1
    elif bomb > 0 and x<w-1 and table[x+1][y] == '#' and table[x+2][y] == '.':
      # print(' 右にボムから右に行く')
      bomb-=1
      x+=2
    elif bomb > 0 and  x < w and y < h and table[x+1][y] == '#' and table[x+1][y+1] == '.':
      # print(' 右にボムから下に行く')
      bomb-=1
      x+=1
      y+=1
    elif bomb > 0 and y<h and x < w and table[x][y+1] == '#' and table[x+1][y+1] == '.':
      # print(' 下にボムから右に行く')
      bomb-=1
      y+=1
      x+=1
    elif bomb > 0 and  y < h-1 and table[x][y+1] == '#' and table[x][y+2] == '.':
      # print(' 下にボムから下に行く')
      bomb-=1
      y+=2
    else:
      break
  if result:
    return result
  # y先行パターン
  while True:
    # print("x:{} y:{}".format(x,y))
    init = False
    if x == w and y == h:
      result = True
      break
    if bomb > 0 and init == False and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1
      init = True
    elif bomb > 0 and ((x==w and y==h-1) or (x==w-1 and y==h)) and table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
      result = True
      break
    elif y<h and table[x][y+1] == '.':
      # print(' 下に進む')
      y+=1
    elif x<w and table[x+1][y] == '.':
      # print(' 右に進む')
      x+=1
    elif bomb > 0 and x<w-1 and table[x+1][y] == '#' and table[x+2][y] == '.':
      # print(' 右にボムから右に行く')
      bomb-=1
      x+=2
    elif bomb > 0 and  x<w and y < h and table[x+1][y] == '#' and table[x+1][y+1] == '.':
      # print(' 右にボムから下に行く')
      bomb-=1
      x+=1
      y+=1
    elif bomb > 0 and y<h and x < w and table[x][y+1] == '#' and table[x+1][y+1] == '.':
      # print(' 下にボムから右に行く')
      bomb-=1
      y+=1
      x+=1
    elif bomb > 0 and  y < h-1 and table[x][y+1] == '#' and table[x][y+2] == '.':
      # print(' 下にボムから下に行く')
      bomb-=1
      y+=2
    else:
      break
  return result

# メイン関数
# 入力からテーブル作成
hw = input('')
h = int(hw.split(' ')[0])
w = int(hw.split(' ')[1])
table = []
for i in range(0,h):
  line = input('')
  r = []
  for j in range(0,w):
    r.append(line[j])
  table.append(r)

for c in range(0,h*w):
  # print("bomb:{}".format(c))
  if checkpath(h-1,w-1,table,c):
    # print('Success!!')
    print(c)
    breakbreak

Submission status: Runtime Error

Solution code:
def main():



    from collections import deque


    h ,w = map(int, input().split())
    a = [ list(input())  for _ in range(h)]

    p = deque()
    q = deque()

    n = a[0][0]=='.'
    if n:
        p.append((0,0,0))
    else:
        q.append((0,0,1))

    while(True):
        if p:
            f = p.pop()
            x, y, c = f[0], f[1], f[2]
        else:
            f = q.pop()
            x, y, c = f[0], f[1], f[2]

        for u, v in [(1,0), (0,1)]:
            if x + u <= w - 1 and y + v <= h - 1:
                if x+u == w-1 and y+v == h-1:
                    n = a[w-1][h-1]=='#'
                    print(c+n)
                    exit()

                n = a[x+u][y+v]=='.'
                if n:
                    p.append((x+u, y+v, c+0))
                else:
                    q.append((x + u, y + v, c+1))



if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
h, w = [int(x) for x in input().split()]
input_ = [list(input()) for x in range(w)]
memo = [[0 for i in range(w)] for _ in range(h)]


for i in range(w):
  w_ = input_[i]
  if i == 0:
    for x in range(len(w_)):
      if x == 0:
        if w_[x] == '#':
          memo[i][x] = 1
      else:
        if w_[x] == '#':
          memo[i][x] = memo[i][x-1] + 1
        else:
          memo[i][x] = memo[i][x-1]
  else:
    for x in range(len(w_)):
      if w_[x] == '#':
        if x==0:
          a = 10**9
          b = memo[i-1][x] + 1
        else:
          try:
            a = memo[i][x-1] + 1
          except:
            a = 10**9
          try:
            b = memo[i-1][x] + 1
          except:
            b = 10**9

        memo[i][x] = min(a, b)
      else:
        if x==0:
          a = 10**9
          b = memo[i-1][x]
        else:
          try:
            a = memo[i][x-1]
          except:
            a = 10**9
          try:
            b = memo[i-1][x]
          except:
            b = 10**9
        memo[i][x] = min(a, b)




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

Submission status: Runtime Error

Solution code:
import sys
sys.getrecursionlimit()

h, w = map(int, input().split())
board = []
for i in range(h):
    board.append(list(input()))

ds = [(1, 0), (0, 1), (-1, 0), (0, -1)]
ans = []

def dfs(x, y, m):
    if board[y][x] == "#":
        m += 1
    if x == w - 1 and y == h - 1:
        ans.append(m)
        return
    temp = board[y][x]
    for (dx, dy) in ds:
        board[y][x] = "x"
        if -1 < x + dx < w and -1 < y + dy < h and board[y + dy][x + dx] != "x":
            dfs(x + dx, y + dy, m)
        board[y][x] = temp
        
dfs(0, 0, 0)
print(min(ans))

Submission status: Runtime Error

Solution code:
h, w = [int(x) for x in input().split()]
input_ = [list(input()) for x in range(w)]
memo = [[0 for i in range(w)] for _ in range(h)]


for i in range(w):
  w_ = input_[i]
  if i == 0:
    for x in range(len(w_)):
      if x == 0:
        if w_[x] == '#':
          memo[i][x] = 1
      else:
        if w_[x] == '#':
          memo[i][x] = memo[i][x-1] + 1
        else:
          memo[i][x] = memo[i][x-1]
  else:
    for x in range(len(w_)):
      if w_[x] == '#':
        if x==0:
          a = 10**9
          b = memo[i-1][x] + 1
        else:
          try:
            a = memo[i][x-1] + 1
          except:
            a = 10**9
          try:
            b = memo[i-1][x] + 1
          except:
            b = 10**9

        memo[i][x] = min(a, b)
      else:
        if x==0:
          a = 10**9
          b = memo[i-1][x]
        else:
          try:
            a = memo[i][x-1]
          except:
            a = 10**9
          try:
            b = memo[i-1][x]
          except:
            b = 10**9
        memo[i][x] = min(a, b)




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

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


# global min_r
min_r = 200

def solve(H: int, W: int, s: "List[str]"):
    l = H+W
    # global min_r
    def cal_c(cmp, y, x, pre):
        global min_r

        if cmp < l :
            if y < H-1:
                cal_c(cmp+((pre==".")&(s[y][x]=="#"))*1, y+1, x, s[y][x])
            if x < W-1:
                cal_c(cmp+((pre==".")&(s[y][x]=="#"))*1, y, x+1, s[y][x])

            if (((y==H-1)&(x==W-1))&(cmp < min_r)):
                if (pre ==".")&(s[y][x]=="#") :
                    min_r = cmp+1
                else :
                    min_r = cmp

            # print(cmp)

    cal_c(0, 0, 0, ".")
    print(min_r)


    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(W)]  # type: "List[str]"
    solve(H, W, s)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def checkpath(h,w,table,max_bomb):
  result = False
  x = 0
  y = 0
  bomb = max_bomb
  # x先行パターン
  while True:
    # print("x:{} y:{} bomb:{}".format(x,y,bomb))
    init = False
    if x == w and y == h:
      # 端についたらゴール
      result = True
      break
    if bomb > 0 and init == False and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1
      init = True
    elif bomb > 0 and ((x=w and y=h-1) or (x=w-1 and y=h)) table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
      result = True
      break
    elif x<w and table[x+1][y] == '.':
      # print('右に進む')
      x+=1
    elif y<h and table[x][y+1] == '.':
      # print(' 下に進む')
      y+=1
    elif bomb > 0 and x<w-1 and table[x+1][y] == '#' and table[x+2][y] == '.':
      # print(' 右にボムから右に行く')
      bomb-=1
      x+=2
    elif bomb > 0 and  x < w and y < h and table[x+1][y] == '#' and table[x+1][y+1] == '.':
      # print(' 右にボムから下に行く')
      bomb-=1
      x+=1
      y+=1
    elif bomb > 0 and y<h and x < w and table[x][y+1] == '#' and table[x+1][y+1] == '.':
      # print(' 下にボムから右に行く')
      bomb-=1
      y+=1
      x+=1
    elif bomb > 0 and  y < h-1 and table[x][y+1] == '#' and table[x][y+2] == '.':
      # print(' 下にボムから下に行く')
      bomb-=1
      y+=2
    else:
      break
  if result:
    return result
  # y先行パターン
  while True:
    # print("x:{} y:{}".format(x,y))
    init = False
    if x == w and y == h:
      result = True
      break
    if bomb > 0 and init == False and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1
      init = True
    elif bomb > 0 and ((x=w and y=h-1) or (x=w-1 and y=h)) table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
      result = True
      break
    elif y<h and table[x][y+1] == '.':
      # print(' 下に進む')
      y+=1
    elif x<w and table[x+1][y] == '.':
      # print(' 右に進む')
      x+=1
    elif bomb > 0 and x<w-1 and table[x+1][y] == '#' and table[x+2][y] == '.':
      # print(' 右にボムから右に行く')
      bomb-=1
      x+=2
    elif bomb > 0 and  x<w and y < h and table[x+1][y] == '#' and table[x+1][y+1] == '.':
      # print(' 右にボムから下に行く')
      bomb-=1
      x+=1
      y+=1
    elif bomb > 0 and y<h and x < w and table[x][y+1] == '#' and table[x+1][y+1] == '.':
      # print(' 下にボムから右に行く')
      bomb-=1
      y+=1
      x+=1
    elif bomb > 0 and  y < h-1 and table[x][y+1] == '#' and table[x][y+2] == '.':
      # print(' 下にボムから下に行く')
      bomb-=1
      y+=2
    else:
      break
  return result

# メイン関数
# 入力からテーブル作成
hw = input('')
h = int(hw.split(' ')[0])
w = int(hw.split(' ')[1])
table = []
for i in range(0,h):
  line = input('')
  r = []
  for j in range(0,w):
    r.append(line[j])
  table.append(r)

for c in range(0,h*w):
  # print("bomb:{}".format(c))
  if checkpath(h-1,w-1,table,c):
    # print('Success!!')
    print(c)
    break