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: Accepted

Solution code:

"""
考察:





"""

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

s = []

for i in range(H):

    S = list(input())
    s.append(S)

dis = [[float("inf")] * W for i in range(H)]

if s[0][0] == "#":
    dis[0][0] = 1
else:
    dis[0][0] = 0

for i in range(H):

    for j in range(W):

        if i != H-1:

            if s[i][j] == "." and s[i+1][j] == "#":

                dis[i+1][j] = min(dis[i][j] + 1,dis[i+1][j])

            else:
                dis[i+1][j] = min(dis[i][j],dis[i+1][j])

        if j != W-1:
            if s[i][j] == "." and s[i][j+1] == "#":

                dis[i][j+1] = min(dis[i][j] + 1,dis[i][j+1])

            else:
                dis[i][j+1] = min(dis[i][j],dis[i][j+1])

print (dis[-1][-1])

Submission status: Accepted

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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

def make_array(dims, fill):
    if len(dims) == 1:
        return [fill]*dims[0]
    nxt = dims[1:]
    ret = [make_array(nxt,fill) for i in range(dims[0])]
    return ret

DP = make_array((h,w,2), float("inf"))
for i in range(h):
    for j in range(w):
        if i|j:
            if board[i][j]:
                DP[i][j][1] = min(DP[i][j-1][0]+1, DP[i][j-1][1], DP[i-1][j][0]+1, DP[i-1][j][1])
            else:
                DP[i][j][0] = min(DP[i][j-1][0], DP[i][j-1][1], DP[i-1][j][0], DP[i-1][j][1])
        else:
            if board[i][j]:
                DP[i][j][1] = 1
            else:
                DP[i][j][0] = 0
ans = min(DP[-1][-1])
print(ans)

Submission status: Accepted

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

A = [[0] * w for i in range(h)]

A[0][0] = 0 if S[0][0] == "." else 1

for i in range(1, w):
    if S[0][i-1] == "." and S[0][i] == "#":
        A[0][i] = A[0][i-1] + 1
    else:
        A[0][i] = A[0][i-1]

for i in range(1, h):
    if S[i-1][0] == "." and S[i][0] == "#":
        A[i][0] = A[i-1][0] + 1
    else:
        A[i][0] = A[i-1][0]

for i in range(1, h):
    for j in range(1, w):
        a = A[i-1][j]
        if S[i-1][j] == "." and S[i][j] == "#":
            a += 1
        b = A[i][j-1]
        if S[i][j-1] == "." and S[i][j] == "#":
            b += 1
        A[i][j] = min(a, b)
# print(A)
print(A[-1][-1])

Submission status: Accepted

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

from collections import deque
q = deque([(int(S[0][0]=='#'),0,0)])

INF = float('inf')
costs = [[INF]*W for _ in range(H)]
costs[0][0] = int(S[0][0]=='#')
while q:
    k,x,y = q.popleft()
    for dx,dy in [(1,0),(0,1)]:
        nx,ny = x+dx,y+dy
        if nx >= W: continue
        if ny >= H: continue
        c = int(S[y][x] != S[ny][nx])
        if k+c >= costs[ny][nx]: continue
        costs[ny][nx] = k+c
        q.append((k+c, nx,ny))
