Problem:


Score : 400 points

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
Each square is painted black or white.

The grid is said to be good if and only if the following condition is satisfied:

From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.

Note that (1, 1) and (H, W) must be white if the grid is good.

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

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

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

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

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

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

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

Sample Output 3

0

No operation may be needed.

Sample Input 4

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

Sample Output 4

4



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


Sample output:
1

Submission status: Runtime Error

Solution code:
from copy import deepcopy
import numpy as np
import sys
sys.setrecursionlimit(10**9)

# def f(i, j):
#     for dx, dy in ((1, 0), (0, 1)):
#         nx = i + dx
#         ny = j + dy
#         if nx >= H:
#             continue
#         if ny >= W:
#             continue
#         if S[nx, ny] == '.' and G[nx, ny] == 0:
#             G[nx, ny] = 1
#             f(nx, ny)


def g(S):
    G = np.zeros(S.shape, dtype='int')
    G[0, 0] = 1
    for x in range(H):
        for y in range(W):
            if S[x, y] != '.':
                continue
            for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
                nx = x + dx
                ny = y + dy
                if nx < 0 or nx >= H:
                    continue
                if ny < 0 or ny >= W:
                    continue
                if G[nx, ny] == 1:
                    G[x, y] = 1
                    break
    return G
    # for x in range(H):
    #     for y in range(W):
    #         if S[x, y] != '.':
    #             continue
    #         for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
    #             nx = x + dx
    #             ny = y + dy
    #             if nx < 0 or nx >= H:
    #                 continue
    #             if ny < 0 or ny >= W:
    #                 continue
    #             if G[nx, ny] == 1:
    #                 G[x, y] = 1
    #                 break


H, W = map(int, input().split())
S = np.array([list(input()) for _ in range(H)])
# print('S', S)

G = g(S)
# f(0, 0)
# print('G')
# print(G)

if G[H - 1, W - 1] == 1:
    print(0)
    exit()


def f4(i, j, S):
    G = g(S)
    if G[H - 1, W - 1] == 1:
        return 0
    l = []
    for dx, dy in ((1, 0), (0, 1)):
        nx = i + dx
        ny = j + dy
        if nx >= W or ny >= H:
            continue
        if G[nx, ny] == 1:
            l.append(f4(nx, ny, S))
        else:
            if dx == 1:  # 縦移動しつつ＃を.に変換
                cnt = 0
                for cnt in range(H):
                    if nx + cnt >= H:
                        break
                    if S[nx + cnt, ny] == '.':
                        break
                S = deepcopy(S)
                S[nx:nx + cnt, ny] = '.'
                l.append(f4(nx, ny, S) + 1)  # 再帰
            if dy == 1:  # 横移動しつつ#を.に変換
                cnt = 0
                for cnt in range(W):
                    if ny + cnt >= W:
                        cnt = cnt
                        break
                    if S[nx, ny + cnt] == '.':
                        break
                S = deepcopy(S)
                S[nx, ny:ny + cnt] = '.'
                l.append(f4(nx, ny, S) + 1)  # 再帰
    return min(l)


tmp = 0
if S[0, 0] == '#':
    tmp += 1
    cnt = 0
    nx, ny = 0, 0
    for cnt in range(H):
        if nx + cnt >= H:
            break
        if S[nx + cnt, ny] == '.':
            break
    new_S = deepcopy(S)
    new_S[nx:nx + cnt, ny] = '.'
    ans1 = f4(0, 0, new_S)

    for cnt in range(W):
        if ny + cnt >= W:
            cnt = cnt
            break
        if S[nx, ny + cnt] == '.':
            break
    new_S = deepcopy(S)
    new_S[nx, ny:ny + cnt] = '.'
    ans2 = f4(0, 0, new_S)
    ans = min(ans1, ans2)
else:
    ans = f4(0, 0, S)
print(ans + tmp)

Submission status: Runtime Error

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

import sys
import numpy
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限
h,w = map(int,input().split())
c = [list(input()) for i in range(h)]
a=[]
# print(c)

count=0
newcount=0
result=1000000

def dfs(a,result,count,x,y):
    if c[x][y] == "#":
        # print('flag')

        count=count+1
        # print(count)
    if x==h-1 and y==w-1:
        # print('flag')
        if count < result:
            result= count
            a.append(result)
        if c[x][y] == "#":
            count=count-1
        return result,count,x,y
    
    if x<w-1:
        dfs(a,result,count,x+1,y)
        # print('flag')

    if y<h-1:
        dfs(a,result,count,x,y+1)
    # print(result)
    
    
    
