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:
#!/usr/bin/env python3
import sys

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 (((y==H-1)&(x==W-1))&(cmp < min_r)):
            if (pre ==".")&(s[y][x]=="#") :
                min_r = cmp+1
            else :
                min_r = cmp
        elif 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])

    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:
# -*- coding: utf-8 -*-
# 整数の入力

a = input()
# スペース区切りの整数の入力
H,W=a.split(" ")
H=int(H)
W=int(W)
bord=[["#" for i in range(W+2)] for j in range(H+2)]
for i in range(H):
    tempbord=input()
    for j in range(W):
        bord[i+1][j+1]=tempbord[j]
DPbord=[[H*W for i in range(W+2)] for j in range(H+2)]
if bord[1][1]==".":
    DPbord[1][1]=0
if bord[1][1]=="#":
    DPbord[1][1]=1


for i in range(3,H+W+3):
    for j in range(i-1):
        if j+1>H or i-j-1>W:
            continue
        if bord[j+1][i-j-1]=="#":
            DPbord[j+1][i-j-1]=min([DPbord[j][i-j-1],DPbord[j+1][i-j-2]])+1
        if bord[j+1][i-j-1]==".":
            DPbord[j+1][i-j-1]=min([DPbord[j][i-j-1],DPbord[j+1][i-j-2]])
print(DPbord[W][H])

Submission status: Runtime Error

Solution code:
from collections import deque
import math
import sys

h,w = map(int,input().split())
s = [0] * h
for i in range(h):
     s[i] = [i for i in str(input())]