print((costs[-1][-1]+1)//2)

Submission status: Accepted

Solution code:
H, W = map(int, raw_input().split())
s = [raw_input() for y in range(H)]

T = [[0]*W for y in range(H)]
for y in range(H):
  for x in range(W):
    if (x, y)==(0, 0):
      T[y][x] = 0 if s[y][x]=="." else 1
    elif x==0:
      T[y][x] = T[y-1][x] + (1 if s[y-1][x]=="." and s[y][x]=="#" else 0)
    elif y==0:
      T[y][x] = T[y][x-1] + (1 if s[y][x-1]=="." and s[y][x]=="#" else 0)
    else:
      T[y][x] = min(
        T[y-1][x] + (1 if s[y-1][x]=="." and s[y][x]=="#" else 0),
        T[y][x-1] + (1 if s[y][x-1]=="." and s[y][x]=="#" else 0))
print T[H-1][W-1]

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
map = []
INF = 10**8
for _ in range(H):
    map.append(input())

dp = [[0 for i in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            if map[i][j] == '#':
                dp[i][j] = 1
        elif i == 0:
            flag = 0
            if map[i][j] == '#' and map[i][j-1] == '.':
                flag = 1
            dp[i][j] = flag + dp[i][j-1]
        elif j == 0:
            flag = 0
            if map[i][j] == "#" and map[i-1][j] == ".":
                flag = 1
            dp[i][j] = flag + dp[i-1][j]
        else:
            flag1 = 0
            flag2 = 0
            if map[i][j] == "#" and map[i][j-1] == ".":
                flag1 = 1
            if map[i][j] == "#" and map[i-1][j] == ".":
                flag2 = 1
            dp[i][j] = min(dp[i][j-1]+flag1,dp[i-1][j]+flag2)

pritn(dp[H-1][W-1])

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
map = []
INF = 10**8
for _ in range(H):
    map.append(input())

dp = [[0 for i in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            if map[i][j] == '#':
                dp[i][j] = 1
        elif i == 0:
            flag = 0
            if map[i][j] == '#' and map[i][j-1] == '.':
                flag = 1
            dp[i][j] = flag + dp[i][j-1]
        elif j == 0:
            flag = 0
            if MAP[i][j] == "#" and MAP[i-1][j] == ".":
                flag = 1
            dp[i][j] = flag + dp[i-1][j]
        else:
            flag1 = 0
            flag2 = 0
            if MAP[i][j] == "#" and MAP[i][j-1] == ".":
                flag1 = 1
            if MAP[i][j] == "#" and MAP[i-1][j] == ".":
                flag2 = 1
            dp[i][j] = min(dp[i][j-1]+flag1,dp[i-1][j]+flag2)

pritn(dp[H-1][W-1])

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
if s[0][0] == '#':
    s[0][0] = 1
else:
    s[0][0] = 0
for i in range(1,w):
    if s[0][i] == '#':
        s[0][i] = s[0][i-1]+1
    else:
        s[0][i] = s[0][i-1]
for i in range(1,h):
    if s[i][0] == '#':
        s[i][0] = s[i-1][0]+1
    else:
        s[i][0] = s[i-1][0]
for i in range(1,w):
    for j in range(1,h):
        if s[i][j] == '#':
            s[i][j] = min(s[i-1][j]+1,s[i][j-1]+1)
        else:
            s[i][j] = min(s[i-1][j],s[i][j-1])
print(s[-1][-1])

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

h, w = na()
m = []
m = [ns() for _ in range(h)]
dp = [[inf] * w for _ in range(h)]
dp[0][0] = int(m[0][0] == '#')
for x in range(1, w):
    dp[0][x] = dp[0][x-1] + (int(m[0][x]=="#") if m[0][x-1]=="." else 0)
for y in range(1, h):
    dp[y][0] = dp[y-1][0] + (int(S[y][0]=="#") if S[y-1][0]=="." else 0)
    for x in range(1, W):
        dp[y][x] = min(
            dp[y][x-1] + (int(S[y][x]=="#") if S[y][x-1]=="." else 0),
            dp[y-1][x] + (int(S[y][x]=="#") if S[y-1][x]=="." else 0)
        )
print(dp[-1][-1])

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
hw = [input() for _ in range(h)]
dp = [[1000]*w for _ in range(h)]
if hw[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0

for i in range(w):
    for j in range(h):
        if i > 0:
            if hw[i][j] == "#" and hw[i-1][j] == ".":
                dp[i][j] = min(dp[i][j], dp[i-1][j] + 1)
            else:
                dp[i][j] = min(dp[i][j], dp[i-1][j])
        if j > 0:
            if hw[i][j] == "#" and hw[i][j-1] == ".":
                dp[i][j] = min(dp[i][j], dp[i][j-1] + 1)
            else:
                dp[i][j] = min(dp[i][j], dp[i][j-1])

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

Submission status: Runtime Error

Solution code:
def resolve():
  H, W = map(int, input().split(" "))
  s = []
  dp = [[0] * W for i in range(H)]
  for _ in range(W):
    s.append([x == "." for x in list(input())])

  for h in range(H):
    for w in range(W):
      if h == 0 and w == 0:
        if not s[0][0]:
          dp[0][0] = 1

      elif h == 0:
        dp[0][w] = dp[0][w-1]
        if s[0][w-1] and not s[0][w]:
          dp[0][w] += 1

      elif w == 0:
        dp[h][0] = dp[h-1][0]
        if s[h-1][0] and not s[h][0]:
          dp[h][0] += 1

      else:
        # 左側と上側を見て、操作回数が最小になるようにする。
        # 上だけをみる
        temp_up = dp[h-1][w]
        if s[h-1][w] and not s[h][w]:
          temp_up += 1

        # 左だけを見る
        temp_left = dp[h][w-1]
        if s[h][w-1] and not s[h][w]:
          temp_left += 1

        dp[h][w] = min(temp_up, temp_left)

  print(dp[H-1][W-1])
  
if __name__ == "__main__":
  resolve()

Submission status: Runtime Error

Solution code:
def resolve():
  H, W = map(int, input().split(" "))
  s = []
  dp = [[0] * W for i in range(H)]
  for _ in range(W):
    s.append([x == "." for x in list(input())])

  for h in range(H):
    for w in range(H):
      if h == 0 and w == 0:
        if not s[0][0]:
          dp[0][0] = 1
      elif h == 0:
        if s[0][w-1] and not s[0][w]:
          dp[0][w] = dp[0][w-1] + 1
        else:
          dp[0][w] = dp[0][w-1]
      elif w == 0:
        if s[h-1][0] and not s[h][0]:
          dp[h][0] = dp[h-1][0] + 1
        else:
          dp[h][0] = dp[h-1][0]
      else:
        # 左側と上側を見て、操作回数が最小になるようにする。
        # 上だけをみる
        if s[h-1][w] and not s[h][w]:
          temp_up = dp[h-1][w] + 1
        else:
          temp_up = dp[h-1][w]
        
        # 左だけを見る
        if s[h][w-1] and not s[h][w]:
          temp_left = dp[h][w-1] + 1
        else:
          temp_left = dp[h][w-1]

        dp[h][w] = min(temp_up, temp_left)

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

if __name__ == "__main__":
  resolve()

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[0]*h
t=[0]*h
for i in range(h):
  st=str(input())
  st=list(st)
  s[i]=st
  tt=[0]*w
  t[i]=tt
if s[0][0]=="#":
  t[0][0]=1
for i in range(1,w):
  t[i][0]=t[i-1][0]
  if s[i][0]=="#":
    t[i][0]=t[i-1][0]+1
for i in range(1,h):
  t[0][i]=t[0][i-1]
  if s[0][i]=="#":
    t[0][i]=t[0][i-1]+1
for i in range(1,h):
  for j in range(1,w):
    t[j][i]=min(t[j-1][i],t[j][i-1])
    if s[j][i]=="#":
      t[j][i]=t[j][i-1]+1
print(t[w-1][h-1])

Submission status: Runtime Error

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


dp = [ [10000]*w for _ in range(h) ]
dp[0][0] = 0

for h_i in range(h):
    for w_i in range(w):
        if h_i+1 < w:
            if sl[h_i][w_i] == '.' and sl[h_i+1][w_i] == '#':
                dp[h_i+1][w_i] = min( dp[h_i+1][w_i], dp[h_i][w_i]+1 )
            else:
                dp[h_i+1][w_i] = min( dp[h_i+1][w_i], dp[h_i][w_i] )

        if w_i+1 < h:
            if sl[h_i][w_i] == '.' and sl[h_i][w_i+1] == '#':
                dp[h_i][w_i+1] = min( dp[h_i][w_i+1], dp[h_i][w_i]+1 )
            else:
                dp[h_i][w_i+1] = min( dp[h_i][w_i+1], dp[h_i][w_i] )


ans = dp[h-1][w-1]
if sl[0][0] == '#': ans += 1
print(ans)

Submission status: Runtime Error

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


dp = [ [10000]*w for _ in range(h) ]
dp[0][0] = 0

for h_i in range(w):
    for w_i in range(h):
        if h_i+1 < w:
            if sl[h_i][w_i] == '.' and sl[h_i+1][w_i] == '#':
                dp[h_i+1][w_i] = min( dp[h_i+1][w_i], dp[h_i][w_i]+1 )
            else:
                dp[h_i+1][w_i] = min( dp[h_i+1][w_i], dp[h_i][w_i] )

        if w_i+1 < h:
            if sl[h_i][w_i] == '.' and sl[h_i][w_i+1] == '#':
                dp[h_i][w_i+1] = min( dp[h_i][w_i+1], dp[h_i][w_i]+1 )
            else:
                dp[h_i][w_i+1] = min( dp[h_i][w_i+1], dp[h_i][w_i] )


ans = dp[h-1][w-1]
if sl[0][0] == '#': ans += 1
print(ans)

Submission status: Runtime Error

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


dp = [ [10000]*h for _ in range(w) ]
dp[0][0] = 0

for wi in range(w):
    for hi in range(h):
        if wi+1 < w:
            if sl[wi][hi] == '.' and sl[wi+1][hi] == '#':
                dp[wi+1][hi] = min( dp[wi+1][hi], dp[wi][hi]+1 )
            else:
                dp[wi+1][hi] = min( dp[wi+1][hi], dp[wi][hi] )

        if hi+1 < h:
            if sl[wi][hi] == '.' and sl[wi][hi+1] == '#':
                dp[wi][hi+1] = min( dp[wi][hi+1], dp[wi][hi]+1 )
            else:
                dp[wi][hi+1] = min( dp[wi][hi+1], dp[wi][hi] )


ans = dp[w-1][h-1]
if sl[0][0] == '#': ans += 1
print(ans)

Submission status: Runtime Error

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


dp = [ [10000]*w for _ in range(h) ]
dp[0][0] = 0

for wi in range(w):
    for hi in range(h):
        if wi+1 < w:
            if sl[wi][hi] == '.' and sl[wi+1][hi] == '#':
                dp[wi+1][hi] = min( dp[wi+1][hi], dp[wi][hi]+1 )
            else:
                dp[wi+1][hi] = min( dp[wi+1][hi], dp[wi][hi] )

        if hi+1 < h:
            if sl[wi][hi] == '.' and sl[wi][hi+1] == '#':
                dp[wi][hi+1] = min( dp[wi][hi+1], dp[wi][hi]+1 )
            else:
                dp[wi][hi+1] = min( dp[wi][hi+1], dp[wi][hi] )


ans = dp[w-1][h-1]
if sl[0][0] == '#': ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
g = [[0] * w for _ in range(h)]#白が1、黒が０
for i in range(h):
    s = list(input())
    for j in range(w):
        if s[j] == '.':
            g[i][j] = 1
INF = 10**9
dp = [[INF] * w for _ in range(h)]
if g[0][0] == 0:
    dp[0][0] = 1
else:
    dp[0][0] = 0

for i in range(h):
    for j in range(w):
        for dx, dy in ((1, 0), (0, 1)):
            nx = j + dx
            ny = i + dy
            if nx >= h or ny >= w:
                continue
            add = 0
            if g[ny][nx] == 0 and g[i][j]  == 1:
                add = 1
            dp[ny][nx] = min(dp[ny][nx], dp[i][j] + add)
            
print(dp[h-1][w-1])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
from collections import namedtuple, defaultdict
from heapq import heappush, heappop
State = namedtuple('State', 'cost node')
Edge = namedtuple('Edge', 'to cost')

def dijkstra(start, end, edges):
    hq = [start]
    visited = set()
    while hq:
        s = heappop(hq)
        if s.node in visited:
            continue
        if s.node == end:
            return s.cost
        visited.add(s.node)
        for n, c in edges[s.node]:
            if n in visited:
                continue
            heappush(hq, State(s.cost+c, n))
    return -1

def solve(H: int, W: int, s: "List[str]"):
    edges = defaultdict(list)
    s = [ss+"#" for ss in s] + ["#"*W]
    for h in range(H):
        for w in range(W):
            edges[(h, w)].append(Edge((h, w+1), 1 if s[h][w+1] == "#" else 0))
            edges[(h, w)].append(Edge((h+1, w), 1 if s[h+1][w] == "#" else 0))
    return dijkstra(State(1 if s[0][0] == "#" else 0, (0, 0)), (H-1, W-1), edges)

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
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]"
    print(solve(H, W, s))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Runtime Error

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

dp[0][0] = 0 if s[0][0] == "." else 1

for i in range(w):
    for j in range(h):
        d, r = INF, INF

        if i != 0:
            r = dp[i - 1][j] + (s[i - 1][j] == "." and s[i][j] == "#")
        if j != 0:
            d = dp[i][j - 1] + (s[i][j - 1] == "." and s[i][j] == "#")

        dp[i][j] = min(r, d, dp[i][j])

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

Submission status: Runtime Error

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

m=[input() for i in range(w)]




print(0)

Submission status: Runtime Error

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

m=[input() for i in range(w)]

cnt=[[1000000 for i in range(h)] for j in range(w)]



print(0)

Submission status: Runtime Error

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

m=[input() for i in range(w)]

cnt=[[1000000 for i in range(h)] for j in range(w)]

if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0


print(0)

Submission status: Runtime Error

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

m=[input() for i in range(w)]

print("A")
cnt=[[1000000 for i in range(h)] for j in range(w)]

if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0

for y in range(w):
    for x in range(h):
        if x>0:
            if m[y][x-1]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1])
        if y>0:
            if m[y-1][x]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x])


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

Submission status: Runtime Error

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

m=[input() for i in range(w)]


cnt=[[1000000 for i in range(h)] for j in range(w)]

if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0

for y in range(w):
    for x in range(h):
        if x>0:
            if m[y][x-1]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1])
        if y>0:
            if m[y-1][x]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x])


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