# if c[0][0]=="#":
#     count=1
dfs(a,result,count,0,0)
print(min(a))

Submission status: Runtime Error

Solution code:
memory = [[-1 for i in range(W)] for j in range(H)]
def flip_count_mem(r, c):
  if memory[r][c] < 0:
    memory[r][c] = flip_coumt(r, c)
  return memory[r][c]

def flip_count (r, c):
  if r == H - 1 and c == W - 1:
    if s[r][c] == "#":
      return 1
    else:
      return 0
  #右に行ったとき
  if c == W - 1:
    count_right = 10000
  else:
    count_right = flip_count_mem (r, c + 1)
    if s[r][c] == "#" and s[r][c + 1] == ".":
      count_right += 1
  #下に行ったとき
  if r == H - 1:
    count_down = 10000
  else:
    count_down = flip_count_mem (r + 1, c)
    if s[r][c] == "#" and s[r + 1][c] == ".":
      count_down += 1
  return min(count_right, count_down)

H, W = input().split()
W = int (W)
H = int (H)
s = []
for i in range(H):
  s.append(input())
print (flip_count(0, 0))

Submission status: Runtime Error

Solution code:
n=int(input(''))
m=input('')
s=[]
for a in range(n):
    s.append(int(m[a]))


kuri=n-1
for a in range(n-1):
    kake=1
    for b in range(kuri):
        if s[b]==0 or s[b]==1:
            kake=kake*1
        else:
            kake=kake*0
            break
    if kake==0:
        for b in range(kuri):
            if s[b]==s[b+1]:
                s[b]=0
            elif s[b]*s[b+1]==2:
                s[b]=1
            elif s[b]*s[b+1]==3:
                s[b]=2
            elif s[b]*s[b+1]==6:
                s[b]=1
            else:
                if s[b]==0:
                    s[b]=s[b+1]

    
            kuri=kuri-1
    else:
        ki=0
        gu=0
        for b in range(n):
            if s[b]%2==1:
                ki=ki+1
            else:
                gu=gu+1
        
        if (ki%2)*(gu%2)==1:
            s[0]=1
        else:
            s[0]=0
        break

    if kake==1:
        break

print(s[0])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
H, W = list(map(int, input().split()))
maze = [list(input()) for i in range(H)]

ans = 0

if maze[0][0]=="#":
    ans += 1
    maze[0][0]="."

start = (0, 0)
goal = (H-1, W-1)

visited = [[-1 for j in range(W)] for i in range(H)]
queue = [start]
visited[start[0]][start[1]] = 0
direc = [(1,0), (-1,0), (0,1), (0,-1)]
c_break=0
end=0

while visited[H-1][W-1]<0:
    if queue==[]:
        queue = [(x, y) for y, row in enumerate(visited) for x, elem in enumerate(row) if visited[x][y] == c_break]
        c_break += 1
        end+=1
        if end>10:break
        p = queue.pop(0)
    else:p = queue.pop(0)
    if p == goal:
        break
    for d in direc:
        ny = p[0]+d[0]
        nx = p[1]+d[1]
        if ny < 0 or ny >= H or nx < 0 or nx >= W:
            continue
        if maze[ny][nx]=="#":
            if visited[p[0]][p[1]]<c_break:
                visited[ny][nx] = c_break
            continue
        if visited[ny][nx]!= -1:
            continue
        visited[ny][nx] = c_break
        queue.append((ny, nx))


print(c_break+ans)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
tuple(s)
co=[[None] * h for _ in range(w)]

def f(a,b,s):
  if a==0:
    if b==0:
      if s[a][b]=="#":
        co[a][b]=1
      else:
        co[a][b]=0
    else:
      x=0
      if s[a][b]=="#":
        if s[a][b-1]==".":
          x=1
      co[a][b]=co[a][b-1]+x
  elif b==0:
      x=0
      if s[a][b]=="#":
        if s[a-1][b]==".":
          x=1
      co[a][b]=co[a-1][b]+x
  else:
      x=0
      if s[a][b]=="#":
        if s[a-1][b]==".":
          x=1
      co1=co[a-1][b]+x    
      x=0
      if s[a][b]=="#":
        if s[a][b-1]==".":
          x=1
      co2=co[a][b-1]+x
      
      co[a][b]=min(co1,co2)
    