memo=[[math.inf] * w for _ in range(h)]
box=[deque([]) for _ in range(w+h-1)]
box[0].append([0,0])
ans = 0
for i in range(w+h-1):
     while box[i]:
          y,x = box[i].popleft()
          if y == h-1 and x == w-1:
               if s[y][x] == '.' and s[0][0] == '.':
                    print(ans//2)
               elif s[y][x] == '.' and s[0][0] == '#' :
                    print((ans+1)//2)
               elif s[y][x] == '#' and s[0][0] == '.':
                    print((ans + 1) // 2)
               else:
                    print(ans//2 + 1)
               sys.exit()
          for k, j in [(1, 0), (0, 1)]:

               nexty, nextx = y + k, x + j
               if nexty < h and nextx < w:
                    if s[nexty][nextx] == s[y][x]:
                         box[i].append([nexty, nextx])

                    elif s[nexty][nextx] != s[y][x]:
                         box[i+1].append([nexty, nextx])


     ans += 1

Submission status: Runtime Error

Solution code:
import numpy as np
H, W = map(int, input().split())

array = np.zeros((H, W))
for i in range(H):
  l = list(map(int, input().split()))
  array[i, :] = l

result_all = 0
if array[0,0] == '#':
  result_all += 1

from itertools import combinations
wl = ["w"]*len(W-1)
hl = ["h"]*len(H-1)
l = wl+hl
base = combinations(l)
base = list(set(base))

rl =[]
for i in base:
  w = 0
  l = 0
  result = result_all
  for j in list(i):
    if j == "w":
      w += 1
    else:
      h += 1
    if array[h, w] == "#":
      result += 1
  rl.append(result)
  
print(rl.min())

Submission status: Runtime Error

Solution code:
def SearchPoint(sw, sh, coun):  #現在地から空白地帯までの距離を出す
    #print(str(sw)+ " " + str(sh))
    dist1 = 1000
    dist2 = 1000
    rx1 = 0
    rx2 = 0
    ry1 = 0
    ry2 = 0
    if sw < w-1:
        rx1, ry1, dist1 = serE(sw, sh, coun)
    if sh < h-1:
        rx2, ry2, dist2 = serS(sw, sh, coun)
    #print("func" + str(sw)+ " " + str(sh)+ " " + str(coun) + " " + str(rx1)+ " " + str(ry1)+ " " + str(dist1)+ " " + str(rx2)+ " " + str(ry2)+ " " + str(dist2))  
    if dist1 <= dist2:
        return rx1, ry1, dist1
    else:
        return rx2, ry2, dist2
    

def serE(ssw, ssh, cou):
    if a[ssw + 1][ssh] == '.':
        return ssw + 1, ssh, cou + 1
    else:
        return SearchPoint(ssw+1,ssh,cou+1)


def serS(ssw, ssh, cou):
    if a[ssw][ssh + 1] == '.':
        return ssw, ssh + 1, cou + 1
    else:
        return SearchPoint(ssw, ssh + 1, cou + 1)
        
w, h = map(int, input().split())
 
a = [input() for i in range(h)]
    
count = 0

if a[0][0] == '#':
    count += 1
    a[0] = "." + str(a[1:])

if a[w-1][h-1] == '#':
    count += 1
    a[h - 1] = str(a[:1]) + "."

nx = 0
ny = 0
countin = 0

while 1:
    if nx == w - 1 and ny == h - 1:
        break
    nx, ny, countin = SearchPoint(nx, ny, 0)
    if nx == ny == 0:
        break
    #print(str(nx)+ " " + str(ny)+ " " + str(countin))
    count += countin-1

print(count)

Submission status: Runtime Error

Solution code:
import itertools
 
H,W=map(int, input().split())
garden=[]
 
for i in range(H):
    garden.append(list(input().replace(".","1").replace("#","0")))
 
#print(garden)
dct=[(1,0),(0,1),(-1,0),(0,-1)]
#garden=[[1,0,1,0,1],[0,1,0,1,0],[1,0,1,0,1],[0,1,0,1,0],[1,0,1,0,1]]
#H,W=5,5
start=(0,0)
goal=(H-1,W-1)
path=[start]
d=[ [i+j for i in range(W)] for j in range(H)]
#print(d)
done=[(0,0)]
for i, j in itertools.product(range(H),range(W)):
    color=garden[i][j]
    for each in dct:
        di,dj=each[0],each[1]
        if i+di<H and j+dj< W and i+di>=0 and j+dj>=0 and  garden[i+di][j+dj]==color:
            d[i+di][j+dj]=min(d[i+di][j+dj],d[i][j])
            done.append((i+di,j+dj))
            #print((i,j),(i+di,j+dj))
            #print("same")
        elif i+di<H and j+dj< W and i+di>=0 and j+dj>=0 :
            d[i+di][j+dj]=min(d[i+di][j+dj],d[i][j]+1)
            done.append((i+di,j+dj))
            #print((i,j),(i+di,j+dj))
            #print("not same")
    #print(d)
if int(garden[0][0])+int(garden[H-1][W-1])==0:
  print(int((d[H-1][W-1]+1)/2)+1)
else:
  print(int((d[H-1][W-1]+1)/2))import itertools
 
H,W=map(int, input().split())
garden=[]
 
for i in range(H):
    garden.append(list(input().replace(".","1").replace("#","0")))
 
#print(garden)
dct=[(1,0),(0,1)]
#garden=[[1,0,1,0,1],[0,1,0,1,0],[1,0,1,0,1],[0,1,0,1,0],[1,0,1,0,1]]
#H,W=5,5
start=(0,0)
goal=(H-1,W-1)
path=[start]
d=[ [i+j for i in range(W)] for j in range(H)]
#print(d)
done=[(0,0)]
for i, j in itertools.product(range(H),range(W)):
    color=garden[i][j]
    for each in dct:
        di,dj=each[0],each[1]
        if i+di<H and j+dj< W and i+di>=0 and j+dj>=0 and  garden[i+di][j+dj]==color:
            d[i+di][j+dj]=min(d[i+di][j+dj],d[i][j])
            done.append((i+di,j+dj))
            #print((i,j),(i+di,j+dj))
            #print("same")
        elif i+di<H and j+dj< W and i+di>=0 and j+dj>=0 :
            d[i+di][j+dj]=min(d[i+di][j+dj],d[i][j]+1)
            done.append((i+di,j+dj))
            #print((i,j),(i+di,j+dj))
            #print("not same")
    #print(d)
if int(garden[0][0])+int(garden[H-1][W-1])==0:
  print(int((d[H-1][W-1]+1)/2)+1)
else:
  print(int((d[H-1][W-1]+1)/2))

Submission status: Runtime Error

Solution code:
import copy
N = int(input())
num_list = list(map(int,input()))
num_list2 = []
for i in range(1,N):
    if(len(num_list2)==0 or len(num_list2)==1):
        break
    if(i != 1):
        num_list = copy.copy(num_list2)
        num_list2 = []
    for j in range(len(num_list)-1):
        num = num_list[j] - num_list[j+1]
        if num == 0:
            continue
            
        num_list2.append(abs(num))
        
if(len(num_list2)==0):
    print(0)
else:
    print(num_list2[0])

Submission status: Runtime Error

Solution code:
zimport numpy as np
h, w = map(int, input().split())
s = [input() for _ in range(h)]

dp = np.zeros(h * w).reshape(h, w)

if s[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0
    
for i in range(h - 1):
    if s[i + 1][0] == "." or s[i][0] == s[i + 1][0]:
        dp[i + 1][0] = dp[i][0]
    else:
        dp[i + 1][0] = dp[i][0] + 1
for j in range(w - 1):
    if s[0][j + 1] == "." or s[0][j] == s[0][j + 1]:
        dp[0][j + 1] = dp[0][j]
    else:
        dp[0][j + 1] = dp[0][j] + 1

for i in range(h - 1):
    for j in range(w - 1):
        if s[i + 1][j + 1] == "." or (s[i][j + 1] == s[i + 1][j + 1] and s[i + 1][j + 1] == s[i + 1][j]):
            dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j])
        elif s[i][j + 1] == s[i + 1][j + 1] and s[i + 1][j + 1] != s[i + 1][j]:
            dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j] + 1)
        elif s[i][j + 1] != s[i + 1][j + 1] and s[i + 1][j + 1] == s[i + 1][j]:
            dp[i + 1][j + 1] = min(dp[i][j + 1] + 1, dp[i + 1][j])
        else:
            dp[i + 1][j + 1] = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1)
    
