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

Solution (Python):
h,w = map(int,input().split())
raw = [[0 for _ in range(w+1)]]
for _ in range(h):
  tmp = input()
  add = [0]
  for j in range(w):
    if tmp[j] == '.':
      add.append(0)
    else:
      add.append(1)
  raw.append(add)

  
ans = []
for i in range(1,h+1):
  add = []
  for j in range(1,w+1):
    can = []
    if i == j == 1:
      add.append(raw[1][1])
      continue
    if i != 1:
      if raw[i][j] == 0:
        can.append(ans[i-2][j-1])
      elif raw[i-1][j] == 1:
        can.append(ans[i-2][j-1])
      else:
        can.append(ans[i-2][j-1]+1)
    if j != 1:
      if raw[i][j] == 0:
        can.append(add[j-2])
      elif raw[i][j-1] == 1:
        can.append(add[j-2])
      else:
        can.append(add[j-2]+1)
    add.append(min(can))
  ans.append(add)

  
print(ans[-1][-1])

Solution (Python):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
dp = [[100000]*W for _ in range(H)]
dp[0][0] = int(S[0][0]=="
for x in range(1, W):
    dp[0][x] = dp[0][x-1] + (int(S[0][x]=="
for y in range(1, H):
    dp[y][0] = dp[y-1][0] + (int(S[y][0]=="
    for x in range(1, W):
        dp[y][x] = min(
            dp[y][x-1] + (int(S[y][x]=="
            dp[y-1][x] + (int(S[y][x]=="
        )
print(dp[-1][-1])

Solution (Python):
h,w=map(int,input().split())
S=[]
for _ in range(h):
    S.append(input())
tbl=[[10**3]*w for _ in range(h)]
if S[0][0]=="
    tbl[0][0]=1
else:
    tbl[0][0]=0
for i in range(h):
    for j in range(w):
        if i>=1 and S[i][j]=="
            tbl[i][j]=min(tbl[i][j],tbl[i-1][j]+1)
        elif i>=1:
            tbl[i][j]=min(tbl[i][j],tbl[i-1][j])
        if j>=1 and S[i][j]=="
            tbl[i][j]=min(tbl[i][j],tbl[i][j-1]+1)
        elif j>=1:
            tbl[i][j]=min(tbl[i][j],tbl[i][j-1])
print(tbl[h-1][w-1])

Solution (Python):
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
C=[]
for i in range(h):
    sub_list=input().rstrip()
    C.append(sub_list)
dp=[[0]*w for i in range(h)]
if C[0][0]=='.':
    dp[0][0]=0
else:
    dp[0][0]=1
for i in range(h):
    for j in range(w):
        if i==0 and j==0:
            pass
        else:
            if i==0:
                if C[i][j]=='.':
                    dp[i][j]=dp[i][j-1]
                else:
                    if C[i][j-1]=='.':
                        dp[i][j]=dp[i][j-1]+1
                    else:
                        dp[i][j] = dp[i][j - 1]
            elif j==0:
                if C[i][j]=='.':
                    dp[i][j]=dp[i-1][j]
                else:
                    if C[i-1][j]=='.':
                        dp[i][j]=dp[i-1][j]+1
                    else:
                        dp[i][j] = dp[i-1][j]
            else:
                if C[i][j]=='.':
                    dp[i][j]=min(dp[i][j-1],dp[i-1][j])
                else:
                    if C[i][j-1]=='.':
                        dp[i][j]=dp[i][j-1]+1
                    else:
                        dp[i][j] = dp[i][j - 1]
                    if C[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])
print(dp[h-1][w-1])

Solution (Python):
h, w = map(int,input().split())
maero='.'*w
maekai=[999]*w
maekai[0]=0
allkai=[]
for i in range(h):
    root=input()
    nowkai=[]
    for j in range(w):
        if root[j]=='.':
            if j==0:
                nowkai.append(maekai[0])
            else:
                nowkai.append(min(nowkai[-1], maekai[j]))
        else:
            if j==0:
                if maero[j]=='.':
                    nowkai.append(maekai[0]+1)
                else:
                    nowkai.append(maekai[0])
            else:
                if root[j-1]=='.':
                    koho1=nowkai[-1]+1
                else:
                    koho1=nowkai[-1]
                if maero[j]=='.':
                    koho2=maekai[j]+1
                else:
                    koho2=maekai[j]
                
                nowkai.append(min(koho1, koho2))
    maero=root
    allkai.append(nowkai)
    maekai=nowkai


    
print(allkai[-1][-1])

Solution (Python):
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
state = [list(input().rstrip()) for _ in range(H)]

def next(h, w):
    ret = []
    if h != H-1:
        ret.append((h+1, w))
    if w != W-1:
        ret.append((h, w+1))

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

for w in range(W-1):
    t = dp[0][w]
    if state[0][w] != state[0][w+1]:
        t += 1
    dp[0][w+1] = t

for h in range(1, H):
    tmp = dp[h-1][0]
    if state[h-1][0] != state[h][0]:
        tmp += 1
    dp[h][0] = tmp
    for w in range(1, W):
        t1 = dp[h][w-1]
        t2 = dp[h-1][w]
        if state[h][w-1] != state[h][w]:
            t1 += 1
        if state[h-1][w] != state[h][w]:
            t2 += 1
        dp[h][w] = min(t1, t2)

p = dp[H-1][W-1]
if p%2 == 0:
    if state[0][0] == ".":
        ans = p//2
    else:
        ans = p//2+1
else:
    ans = (p+1)//2
print(ans)

Solution (Python):
H, W = map(int,input().split())
s = list(list(input())for i in range(H))
cost = [[10**9]*W for _ in range(H)]
cost[0][0] = 0
if s[0][0] == "
    cost[0][0] += 1
for i in range(H-1):
    for j in range(W-1):
        if(s[i][j] == "." and s[i][j+1] == "
            cost[i][j+1] = min(cost[i][j+1],cost[i][j]+1)
        else:
            cost[i][j+1] = min(cost[i][j+1],cost[i][j])
        if(s[i][j] == "." and s[i+1][j] == "
            cost[i+1][j] = min(cost[i+1][j],cost[i][j]+1)
        else:
            cost[i+1][j] = min(cost[i+1][j],cost[i][j])
    if(s[i][-1] == "." and s[i+1][-1] == "
        cost[i+1][-1] = min(cost[i+1][-1],cost[i][-1]+1)
    else:
        cost[i+1][-1] = min(cost[i+1][-1],cost[i][-1])
for j in range(W-1):
    if(s[H-1][j] == "." and s[H-1][j+1] == "
        cost[H-1][j+1] = min(cost[H-1][j+1],cost[H-1][j]+1)
    else:
        cost[H-1][j+1] = min(cost[H-1][j+1],cost[H-1][j])

print(cost[-1][-1])

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

DP = [[0] * W for _ in range(H)]
if s[0][0] == "
  DP[0][0] = 1

for i in range(1, W):
  if s[0][i - 1] != s[0][i]:
    DP[0][i] = DP[0][i - 1] + 1
  else:
    DP[0][i] = DP[0][i - 1]

for i in range(1, H):
  DP[i][0] = DP[i - 1][0]
  if s[i][0] != s[i - 1][0]:
    DP[i][0] += 1
  for j in range(1, W):
    x = DP[i][j - 1]
    if s[i][j] != s[i][j - 1]:
      x += 1
    y = DP[i - 1][j]
    if s[i][j] != s[i - 1][j]:
      y += 1
    DP[i][j] = min(x, y)

print((DP[-1][-1] + 1) // 2)

Solution (Python):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
d = [[1000000] * w for _ in range(h)]
d[0][0] = 0
for i in range(h):
    for j in range(w):
        if i < h - 1:
            d[i + 1][j] = min(d[i + 1][j], d[i][j] +
                              (1 if s[i][j] != s[i + 1][j] else 0))
        if j < w - 1:
            d[i][j + 1] = min(d[i][j + 1], d[i][j] +
                              (1 if s[i][j] != s[i][j + 1] else 0))
ans = d[h - 1][w - 1]
if s[0][0] == '
    ans += 1
if s[h - 1][w - 1] == '
    ans += 1
print(ans // 2)

Solution (Python):

n,m=map(int,input().split())
it=[[0 for i in range(m)] for j in range(n)]
for i in range(n):
    j=input()
    for k in range(m):
        if j[k]=="
            it[i][k]=0
        else:
            it[i][k]=1
dp=[[0 for i in range(m)] for j in range(n)]
for i in range(n):
    for j in range(m):
        
        if(i>0 or j>0):
            if(i==0):
                dp[i][j]=dp[i][j-1]
                if(it[i][j-1]==1 and it[i][j]==0):
                    dp[i][j]+=1
            elif j==0:
                dp[i][j]=dp[i-1][j]
                if(it[i][j]==0 and it[i-1][j]==1):
                    dp[i][j]+=1
            else:
                a=0
                b=0
                if(it[i][j]==0):
                    if(it[i-1][j]==1):
                        a=1
                    if(it[i][j-1]==1):
                        b=1
                dp[i][j]=min(dp[i-1][j]+a,dp[i][j-1]+b)
        else:
            if it[i][j]==0:
                dp[i][j]=1

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

Solution (Python):
h,w=map(int,input().split())
b=[list(input())for _ in range(h)]
ans=[w*[10**9]for _ in range(h)]
ans[0][0]=(b[0][0]=="
for i in range(h):
  for j in range(w):
    if i==j==0:continue
    if i==0:
      if b[0][j-1]=="." and b[0][j]=="
      else:ans[0][j]=ans[0][j-1]
    elif j==0:
      if b[i-1][0]=="." and b[i][0]=="
      else:ans[i][0]=ans[i-1][0]
    else:
      ii=ans[i-1][j]
      if b[i-1][j]=="." and b[i][j]=="
      jj=ans[i][j-1]
      if b[i][j-1]=="." and b[i][j]=="
      ans[i][j]=min(ii,jj)
print(ans[-1][-1])

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



H,W = LI()
S = [list(map(str, input())) for _ in range(H)]

count = [[0]*W for _ in range(H)]
for i in range(H)[::-1]:
    for j in range(W)[::-1]:
        if i==H-1 and j==W-1:
            if S[i][j] == '
                count[i][j] = 1
            else:
                count[i][j] = 0
        elif i==H-1:
            if S[i][j+1]=='.' and S[i][j]=='
                count[i][j] = count[i][j+1]+1
            else:
                count[i][j] = count[i][j+1]
        elif j==W-1:
            if S[i+1][j]=='.' and S[i][j]=='
                count[i][j] = count[i+1][j]+1
            else:
                count[i][j] = count[i+1][j]
        else:
            if S[i][j+1]=='.' and S[i][j]=='
                one = count[i][j+1]+1
            else:
                one = count[i][j+1]
            if S[i+1][j]=='.' and S[i][j]=='
                two = count[i+1][j]+1
            else:
                two = count[i+1][j]
            count[i][j] = min(one,two)

print(count[0][0])

Solution (Python):
import sys
input=lambda: sys.stdin.readline().rstrip()
h,w=map(int,input().split())
A=[input() for i in range(h)]
n=h*w
edge=[[] for i in range(n)]
for i in range(n):
  h1,w1=i//w,i%w
  if w1!=w-1:
    if A[h1][w1]=="." and A[h1][w1+1]=="
      edge[i].append(10**6+i+1)
    else:
      edge[i].append(i+1)
  if h1!=h-1:
    if A[h1][w1]=="." and A[h1+1][w1]=="
      edge[i].append(10**6+i+w)
    else:
      edge[i].append(i+w)

import heapq
def dijkstra_heap(s):
  d=[float("inf")]*n
  used=[True]*n
  d[s]=0
  used[s]=False
  edgelist=[]
  for e in edge[s]:
    heapq.heappush(edgelist,e)
  while edgelist:
    minedge=heapq.heappop(edgelist)
    minedge_cost=minedge//(10**6)
    minedge_v=minedge%(10**6)
    if not used[minedge_v]:
      continue
    d[minedge_v] = minedge_cost
    used[minedge_v] = False
    for e in edge[minedge_v]:
      e_cost=e//(10**6)
      e_v=e%(10**6)
      if used[e_v]:
        heapq.heappush(edgelist,e+minedge_cost*10**6)
  return d
D=dijkstra_heap(0)
if A[0][0]=="
  print(D[h*w-1]+1)
else:
  print(D[h*w-1])

Solution (Python):
import queue

H, W = map(int,input().split())
b = [list(input()) for _ in range(H)]
ini = 0
if b[0][0] == '
    ini = 1
dist = [[ini for _ in range(W)] for _ in range(H)]
start = (0,0)
qq = queue.Queue()
for i in range(H):
    for j in range(W):
        tgt = []
        if j==0:
            pass
        else:
            if b[i][j-1] == '.' and b[i][j] == '
                tgt.append(dist[i][j-1] + 1)
            else:
                tgt.append(dist[i][j-1])
        if i==0:
            pass
        else:
            if b[i-1][j] == '.' and b[i][j] == '
                tgt.append(dist[i-1][j] + 1)
            else:
                tgt.append(dist[i-1][j])
        if i+j==0:
            continue
        dist[i][j] = min(tgt)
print(dist[H-1][W-1])

Solution (Python):
import sys
input = sys.stdin.readline

H,W=map(int,input().split())
MAP=[input().strip() for i in range(H)]

ANS=[[1<<30]*W for i in range(H)]

if MAP[0][0]==".":
    ANS[0][0]=0
else:
    ANS[0][0]=1

for i in range(H):
    for j in range(W):
        if i+1<H:
            if MAP[i][j]==MAP[i+1][j]:
                ANS[i+1][j]=min(ANS[i+1][j],ANS[i][j])
            else:
                ANS[i+1][j]=min(ANS[i+1][j],ANS[i][j]+1)

        if j+1<W:
            if MAP[i][j]==MAP[i][j+1]:
                ANS[i][j+1]=min(ANS[i][j+1],ANS[i][j])
            else:
                ANS[i][j+1]=min(ANS[i][j+1],ANS[i][j]+1)
            
print((ANS[-1][-1]+1)//2)

Solution (Python):
import sys
from collections import deque

def solve():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    S = [input().strip("\n") for _ in range(H)]
    DP = [[100000 for w in range(W)] for _ in range(H)]
    ncol = (0 if S[0][0] == "." else 1)
    q = deque()
    q.append((0, 0, ncol, ncol))
    Se = ((0, 1), (1, 0))
    while q:
        h, w, d, c = q.popleft()
        if d < DP[h][w]:
            DP[h][w] = d
            if c == 0:
                for dh, dw in Se:
                    if h + dh < H and w + dw < W:
                        if S[h+dh][w+dw] == ".": q.append((h+dh, w+dw, d, 0))
                        else: q.append((h+dh, w+dw, d+1, 1))
            else:
                for dh, dw in Se:
                    if h + dh < H and w + dw < W:
                        if S[h+dh][w+dw] == ".": q.append((h+dh, w+dw, d, 0))
                        else: q.append((h+dh, w+dw, d, 1))
    print(DP[H-1][W-1])
    return 0

if __name__ == "__main__":
    solve()

Solution (Python):

"""
考察:





"""

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

Solution (Python):
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=="

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)

Solution (Python):
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[-1][-1])

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

from collections import deque
q = deque([(int(S[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)

Solution (Python):
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]=="
    elif y==0:
      T[y][x] = T[y][x-1] + (1 if s[y][x-1]=="." and s[y][x]=="
    else:
      T[y][x] = min(
        T[y-1][x] + (1 if s[y-1][x]=="." and s[y][x]=="
        T[y][x-1] + (1 if s[y][x-1]=="." and s[y][x]=="
print T[H-1][W-1]