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:
import math

H,W=map(int,input().split())
maps=[]
for i in range(H):
    maps.append(list(input()))



flag=True
que=[]

now=[0,0,0]
result=[]
if maps[0][0]=="#":
    now=[0,0,1]

while flag:

    if now[0]==H-1 and now[1]==W-1:

        result.append(now)


        if not que:
            flag=False
            continue
        else:
            now=que.pop()


    if now[0]+1<W:
        if maps[now[0]+1][now[1]]==".":
            que.append([now[0]+1,now[1],now[2]])

        else:
            que.append([now[0]+1,now[1],now[2]+1])

    if now[1]+1<H:
        if maps[now[0]][now[1]+1]==".":
            que.append([now[0],now[1]+1,now[2]])

        else:
            que.append([now[0],now[1]+1,now[2]+1])

    if not que:
        flag=False
        continue
    now=que.pop()


ans=result[0][2]
for i in result:
    if i[2] <ans:
        ans=i[2]
print(ans)

Submission status: Runtime Error

Solution code:
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:
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:
            maze(r+1, c, MAP, count+1, 1, H, W)
            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:
            maze(r+1, c, MAP, count, 0, H, W)
            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:
H,W=map(int,input().split())

a=[[0]*W for i in range(H)]
b=[[0]*W for i in range(H)]


for i in range(H):
	a[i]=input().split(",")

for j in range(1,W):
	b[0][j]=b[0][j-1]+int(not(a[0][j]==a[0][j-1]))

for i in range(1,H):
	b[i][0]=b[i-1][0]+int(not(a[i-1][0]==a[i][0]))
	for j in range(1,W):
		b[i][j]=min(b[i-1][j]+int(not(a[i-1][j]==a[i][j])),b[i][j-1]+int(not(a[i][j-1]==a[i][j])))
	
print(b[H-1][W-1])

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,n):
            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:
import sys
input = sys.stdin.readline

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 = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
stop = (0, 0)
next_start = (0,0)
def dfs(h, w):
    global stop
    seen[h][w] = True
    for dir in range(4):
        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] == '#':
            step = max(stop[0]+stop[1], nh+nw)
            if step != stop[0]+stop[1]: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(4):
        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] == '.':
            step = max(next_start[0]+next_start[1], nh+nw)
            if step != 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
        # if sh == gh and sw == gw:
        #     break
        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 = []
ans = 0
for i in range(H):
    s = list(input())
    S.append(s)
#横に何回移動したか縦に何回移動したかでDP
dp = [[0 for i in range(H)]for j in range(W)]
if S[0][0]=='#':
    dp[0][0]=1
if H==2:
    if S[0][1]=='#':
        dp[0][1]=dp[0][0]+1
    else:
        dp[0][1]=dp[0][0]
else:
    for i in range(1, H):
        if S[0][i]=='#':
            dp[0][i]=dp[0][i-1]+1
        else:
            dp[0][i]=dp[0][i-1]
    
if W==2:
    if S[1][0]=='#':
        dp[1][0]=dp[0][0]+1
    else:
        dp[1][0]=dp[0][0]
else:
    for j in range(1, W):
        if S[j][0]=='#':
            dp[j][0]=dp[j-1][0]+1
        else:
            dp[j][0]=dp[j-1][0]
#print(dp)
        
for i in range(1,H):
    for j in range(1,W):
        #print(i,j)
        #print(S[i][j])
        if S[i][j]=='#':
            dp[i][j]=min(dp[i-1][j],dp[i][j-1])+1
            #print(dp)
        else:
            dp[i][j]=min(dp[i-1][j],dp[i][j-1])
            #print(dp)
            
ans = dp[-1][-1]
    
print(ans)

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]"):

    global min_r
    def cal_c(cmp, y, x, pre):
        global min_r
        # print(y,x, cmp)

        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 main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = list(map(int,input()))
  import copy
  for _ in range(n-1):
    b=[0]*(len(a)-1)
    for i in range(len(a)-1):
      b[i]=abs(a[i]-a[i+1])
      #print(b)
    a = copy.deepcopy(b)
    #print(a)

  print(b[0])
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