if s[h - 1][w - 1] == "#" and (s[0][0] == "#" and dp[h - 2][w - 1] != 1 and dp[h - 1][w - 2] != 1):
    dp[h- 1][w - 1] += 1

print(int(dp[h - 1][w - 1]))

Submission status: Runtime Error

Solution code:
N = int(input())
a = input()
b = [0] * (N-1)
for i in range(N-1):
    b = [0] * (N-1-i)
    for j in range(N-1-i):
        b[j] = abs(int(a[j]) - int(a[j+1]))
    a = b
print(a[0])

Submission status: Runtime Error

Solution code:
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans
'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)
'''


#a = list(map(int, input().split()))

h,w = list(map(int, input().split()))

s = []
for i in range(h):
    ss=[]
    a=input()
    for j in range(w):
        if(a[j]=='.'):
            ss.append(0)
        else:
            ss.append(1)
    s.append(ss)
        
dp = [[999 for j in range(w)]for i in range(h)]
dp[0][0] = s[0][0]

for i in range(h+w-2):
    for j in range(i+2):
        x = j
        y = i+1-j
        
        if(0<= x and x<=w-1 and 0<=y and y<=h-1):
            #print(x,y)
            if(s[x][y]==0):
                dp[x][y] = min(dp[max(0,x-1)][y], dp[x][max(0,y-1)])
            else:
                if(x!=0 and s[x-1][y]==1):
                    dp[x][y] = dp[x-1][y]
                elif(x!=0 and s[x-1][y]==0):
                    dp[x][y] = dp[x-1][y]+1
                if(y!=0 and s[x][y-1]==1):
                    dp[x][y] = min(dp[x][y],dp[x][y-1])
                elif(y!=0 and s[x][y-1]==0):
                    dp[x][y] = min(dp[x][y],dp[x][y-1]+1)
                   




print(dp[-1][-1])

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))
    if x == w and y == h:
      # 端についたらゴール
      result = True
      break
    if bomb > 0 and ((x==w and y==h-1) or (x==w-1 and y==h)) and table[w][h] == '#':
      # print(' ゴールが黒ならボムで')
      bomb -=1
      result = True
      break
    if x==0 and y==0 and table[x][y] == '#':
      # print(' 現在地が黒ならボムで')
      if bomb > 0:
        bomb -=1
      else:
        result = False
        break
    if 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 ((x==w and y==h-1) or (x==w-1 and y==h)) and table[w][h] == '#':
      # print(' ゴールが黒ならボムで')
      bomb -=1
      result = True
      break
    if x==0 and y==0 and table[x][y] == '#':
      # print(' 現在地が黒ならボムで')
      if bomb > 0:
        bomb -=1
      else:
        result = False
        break
    if 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:
N=input()
N = int(N)
a = input()
b = list(a)
c =list(map(int,b))
d= []
for s in range(N-1):
  r = abs(c[s] -c[s+1])
  d.append(r)
for i in range(N-2):
  for p in range(N-i):
    l = abs(d[-p] - d[-p+1])
    d.append(l)
print(d)

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))
    if x == w and y == h:
      # 端についたらゴール
      result = True
      break
    if bomb > 0 and ((x==w and y==h-1) or (x==w-1 and y==h)) and table[w][h] == '#':
      print(' ゴールが黒ならボムで')
      bomb -=1
      result = True
      break
    if x==0 and y==0 and table[x][y] == '#':
      print(' 現在地が黒ならボムで')
      if bomb > 0:
        bomb -=1
      else:
        result = False
        break
    if 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 ((x==w and y==h-1) or (x==w-1 and y==h)) and table[w][h] == '#':
      print(' ゴールが黒ならボムで')
      bomb -=1
      result = True
      break
    if x==0 and y==0 and table[x][y] == '#':
      print(' 現在地が黒ならボムで')
      if bomb > 0:
        bomb -=1
      else:
        result = False
        break
    if 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:
N = int(input())
a = input()
l = [int(a[i]) for i in range(N)]
k = []

for j in range(1,N):
    for i in range(N-j):
        min = abs(l[i] - l[i+1])
        k.append(min)
    l = k
    k = []
        
ans = l[0]
print(ans)

Submission status: Runtime Error

Solution code:
# -*- utf-8 -*-

tar_x, tar_y = map(int, input().split())

stage = []
tmp = list(input().split())

while(not 0 == len(tmp)):
    stage.append(tmp)
    tmp = list(input())


ans = 1000000000

tar_x, tar_y = (tar_x - 1, tar_y - 1)
now_x, now_y = (0, 0)

# serch
cnt = 0
while(now_x != tar_x or now_y != tar_y):
    if("#" == stage[now_x][now_y]):
        cnt += 1

    # move next
    if(tar_x == now_x):
        next_x = now_x
    else:
        next_x = now_x + 1

    if(tar_y == now_y):
        next_y = now_y
    else:
        next_y = now_y + 1

    # setch x
    if("#" != stage[next_x][now_y] and tar_x != now_x):
        now_x = next_x

    # serch y
    elif("#" != stage[now_x][next_y] and tar_y != now_y):
        now_y= next_y

    # bose "#"
    else:
        if(tar_x != now_x):
            now_x = next_x
        elif(tar_x != now_x):
            now_y = next_y
        else:
            tmp_cnt_x = 0
            tmp_cnt_y = 0

            # x
            next_next_x = next_x + 1 if tar_x >= (next_x + 1) else None
            next_next_y = now_y + 1 if tar_y >= (now_y + 1) else None

            if(not None is next_next_x):
                if("." == stage[next_next_x][now_y]):
                    tmp_cnt_x += 1
            if(not None is next_next_y):
                if("." == stage[next_x][next_next_y]):
                    tmp_cnt_x += 1
            if(not None is next_next_x and not None is next_next_y):
                if("." == stage[next_next_x][next_next_y]):
                    tmp_cnt_x += 1

            # y
            next_next_x = now_x + 1 if tar_x >= (now_x + 1) else None
            next_next_y = next_y + 1 if tar_y >= (next_y + 1) else None

            if(not None is next_next_x):
                if("." == stage[next_next_x][next_y]):
                    tmp_cnt_y += 1
            if(not None is next_next_y):
                if("." == stage[now_x][next_next_y]):
                    tmp_cnt_y += 1
            if(not None is next_next_x and not None is next_next_y):
                if("." == stage[next_next_x][next_next_y]):
                    tmp_cnt_y += 1

            if(tmp_cnt_x >= tmp_cnt_y):
                now_x = next_x
            else:
                now_y = next_y

if("#" == stage[now_x][now_y]):
    cnt += 1

print(cnt)

Submission status: Runtime Error

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

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 (((y==H-1)&(x==W-1))&(cmp < min_r)):
            if (pre ==".")&(s[y][x]=="#") :
                min_r = cmp+1
            else :
                min_r = cmp
        elif 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])


            # 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:
from collections import deque
h,w = map(int,input().split())
m = []
count = 0
cl =[]
for i in range(h):
  m.append(list(input()))

def bfs_w(cx,cy,count,cl,gx,gy,flg):
  dx = [0,1]
  dy = [1,0]
  d = deque()
  d.appendleft([cx,cy])
  
  if flg == 1:
    count += 1
    key = '.'
    flg =0
  else :
    key = '#'
    flg = 1
    
  while d:
    n = d.pop()
    x = n[0]
    y = n[1]

    

    for i in range(2):
      nx = x + dx[i]
      ny = y + dy[i] 

      if(nx == gx and ny == gy and m[ny][nx] != key):
        cl.append(count) 

        break
      elif nx < w and ny < h and m[ny][nx] != key:
        d.appendleft([nx,ny])

      elif nx < w and ny < h and m[ny][nx] == key:

        bfs_w(x,y,count,cl,gx,gy,flg)  

if m[0][0] == '.':
  bfs_w(0,0,count,cl,w-1,h-1,0)
else:
  bfs_w(0,0,count,cl,w-1,h-1,1)
  
print(min(cl))

Submission status: Runtime Error

Solution code:
from collections import deque
h,w = map(int,input().split())
m = []
count = 0
cl =[]
for i in range(h):
  m.append(list(input()))
  
print(m)

def bfs_w(cx,cy,count,cl,gx,gy,flg):
  dx = [0,1]
  dy = [1,0]
  d = deque()
  d.appendleft([cx,cy])
  
  if flg == 1:
    count += 1
    key = '.'
    flg =0
  else :
    key = '#'
    flg = 1
    
  while d:
    n = d.pop()
    x = n[0]
    y = n[1]

    

    for i in range(2):
      nx = x + dx[i]
      ny = y + dy[i] 

      if(nx == gx and ny == gy and m[ny][nx] != key):
        cl.append(count) 

        break
      elif nx < w and ny < h and m[ny][nx] != key:
        d.appendleft([nx,ny])

      elif nx < w and ny < h and m[ny][nx] == key:

        bfs_w(x,y,count,cl,gx,gy,flg)  

if m[0][0] == '.':
  bfs_w(0,0,count,cl,w-1,h-1,0)
else:
  bfs_w(0,0,count,cl,w-1,h-1,1)
  
print(min(cl))

Submission status: Runtime Error

Solution code:
#coding:utf-8

from collections import deque

def main():
    h, w = map(int, input().split())
    mtrx = []
    for _ in range(h):
        mtrx.append(list(input()))
    
    dire = [[0, 1], [1, 0]]
    cnt = 0
    q = deque([])
    # cnt, flg, i, j
    if mtrx[0][0] == '#':
        cnt += 1
    q.append([cnt, mtrx[0][0], 0, 0])

    minv = 1000000

    dummy = [[minv for _ in range(w)] for _ in range(h)]
    while len(q) > 0:
        now = q.popleft()
        i = now[2]
        j = now[3]

        for d in dire:
            i_n = i + d[0]
            j_n = j + d[1]
            cnt = now[0]
            if i_n >= w or j_n >= h:
                continue
            if mtrx[i_n][j_n] == '#':
                cnt += 1
            if dummy[i_n][j_n] <= cnt:
                continue
            dummy[i_n][j_n] = cnt
            next_obj = [cnt, mtrx[i_n][j_n], i_n, j_n]
            q.append(next_obj)
            
        if i == h - 1 and j == w - 1:
            minv = min(minv, now[0])
    print(minv)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#coding:utf-8

from collections import deque

def main():
    h, w = map(int, input().split())
    mtrx = []
    for _ in range(h):
        mtrx.append(list(input()))
    
    dire = [[0, 1], [1, 0]]
    cnt = 0
    q = deque([])
    # cnt, flg, i, j
    if mtrx[0][0] == '#':
        cnt += 1
    q.append([cnt, mtrx[0][0], 0, 0])

    minv = 100000000000

    dummy = [[minv for _ in range(w)] for _ in range(h)]
    while len(q) > 0:
        now = q.popleft()
        i = now[2]
        j = now[3]

        for d in dire:
            i_n = i + d[0]
            j_n = j + d[1]
            cnt = now[0]
            if i_n >= w or j_n >= h:
                continue
            if mtrx[i_n][j_n] == '#':
                cnt += 1
            if dummy[i_n][j_n] <= cnt:
                continue
            dummy[i_n][j_n] = cnt
            next_obj = [cnt, mtrx[i_n][j_n], i_n, j_n]
            q.append(next_obj)
            
        if i == h - 1 and j == w - 1:
            minv = min(minv, now[0])
    print(minv)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
x_1 = list(input())

def a(x):
    result = list()
    if len(x) <= 1:
        return x
    
    for i in range(len(x)-1):
        result.append(abs(int(x[i])-int(x[i+1])))
    kaeri = a(result)
    return kaeri

b = a(x_1)
c = b[0]
print(c)

Submission status: Runtime Error

Solution code:
#coding:utf-8

from collections import deque

def main():
    h, w = map(int, input().split())
    mtrx = []
    for _ in range(h):
        mtrx.append(list(input()))
    
    dire = [[0, 1], [1, 0]]
    cnt = 0
    q = deque([])
    # cnt, flg, i, j
    if mtrx[0][0] == '#':
        cnt += 1
    q.append([cnt, mtrx[0][0], 0, 0])

    minv = 100000000000

    dummy = [[minv for _ in range(w)] for _ in range(h)]
    while len(q) > 0:
        now = q.popleft()
        i = now[2]
        j = now[3]

        for d in dire:
            i_n = i + d[0]
            j_n = j + d[1]
            cnt = now[0]
            if i_n >= w or j_n >= h:
                continue
            if mtrx[i_n][j_n] == '#':
                cnt += 1
            if dummy[i_n][j_n] <= cnt:
                continue
            dummy[i_n][j_n] = cnt
            print(now)
            print(mtrx[i_n][j_n])
            next_obj = [cnt, mtrx[i_n][j_n], i_n, j_n]
            q.append(next_obj)
            
        if i == h - 1 and j == w - 1:
            minv = min(minv, now[0])
    print(minv)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy
h, w = map(int, input().split())
s = [input() for _ in range(h)]

dp = np.zeros(h * w).reshape(h, w)

if s[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0
    
for i in range(h - 1):
    if s[i + 1][0] == "." or s[i][0] == s[i + 1][0]:
        dp[i + 1][0] = dp[i][0]
    else:
        dp[i + 1][0] = dp[i][0] + 1
for j in range(w - 1):
    if s[0][j + 1] == "." or s[0][j] == s[0][j + 1]:
        dp[0][j + 1] = dp[0][j]
    else:
        dp[0][j + 1] = dp[0][j] + 1

for i in range(h - 1):
    for j in range(w - 1):
        if s[i + 1][j + 1] == "." or (s[i][j + 1] == s[i + 1][j + 1] and s[i + 1][j + 1] == s[i + 1][j]):
            dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j])
        elif s[i][j + 1] == s[i + 1][j + 1] and s[i + 1][j + 1] != s[i + 1][j]:
            dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j] + 1)
        elif s[i][j + 1] != s[i + 1][j + 1] and s[i + 1][j + 1] == s[i + 1][j]:
            dp[i + 1][j + 1] = min(dp[i][j + 1] + 1, dp[i + 1][j])
        else:
            dp[i + 1][j + 1] = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1)
    
if s[h - 1][w - 1] == "#" and (dp[h - 2][w - 1] != 1 and dp[h - 2][w - 1] != 1):
    dp[h- 1][w - 1] += 1

print(int(dp[h - 1][w - 1]))

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(h):
  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])