Submission status: Runtime Error

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

m=[input() for i in range(w)]


cnt=[[1000000 for i in range(h)] for j in range(w)]

print(cnt)
if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0

for y in range(w):
    for x in range(h):
        if x>0:
            if m[y][x-1]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1])
        if y>0:
            if m[y-1][x]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x])


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

Submission status: Runtime Error

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

m=[input() for i in range(w)]


cnt=[[1000000 for i in range(h)]for j in range(w)]


if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0

for y in range(w):
    for x in range(h):
        if x!=0:
            if m[y][x-1]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1])
        if y!=0:
            if m[y-1][x]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x])


print(1)

Submission status: Runtime Error

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

m=[input() for i in range(w)]


cnt=[[1000000 for i in range(h)]for j in range(w)]


if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0

for y in range(w):
    for x in range(h):
        if x!=0:
            if m[y][x-1]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1])
        if y!=0:
            if m[y-1][x]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x])


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

Submission status: Runtime Error

Solution code:
import queue

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

m=[input() for i in range(w)]

q=queue.Queue()

cnt=[[1000000 for i in range(h)]for j in range(w)]


if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0

for y in range(w):
    for x in range(h):
        if x!=0:
            if m[y][x-1]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1])
        if y!=0:
            if m[y-1][x]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x])


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