dfs(0, 0, 0)
print(min(ans))

Submission status: Runtime Error

Solution code:
"""
ID: takesh.1
LANG: PYTHON2
TASK: task1
"""


gr = []

maxw = 100
bw = maxw+1
MAXX = maxw*maxw+1

w, h = map(int, raw_input().split(" "))
wei = []
for i in range(h):
    wwei = [bw]*w
    gr.append(raw_input())
    for j, c in enumerate(gr[i]):
        if c == ".":
            wwei[j] = 1
        #else:
        #    wwei[j] = 1
    wei.append(wwei)

def calcW(ww, hh):
    if (ww == 0) and (hh == 0):
        return wei[ww][hh]

    if (ww < 0) or (hh < 0):
        return MAXX

    wLeft = calcW(ww-1, hh)
    wUp = calcW(ww, hh-1)
    return wei[ww][hh] + min(wLeft, wUp)

weiz = calcW(w-1, h-1)
out = int(weiz/100)

print(str(out)+"\n")

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

w, h = [int(i) for i in input().split()]
s = []
for _ in range(h):
    s.append(input())

x, y, cnt = 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:
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))
    if x == w and y == h:
      # 端についたらゴール
      result = True
      break
    if bomb > 0 and table[0][0] == '#':
      # 現在地が黒ならボムで
      table[0][0] = '.' 
      bomb -=1      
    elif bomb > 0 and table[w][h] == '#':
      # ゴールが黒ならボムで
      table[w][h] = '.' 
      bomb -=1  
    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
      table[x+1][y]='.'
      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
      table[x][y+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
      table[x][y+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
      table[x][y+1]='.'
      y+=2
    else:
      break
  if result:
    return result
  # y先行パターン
  while True:
    # print("x:{} y:{}".format(x,y))
    if x == w and y == h:
      result = True
      break
    if bomb > 0 and table[0][0] == '#':
      # 現在地が黒ならボムで
      bomb -=1  
    elif bomb > 0 and table[w][h] == '#':
      # ゴールが黒ならボムで
      bomb -=1
    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
      table[x+1][y]='.'
      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
      table[x][y+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
      table[x][y+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
      table[x][y+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):
  tmp = copy.deepcopy(table)
  # print("bomb:{}".format(c))
  if checkpath(h-1,w-1,tmp,c):
    # print('Success!!')
    print(c)
    break

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2147483647)

n = int(input())
a = input()

for k in range(n-1):
    for v in range(n-1-k):
        x1 = a[v] - a[v+1]
        if x1 <= 0:
            x1 = -x1
        ans += str(x1)
    a = ans
    ans = ""
print(a[0])

Submission status: Runtime Error

Solution code:
import math

H,W=map(int,input().split())
maps=[]
for i in range(H):
    maps.append(list(input()))

flag=True
que=[]
quenot=[]


now=[0,0,0]
count=0
rireki=[]




result=[]
if maps[0][0]=="#":
    count+=1
    now=[0,0,1]

while flag:

    if now[0]==H-1 and now[1]==W-1:

        result.append(now)


        if not que:
            flag=False
            continue
        else:
            now=que.pop()


    if now[0]+1<W:
        if maps[now[0]+1][now[1]]==".":
            que.append([now[0]+1,now[1],now[2]])

        else:
            que.append([now[0]+1,now[1],now[2]+1])
            quenot.append([now[0]+1,now[1],now[2]+1])
    if now[1]+1<H:
        if maps[now[0]][now[1]+1]==".":
            que.append([now[0],now[1]+1,now[2]])

        else:
            que.append([now[0],now[1]+1,now[2]+1])
            quenot.append([now[0],now[1]+1,now[2]])
    if not que:
        flag=False
        continue
    now=que.pop()


ans=result[0][2]
for i in result:
    if i[2] <ans:
        ans=i[2]
print(ans)

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:
            maze(r+1, c, MAP, count+1, 1, H, W)
            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:
            maze(r+1, c, MAP, count, 0, H, W)
            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 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))
    if x == w and y == h:
      # 端についたらゴール
      result = True
      break
    if bomb > 0 and table[0][0] == '#':
      # 現在地が黒ならボムで
      table[0][0] = '.' 
      bomb -=1      
    elif bomb > 0 and table[w][h] == '#':
      # ゴールが黒ならボムで
      table[w][h] = '.' 
      bomb -=1  
    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
      table[x+1][y]='.'
      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
      table[x][y+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
      table[x][y+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
      table[x][y+1]='.'
      y+=2
    else:
      break
  if result:
    return result
  # y先行パターン
  while True:
    print("x:{} y:{}".format(x,y))
    if x == w and y == h:
      result = True
      break
    if bomb > 0 and table[0][0] == '#':
      # 現在地が黒ならボムで
      table[0][0] = '.' 
      bomb -=1      
    elif bomb > 0 and table[w][h] == '#':
      # ゴールが黒ならボムで
      table[w][h] = '.' 
      bomb -=1
    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
      table[x+1][y]='.'
      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
      table[x][y+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
      table[x][y+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
      table[x][y+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):
  tmp = copy.deepcopy(table)
  # print("bomb:{}".format(c))
  if checkpath(h-1,w-1,tmp,c):
    # print('Success!!')
    print(c)
    break

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[1],g[1]=0,0

for j in range(h*w-h):
    if j%w==0 or j%w==w-1: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
def MI(): return map(int, sys.stdin.readline().split())

H,W = MI()
s = [list(input()) for _ in range(H)]
dp = [[0 for _ in range(W)] for _ in range(H)]

if s[0][0]=='#':
	dp[0][0] = 1

for i in range(W-1):
	if s[0][i+1]=='#' and s[0][i]=='.':
		dp[0][i+1] = dp[0][i] + 1
	else:
		dp[0][i+1] = dp[0][i]

for i in range(H-1):
	if s[i+1][0]=='#' and s[i][0]=='.':
		dp[i+1][0] = dp[i][0] + 1
	else:
		dp[i+1][0] = dp[i][0]

def next(start, goal, start_value):
	if start=='.' and goal=='#':
		start_value += 1
	return start_value

for i in range(1,W):
	for j in range(1,H):
		# print(next(s[i][j-1],s[i][j],dp[i][j-1]),next(s[i-1][j],s[i][j],dp[i-1][j]))
		dp[i][j] = min(next(s[i][j-1],s[i][j],dp[i][j-1]), next(s[i-1][j],s[i][j],dp[i-1][j]))

Submission status: Runtime Error

Solution code:
"""
ID: takesh.1
LANG: PYTHON2
TASK: task1
"""


gr = []

maxw = 100
bw = maxw+1
MAXX = maxw*maxw+1

w, h = map(int, raw_input.readline().split(" "))
wei = []
for i in range(h):
    wwei = [bw]*w
    gr.append(raw_input.readline().rstrip("\r\n"))
    for j, c in enumerate(gr[i]):
        if c == ".":
            wwei[j] = 1
        #else:
        #    wwei[j] = 1
    wei.append(wwei)

def calcW(ww, hh):
    if (ww == 0) and (hh == 0):
        return wei[ww][hh]

    if (ww < 0) or (hh < 0):
        return MAXX

    wLeft = calcW(ww-1, hh)
    wUp = calcW(ww, hh-1)
    return wei[ww][hh] + min(wLeft, wUp)

weiz = calcW(w-1, h-1)
out = int(weiz/100)

print(str(out)+"\n")

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.extend(input())

T = [-1] * H * W
if S[0] == '#':
    T[0] = 1
else:
    T[0] = 0
S[0] = '-'

k = 0
v = T[k]
R = {k:v}

def get_search_address(add):
    p = []
    x = add % W
    y = add // W
    next_x = x+1
    next_y = y+1
    if next_x <= (H-1):
        p.append(W*y+next_x)
    if next_y <= (W-1):
        p.append(W*next_y+x)
    return p

while R:
    k = min(R, key=R.get)
    # print('k:{}'.format(k))
    p = get_search_address(k)
    # print('p:{}'.format(p))
    for n in p:
        # print('n:{}'.format(n))
        if T[n] == -1:
            if S[n] == '-':
                if T[k]+1 < T[n]:
                    T[n] = T[k]+1
            elif S[n] == '#':
                T[n] = T[k]+1
                S[n] = '-'
                R[n] = T[n]
            else:
                T[n] = T[k]
                S[n] = '-'
                R[n] = T[n]
    del R[k]
    # print('R:{}'.format(R))
    # print('T:{}'.format(T))

print(T[H*W-1])

Submission status: Runtime Error

Solution code:
import math
h,w=map(int,input().split())
s=[]
dp=[]
for i in range(h):
    s.append(input())
    dp.append([1000]*w)
dp[0][0]=0 if s[0][0]=='.' else 1
for i in range(h+w-1):
    for j in range(min(i+1,h+w-i-1)):
        a=min(i+1,h+w-i-1)
        b=max(0,math.ceil((i-(h+w-i-1))/2))
        if j+b+1<h:
            dp[j+b+1][a+b-j-1]=min(dp[j+b+1][a+b-j-1],dp[j+b][a+b-j-1]+(s[j+b+1][a+b-j-1]=='#'))
        if a-j+b<w:
            dp[j+b][a+b-j]=min(dp[j+b][a+b-j],dp[j+b][a+b-j-1]+(s[j+b][a+b-j]=='#'))
print(dp[-1][-1])

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())

import numpy as np
dp = np.zeros((h,w),np.int32)
s = np.full((h,w),'.')
for H in range(h):
    S = input()
    for W in range(w):
        s[H][W] = S[W]
        own = s[H][W]
        if H==0 and W==0: dp[H][W] = 0 if own=='.' else 1
        elif H==0: dp[H][W] = dp[H][W-1]+1 if s[H][W-1]=='.' and own=='#' else dp[H][W-1]
        elif W==0: dp[H][W] = dp[H-1][W]+1 if s[H-1][W]=='.' and own=='#' else dp[H-1][W]
        else:
            dp[H][W] = min(dp[H][W-1], dp[H-1][W])
            if own=='#':
                if s[H][W-1]=='.' and s[H-1][W]=='.': dp[H][W] += 1
                elif s[H][W-1]=='#' and s[H-1][W]=='.': dp[H][W] = min(dp[H][W-1], dp[H-1][W]+1)
                elif s[H][W-1]=='.' and s[H-1][W]=='#': dp[H][W] = min(dp[H][W-1]+1, dp[H-1][W])
print(dp[h-1,w-1])

Submission status: Runtime Error

Solution code:
def flip_sq(sy, sx):
    gy = sy
    gx = sx
    while True:
        if sx == W - 1:
            if tiles[sy+1][sx] == "#":
                sy += 1
            else:
                break
        elif tiles[sy][sx+1] == "#":
            sx += 1
            if sy == H - 1:
                break
            if tiles[sy+1][sx] == "#":
                sy += 1
        else:
            break
    for y in range(gy, sy+1):
        for x in range(gx, sx+1):
            tiles[y][x] = flip[tiles[y][x]]

    print(tiles)

H, W = [int(i) for i in input().strip().split(" ")]
tiles = []
for i in range(H):
    tiles.append([i for i in input().strip()])

tile = {"#":1, ".":0}
flip = {"#":".", ".":"#"}
count = []
for i in range(H):
    count.append([])
    for j in range(W):
        count[i].append(0)

count[0][0] = tile[tiles[0][0]]
if tile[tiles[0][0]]:
    flip_sq(0, 0)

for y in range(H):
    for x in range(W):
        if y == 0:
            if x == 0:
                continue
            count[y][x] = count[y][x-1] + tile[tiles[y][x]]
        else:
            if x == 0:
                count[y][x] = count[y-1][x] + tile[tiles[y][x]]
            else:
                count[y][x] = min(count[y-1][x], count[y][x-1]) + tile[tiles[y][x]]
        if tile[tiles[y][x]]:
            flip_sq(y, x)

print(count[H-1][W-1])