for i in range(h):
  for j in range(w):
    f(i,j,s)
print(co[h-1][w-1])

Submission status: Runtime Error

Solution code:
import queue

class VInfo(object):
    def __init__(self,color,x,y,d,isChange):
        if not isChange:
            self.color=color
            self.d=d
        else:
            if color==".":self.color="#"
            else:self.color="."
            self.d=d+1
        self.x=x
        self.y=y

h,w=map(int,input().split())
a=[[str(i) for i in input()] for j in range(h)]
sx,sy=0,0
gx,gy=w-1,h-1
cw=0


mapA=[]
m=100000000000000
for my in range(h):
    l=[]
    for mx in range(w):
        if a[my][mx]=="#":l.append(VInfo("#",mx,my,m,False))
        else:l.append(VInfo(".",mx,my,m,False))
    mapA.append(l)
q=queue.Queue()
if a[0][0]==".":q.put(VInfo(a[0][0],sx,sy,0,False))
else:q.put(VInfo(a[0][0],sx,sy,1,True))
while not q.empty():
    now=q.get()
    if now.x==gx and now.y==gy:
        if now.color=="." and a[gy][gx]==".":m=min(m,now.d)
        if now.color=="#" and a[gy][gx]==".":m=min(m,now.d)
        if now.color=="." and a[gy][gx]=="#":m=min(m,now.d)+2
        if now.color=="#" and a[gy][gx]=="#":m=min(m,now.d)+2

    if now.y+1<w and a[now.y+1][now.x]==now.color:
        if mapA[now.y+1][now.x].d>now.d:
            mapA[now.y+1][now.x].d=now.d
            q.put(VInfo(now.color,now.x,now.y+1,now.d,False))
    if now.y+1<w and a[now.y+1][now.x]!=now.color:
        if mapA[now.y+1][now.x].d>now.d:
            mapA[now.y+1][now.x].d=now.d
            q.put(VInfo(now.color,now.x,now.y+1,now.d,True))

    if now.x+1<h and a[now.y][now.x+1]==now.color:
        if mapA[now.y][now.x+1].d>now.d:
            mapA[now.y][now.x+1].d=now.d
            q.put(VInfo(now.color,now.x+1,now.y,now.d,False))
    if now.x+1<h and a[now.y][now.x+1]!=now.color:
        if mapA[now.y][now.x+1].d>now.d:
            mapA[now.y][now.x+1].d=now.d
            q.put(VInfo(now.color,now.x+1,now.y,now.d,True))

m=m//2
print(m)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
map = [list(input())for i in range(h)]
jp= [[0 for _ in range(h+1)] for _ in range(w+1)]

r = h - 1
c = w - 1
while True:
    p = 0
    if r < 0 and c <= 0:
        break
    if r < 0:
        r = h - 1
        c -= 1
    if map[r][c] == "#":
        p = 1
    if c == w-1:
        jp[r][c] = jp[r+1][c] + p
    elif r == h-1:
        jp[r][c] = jp[r][c+1] + p
    elif jp[r+1][c] < jp[r][c+1]:
        jp[r][c] = jp[r+1][c] + p
    elif jp[r+1][c] >= jp[r][c+1]:
        jp[r][c] = jp[r][c+1] + p
    r -= 1

print(jp[0][0])

Submission status: Runtime Error

Solution code:
ans = 0

if a[1][1]==100000:
    a[1][1]=1
    ans += 1

if a[H][W]==100000:
    a[H][W]=1
    ans += 1

dp = []
dp.append([10**10]*(W+2))
for _ in range(H):
    i = [0 for i in range(W)]
    i.append(10**10)
    i.insert(0,10**10)
    dp.append(i)
dp.append([10**10]*(W+2))
dp[1][1]=1


cnt = 1
h_temp = 1
w_temp = 1

for i in range(2,H+W):
    if i <= min(H,W):
        cnt += 1
    elif i > max(H,W):
        cnt -= 1
    else:
        cnt = cnt
    if i <= H:
        h_temp += 1
    if i > H:
        w_temp += 1
    
    for j in range(cnt):
        h = h_temp-j
        w = w_temp+j

        dp[h][w] = min(dp[h-1][w]+a[h][w],dp[h][w-1]+a[h][w])