Submission status: Runtime Error

Solution code:
import queue

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

m=[input() for i in range(w)]

q=queue.Queue()

cnt=[[1000000 for i in range(h)]for j in range(w)]


if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0

for y in range(w):
    for x in range(h):
        if x!=0:
            if m[y][x-1]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y][x-1])
        if y!=0:
            if m[y-1][x]=="." and m[y][x]=="#":
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x]+1)
            else:
                cnt[y][x]=min(cnt[y][x],cnt[y-1][x])


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

Submission status: Runtime Error

Solution code:
import queue

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

m=[input() for i in range(w)]
cx=0
cy=0

q=queue.Queue()

cnt=[[10000000 for i in range(h)]for j in range(w)]


if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0

while cx!=h-1 or cy!=w-1:
    if cx<h-1:
        q.put([cx+1,cy])
        if m[cy][cx]=="." and m[cy][cx+1]=="#":
            cnt[cy][cx+1]=min(cnt[cy][cx+1],cnt[cy][cx]+1)
        else:
            cnt[cy][cx+1]=min(cnt[cy][cx+1],cnt[cy][cx])
    if cy<w-1:
        q.put([cx,cy+1])
        if m[cy][cx]=="." and m[cy+1][cx]=="#":
            cnt[cy+1][cx]=min(cnt[cy+1][cx],cnt[cy][cx]+1)
        else:
            cnt[cy+1][cx]=min(cnt[cy+1][cx],cnt[cy][cx])
    cx,cy=q.get()


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

Submission status: Runtime Error

Solution code:
import queue


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

m=[input() for i in range(w)]

q=queue.Queue()


cnt=[[100000000 for i in range(h)] for j in range(w)]

if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0

q.put([1,1])

c=[1,1]#[w,h]
while c!=[w,h]:
    c=q.get()
    if c[0]+1<=w:
        q.put([c[0]+1,c[1]])
        if m[c[0]][c[1]-1]=="#":
            cnt[c[0]][c[1]-1]=min(cnt[c[0]][c[1]-1],cnt[c[0]-1][c[1]-1]+1)
        else:
            cnt[c[0]][c[1]-1]=min(cnt[c[0]][c[1]-1],cnt[c[0]-1][c[1]-1])
    if c[1]+1<=h:
        q.put([c[0],c[1]+1])
        if m[c[0]-1][c[1]]=="#":
            cnt[c[0]-1][c[1]]=min(cnt[c[0]-1][c[1]],cnt[c[0]-1][c[1]-1]+1)
        else:
            cnt[c[0]-1][c[1]]=min(cnt[c[0]-1][c[1]],cnt[c[0]-1][c[1]-1])

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

Submission status: Runtime Error

Solution code:
import queue


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

m=[input() for i in range(w)]

q=queue.Queue()


cnt=[[100000000 for i in range(h)] for j in range(w)]