for _ in range(100):
    cnt = 1
    h_temp = 1
    w_temp = 1

    for i in range(2,H+W):
        if i <= min(H,W):
            cnt += 1
        elif i > max(H,W):
            cnt -= 1
        else:
            cnt = cnt
        if i <= H:
            h_temp += 1
        if i > H:
            w_temp += 1

        for j in range(cnt):
            h = h_temp-j
            w = w_temp+j

            dp[h][w] = min(dp[h-1][w]+a[h][w],dp[h][w-1]+a[h][w],dp[h][w+1]+a[h][w],dp[h+1][w]+a[h][w],dp[h][w])

print(ans+int(dp[H][W]/100000))

Submission status: Runtime Error

Solution code:
                    route.append(route1) 
                    count += 1
                    block = (H - 1) + (W - 1)
                    print(route1)
                    for q in range(0, len(route1)):
                        if q+1 != len(route1):
                            if s[route1[q+1]] == s[route1[q]] and ((route1[q+1] == route1[q] + 1) or (route1[q] % W == (route1[q] + 1) % W)):
                                block = block - 1
                    best = min([best, block])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2147483647)

n = int(input())
a = input()
ans = ""

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input(''))
m=input('')
s=[]
for a in range(n):
    s.append(int(m[a]))


kuri=n-1
for a in range(n-1):
    kake=1
    for b in range(kuri):
        if s[b]==0 or s[b]==1:
            kake=kake*1
        else:
            kake=kake*0
            break
    if kake==0:
        for b in range(kuri):
            if s[b]==s[b+1]:
                s[b]=0
            elif s[b]*s[b+1]==2:
                s[b]=1
            elif s[b]*s[b+1]==3:
                s[b]=2
            elif s[b]*s[b+1]==6:
                s[b]=1
            else:
                if s[b]==0:
                    s[b]=s[b+1]

    
            kuri=kuri-1
    else:
        ki=0
        gu=0
        for b in range(kuri):
            if s[b]%2==1:
                ki=ki+1
            else:
                gu=gu+1
        
        if (ki%2)*(gu%2)==1:
            s[0]=1
        else:
            s[0]=0
        break

    if kake==1:
        break