if m[0][0]=="#":
    cnt[0][0]=1
else:
    cnt[0][0]=0

q.put([1,1])

c=[0,0]#[w,h]
while c!=[w,h]:
    c=q.get()
    if c[0]+1<=w:
        q.put([c[0]+1,c[1]])
        if m[c[0]][c[1]-1]=="#":
            cnt[c[0]][c[1]-1]=min(cnt[c[0]][c[1]-1],cnt[c[0]-1][c[1]-1]+1)
        else:
            cnt[c[0]][c[1]-1]=min(cnt[c[0]][c[1]-1],cnt[c[0]-1][c[1]-1])
    if c[1]+1<=h:
        q.put([c[0],c[1]+1])
        if m[c[0]-1][c[1]]=="#":
            cnt[c[0]-1][c[1]]=min(cnt[c[0]-1][c[1]],cnt[c[0]-1][c[1]-1]+1)
        else:
            cnt[c[0]-1][c[1]]=min(cnt[c[0]-1][c[1]],cnt[c[0]-1][c[1]-1])

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

Submission status: Runtime Error

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


def solve(H: int, W: int, s: "List[str]"):
    dp = [[0 for w in range(W)] for h in range(H)] # dp[h][w]はh,wに達するための回数
    if s[0][0] == '#':
        dp[0][0] = 1

    for w in range(W-1): # 0行目について
        if s[0][w+1] == '.': # 移動先が白だったら特に変わりなし
            dp[0][w+1] = dp[0][w]
        elif s[0][w] == '.' and s[0][w+1] == '#': # 移動元が白で先が黒ならば、新しく施行1回追加
            dp[0][w+1] = dp[0][w] + 1
        elif s[0][w] == '#' and s[0][w+1] == '#': # 移動元も先も黒だったとしたら、試行回数は変わらない
            dp[0][w+1] = dp[0][w]

    for h in range(H-1): # 1列目について
        if s[h+1][0] == '.':
            dp[h+1][0] = dp[h][0]
        elif s[h][0] == '.' and s[h+1][0] == '#':
            dp[h+1][0] = dp[h][0] + 1
        elif s[h][0] == '#' and s[h+1][0] == '#':
            dp[h+1][0] = dp[h][0]

    for h in range(1, H):
        for w in range(W-1):
            if s[h][w+1] == '.':
                dp[h][w+1] = min(dp[h][w], dp[h-1][w+1])
            elif s[h][w] == '.' and s[h][w+1] == '#':
                if s[h-1][w+1] == '.':
                    dp[h][w+1] = min(dp[h][w]+1, dp[h-1][w+1]+1)
                elif s[h-1][w+1] == '#':
                    dp[h][w+1] = min(dp[h][w]+1, dp[h-1][w+1])
            elif s[h][w] == '#' and s[h][w+1] == '#':
                if s[h-1][w+1] == '.':
                    dp[h][w+1] = min(dp[h][w], dp[h-1][w+1]+1)
                elif s[h-1][w+1] == '#':
                    dp[h][w+1] = min(dp[h][w], dp[h-1][w+1])
    print(dp[H-1][W-1])
    return


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


def solve(H: int, W: int, s: "List[str]"):
    dp = [[0 for w in range(W)] for h in range(H)] # dp[h][w]はh,wに達するための回数
    if s[0][0] == '#':
        dp[0][0] = 1
    
    for w in range(W-1): # 0行目について
        if s[0][w+1] == '.': # 移動先が白だったら特に変わりなし
            dp[0][w+1] = dp[0][w]
        elif s[0][w] == '.' and s[0][w+1] == '#': # 移動元が白で先が黒ならば、新しく施行1回追加
            dp[0][w+1] = dp[0][1] + 1
        elif s[0][w] == '#' and s[0][w+1] == '#': # 移動元も先も黒だったとしたら、試行回数は変わらない
            dp[0][w+1] = dp[0][w]

    for h in range(H-1): # 1列目について
        if s[h+1][0] == '.':
            dp[h+1][0] = dp[h][0]
        elif s[h][0] == '.' and s[h+1][0] == '#':
            dp[h+1][0] = dp[h][0] + 1
        elif s[h][0] == '#' and s[h+1][0] == '#':
            dp[h+1][0] = dp[h][0]

    for h in range(1, H):
        for w in range(W-1):
            if s[h][w+1] == '.':
                dp[h][w+1] = min(dp[h][w], dp[h-1][w+1])
            elif s[h][w] == '.' and s[h][w+1] == '#':
                if s[h-1][w+1] == '.':
                    dp[h][w+1] = min(dp[h][w]+1, dp[h-1][w+1]+1)
                elif s[h-1][w+1] == '#':
                    dp[h][w+1] = min(dp[h][w]+1, dp[h-1][w+1])
            elif s[h][w] == '#' and s[h][w+1] == '#':
                if s[h-1][w+1] == '.':
                    dp[h][w+1] = min(dp[h][w], dp[h-1][w+1]+1)
                elif s[h-1][w+1] == '#':
                    dp[h][w+1] = min(dp[h][w], dp[h-1][w+1])
    print(dp[H-1][W-1])
    return


# Generated by 1.1.7.1 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:
import copy
import heapq
H, W = map(int, input().split())
S = []
cnt_list = []
min_cnt = 10**100
for _ in range(H):
    S.append(list(input()))

def move(i,j,cnt,frag):
    global min_cnt
    if cnt > min_cnt:
        return
    c_cnt = copy.deepcopy(cnt)
    c_frag = copy.deepcopy(frag)
    if frag != S[i][j]:
        c_cnt = cnt + 1
    c_frag = S[i][j]
    if i != H-1:
        move(i+1,j,c_cnt,c_frag)
    if j != W-1:
        move(i,j+1,c_cnt,c_frag)
    if i == H-1 and j == W-1:
        if c_cnt < min_cnt:
            min_cnt = c_cnt


if S[0][0] == '#' and S[H][W] == '#':
    a = 1
else :
    a = 0

move(0,0,0,S[0][0])

print(min_cnt//2 + a)