print(s[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=[]

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



nummin=0
first=True
while flag:


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

        if first:
            nummin=now[2]
            first=False
        if now[2]<nummin:
            nummin=now[2]


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





    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()

print(nummin+count)

Submission status: Runtime Error

Solution code:
import itertools
tateyoko = list(map(int, input().split()))
masu = []
for i in range(tateyoko[0]):
    masu.append(input())

now_index = [0,0]
route = [[0,0]]
bunkiten = []
goal_status = False
retry_number = 0
black_num = 0

kouho = []

for i in range(tateyoko[0] - 1):
    kouho.append(0)
for i in range(tateyoko[1] - 1):
    kouho.append(1)

pattern_num = tateyoko[0] + tateyoko[1] - 4
pattern = list(itertools.product(range(2), repeat=pattern_num))

result = []
black_num_list = []

for a_pattern in pattern:
    for sousa_num in range(pattern_num):
        if a_pattern[sousa_num] == 0:
            now_index = [now_index[0] + 1,now_index[1]]
        if a_pattern[sousa_num] == 1:
            now_index = [now_index[0],now_index[1] + 1]
        if masu[now_index[0]][now_index[1]] == ".":
            pass
        else:
            black_num += 1
    result.append([black_num,a_pattern])
    black_num_list.append(black_num)

black_num_min = min(black_num_list)

if black_num_min == 0:
    print(0)

Submission status: Runtime Error

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

def bfs_w(cx,cy,count,cl,gx,gy,flg,k):
  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]
    if k [y][x] >= 0 and k[y][x] < count :
      break
    else :
      k[y][x] = count

    
    nowhere = 0
    lo = []
    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:
        if k[ny][nx] >=0 and k[ny][nx] <= count:
          break
        else:
          d.appendleft([nx,ny])
          
      
      else:
        nowhere += 1
    if nowhere == 2:
      bfs_w(x,y,count,cl,gx,gy,flg,k)  

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

Submission status: Runtime Error

Solution code:
import numpy as np

BLACK = '#'
WHITE = '.'

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


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

    s[h][w] = visited

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


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


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

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

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

print(ans)

Submission status: Runtime Error

Solution code:
 n            =int(input())                     -

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
an = [int(x) for x in list(input())]

arr = np.array(an)

for i in range(N-1,0,-1):
    arr2 = abs(arr[0:-1] - arr[1:])
    arr = arr2

print(arr[0])

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
lstMap = [list(input())for i in range(h)]
lstJudgeP = [[0 for _ in range(h+1)] for _ in range(w+1)]

r = h - 1
c = w - 1
while True:
    plusP = 0
    if r < 0 and c <= 0:
        break
    if r < 0:
        r = h - 1
        c -= 1

    if lstMap[r][c] == "#":
        plusP = 1
    if c == w-1:
        lstJudgeP[r][c] = lstJudgeP[r+1][c] + plusP
    elif r == h-1:
        lstJudgeP[r][c] = lstJudgeP[r][c+1] + plusP
    elif lstJudgeP[r+1][c] < lstJudgeP[r][c+1]:
        lstJudgeP[r][c] = lstJudgeP[r+1][c] + plusP
    elif lstJudgeP[r+1][c] >= lstJudgeP[r][c+1]:
        lstJudgeP[r][c] = lstJudgeP[r][c+1] + plusP

    r -= 1

print(lstJudgeP)

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    num = [int(x) for x in list(str(int(input())))]
    length= len(num)
    count_stopper = length
    cnt = 1
    big_count = 0
    while big_count != length-1:
        new = []
        while cnt != count_stopper:
            a = 0
            a = num[cnt - 1] - num[cnt]
            new.append(abs(a))
            cnt += 1
        cnt = 1
        num = new
        big_count += 1
        count_stopper -= 1
    print(num[0])
main()

Submission status: Runtime Error

Solution code:
S = input().split()
H = S[0]
W = S[1]
h = int(H)
w = int(W)
s = []
for x in range(0,h):
    s.append(list(S[x +2]))

    
    
def rou(i,j):
    if i > 1 and j > 1:
        if s[i][j] =='.' :
            a = rou(i-1,j)
            b = rou(i,j-1)
            if a > b:
                return b
            else:
                return a
        else:
            c = rou(i-1,j)
            d = rou(i,j-1)
            if c > d:
                return d
            else:
                return c
    elif i == 0 and j > 0:
        if s[i][j] =='.' :
            return rou(1,j-1) 
        else:
            return rou(1,j-1) +1
    elif j == 0 and i > 0:
        if s[i][j] =='.' :
            return rou(i-1,j) 
        else:
            return rou(i-1,j) +1
    elif i == j == 0:
        if s[0][0] =='.':
            return 0
        else:
            return 1

print(rou(h-1,w-1))

Submission status: Runtime Error

Solution code:

maxw = 100
bw = maxw*2
MAXX = (maxw+1)*(maxw+1)
weight = []


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

wwei = [MAXX] * (w+2)
wei.append(wwei)
weight.append([MAXX] * (w+1))

def calcWB(ww, hh):
    for i in range(1, hh+1):
        for j in range(1, ww+1):
            zz = weight[i][j] + wei[i][j+1]
            yy = weight[i][j] + wei[i+1][j]
            if weight[i][j+1] > zz:
                weight[i][j + 1] = zz
            if weight[i+1][j] > yy:
                weight[i+1][j] = yy

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)

weight[1][1] = wei[1][1]
calcWB(w, h)
weiz = weight[w][h]
out = int(weiz/200)

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

Submission status: Runtime Error

Solution code:
# import bisect
# from collections import Counter, deque
# import copy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():
    H,W=map(int,input().split())
    maze=[[_ for _ in input()] for _ in range(H)]

    from collections import deque

    sy, sx = 1,1
    gy, gx = H,W
    sy, sx, gy, gx = sy - 1, sx - 1, gy - 1, gx - 1

    maze = [input() for i in range(H)]
    visited = [[-1] * W for j in range(H)]


    def bfs(maze, visited, sy, sx, gy, gx):
        queue = deque([[sy, sx]])
        visited[sy][sx] = 0
        while queue:
            y, x = queue.popleft()
            if [y, x] == [gy, gx]:
                return visited
            for j, k in ([1, 0], [0, 1]):
                ny, nx = y + j, x + k
                if 0 <= ny < H and 0 <= nx < W and visited[nx][nx] == -1:
                    visited[ny][nx] = visited[y][x] + 1
                    queue.append([ny, nx])

    for i in range(H):
        for j in range(W):
            if visited[i][j]


    print(bfs(maze, visited, sy, sx, gy, gx))



resolve()

Submission status: Runtime Error

Solution code:
ans = 0
H,W = map(int,input().split())
s = [list(input()) for _ in range(W)]

print(ans)

Submission status: Runtime Error

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

print(1)

Submission status: Runtime Error

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

def bfs_w(cx,cy,count,cl,gx,gy,flg,k):
  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]
    k[x][y] = count

    
    nowhere = 0
    lo = []
    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:
        if k[nx][ny] >=0 and k[nx][ny] <= count:
          break
        else:
          d.appendleft([nx,ny])
          
      
      else:
        nowhere += 1
    if nowhere == 2:
      bfs_w(x,y,count,cl,gx,gy,flg,k)  

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

Submission status: Runtime Error

Solution code:
ans = 0
H,W = map(int,input().split())
s = [list(input()) for _ in range(W)]
if s[H-1][W-1] == '#':
  ans += 1
if s[0][0] == '#':
  ans += 1
for i in range(H):
  for j in range(W):
    if s[i][j] == '.':
      s[i][j] = 0
    else:
      s[i][j] = 1
      

print(ans)

Submission status: Runtime Error

Solution code:
ans = 0
H,W = map(int,input().split())
s = [list(input()) for _ in range(W)]
if s[H-1][W-1] == '#':
  ans += 1
if s[0][0] == '#':
  ans += 1
for i in range(H):
  for j in range(W):
    if s[i][j] == '.':
      s[i][j] = 0
    else:
      s[i][j] = 1
      
i = 0
j = 0

while i < H-1 and j < W-1:
  if s[i+1][j] != 1:
    i += 1
  elif s[i][j+1] != 1:
    j += 1
  elif s[i+1][j] == 1 and s[i][j+1] == 1:
    if i < W-2:
      i += 1
    else:
      j += 1
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
import copy
import sys
sys.setrecursionlimit(10000)

H, W = [int(n) for n in input().split()]

field = [[0] * W] * H
for h in range(H):
    field[h]  = [1 if c == '#' else 0 for c in input()]

def action(field,r0,c0,r1,c1):
    result = copy.deepcopy(field)
    for r in range(H):
        for c in range(W):
            if r0 <= r <= r1:
                if c0 <= c <= c1:
                    result[r][c] = 1 - field[r][c]
    return result

def debug(field,r0,c0,r1,c1,c=0):
    print('-'*W,c)
    for r in range(H):
        line = ''
        for c in range(W):
            if field[r][c] == 0:
                if r == r0 and c == c0:
                    line += 's'
                elif r == r1 and c == c1:
                    line += 'g'
                else:
                    line += '.'
            else:
                if r == r0 and c == c0:
                    line += 'S'
                elif r == r1 and c == c1:
                    line += 'G'
                else:
                    line += '#'
        print(line)

def answer(field,r0,c0,r1,c1,c=0):
    # debug(field,r0,c0,r1,c1,c)
    if field[r0][c0] == 0:
        if r0 == r1:
            if c0 == c1:
                return 0
            return answer(field,r0,c0+1,r1,c1,c)
        if c0 == c1:
            return answer(field,r0+1,c0,r1,c1,c)
        a1 = answer(field,r0+1,c0,r1,c1,c)
        a2 = answer(field,r0,c0+1,r1,c1,c)
        ans = min(*[a1,a2])
        return ans
    if r0 == r1:
        if c0 == c1:
            return 1
        else:
            ans = 9999
            for w in range(c0,c1+1):
                trial = action(field,r0,c0,r1,w)
                a = answer(trial,r0,c0+1,r1,c1,c+1) + 1
                if a < ans:
                    ans = a
            return ans
    else:
        if c0 == c1:
            ans = 9999
            for h in range(r0,r1+1):
                trial = action(field,r0,c0,h,c1)
                a = answer(trial,r0+1,c0,r1,c1,c+1) + 1
                if a < ans:
                    ans = a
            return ans
        else:
            ans = 9999
            for h in range(r0,r1+1):
                for w in range(c0,c1+1):
                    trial = action(field,r0,c0,h,w)
                    a1 = answer(trial,r0,c0+1,r1,c1,c+1) + 1
                    a2 = answer(trial,r0+1,c0,r1,c1,c+1) + 1
                    a = min(*[a1,a2])
                    if a < ans:
                        ans = a
            return ans

print(answer(field,0,0,W-1,H-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]
nummin=0
first=True
while flag:


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

        if first:
            nummin=now[2]
            first=False
        if now[2]<nummin:
            nummin=now[2]
        result.append(now)

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





    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()
    
print(nummin)