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):
import math  
import bisect  
import queue  


if __name__ == '__main__':
    H, W = map(int, input().split())
    S = [[s for s in input()] for _ in range(H)]

    INF = 1000000005
    traj = [[INF for _ in range(W)] for _ in range(H)]
    que = queue.Queue()
    que.put((0, 0))
    traj[0][0] = 0 if S[0][0] == '.' else 1
    while not que.empty():
        h, w = que.get()
        d = ((0, 1), (1, 0))
        for dh, dw in d:
            nh = h + dh
            nw = w + dw
            if nh < 0 or H <= nh or nw < 0 or W <= nw:
                continue
            cost = traj[h][w]
            if S[h][w] == '.' and S[nh][nw] == '
                cost += 1
            if cost < traj[nh][nw]:
                traj[nh][nw] = cost
                que.put((nh, nw))

    print(traj[-1][-1])

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

dp = [[float('inf')]* W for _ in range(H)]

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

for c in range(W):
    for r in range(H):
        if r > 0:
            count=0
            if s[r-1][c]=='.' and s[r][c]=='
                count = 1
            dp[r][c] = min(dp[r][c], dp[r-1][c]+count)
        if c > 0:
            count=0
            if s[r][c-1]=='.' and s[r][c]=='
                count = 1
            dp[r][c] = min(dp[r][c], dp[r][c-1]+count)
print(dp[H-1][W-1])

Solution (Python):

import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def lprint(l): print(*l, sep='\n')
def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

H,W = imn()

s = [input() for _ in range(H)]
dp = [[INF for _ in range(W)] for _ in range(H)]
dp[0][0] = 0
if s[0][0] == '

for i in range(H):
    for j in range(W):
        if i+1 < H:
            cnt = dp[i][j]
            if s[i][j] == "." and s[i+1][j] == "
                cnt += 1 
            dp[i+1][j] = min(dp[i+1][j], cnt)
        if j+1 < W:
            cnt = dp[i][j]
            if s[i][j] == "." and s[i][j+1] == "
                cnt += 1
            dp[i][j+1] = min(dp[i][j+1], cnt)
print(dp[-1][-1])

Solution (Python):

import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def lprint(l): print(*l, sep='\n')
def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

H,W = imn()

s = [input() for _ in range(H)]
d = [[INF for _ in range(W)] for _ in range(H)]
d[0][0] = 0
if s[0][0] == '

xd = [1, 0]
yd = [0, 1]
que = deque([(0, 0)])
cnt = 0
while len(que) > 0:
    cnt += 1
    point = que.popleft()
    x = point[0]
    y = point[1]
    if x == W-1 and y == H-1: continue
    for i in range(2):
        nx = x + xd[i]
        ny = y + yd[i]
        if nx >= W or ny >= H: continue
        if s[ny][nx] == '
            if d[ny][nx] > d[y][x]+1:
                d[ny][nx] = d[y][x]+1
                que.append((nx,ny))
        else:
            if d[ny][nx] > d[y][x]:
                d[ny][nx] = d[y][x]
                que.append((nx,ny))
print(d[H-1][W-1])

Solution (Python):

import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def lprint(l): print(*l, sep='\n')
def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

H,W = imn()

s = [input() for _ in range(H)]
d = [[INF for _ in range(W)] for _ in range(H)]
d[0][0] = 0
if s[0][0] == '

xd = [1, 0]
yd = [0, 1]
que = deque([(0, 0)])
cnt = 0
while len(que) > 0:
    cnt += 1
    point = que.popleft()
    x = point[0]
    y = point[1]
    if x == W-1 and y == H-1: continue
    for i in range(2):
        nx = x + xd[i]
        ny = y + yd[i]
        if nx >= W or ny >= H: continue
        if s[ny][nx] == '
            if d[ny][nx] > d[y][x]+1:
                d[ny][nx] = d[y][x]+1
                que.append((nx,ny))
        else:
            if d[ny][nx] > d[y][x]:
                d[ny][nx] = d[y][x]
                que.append((nx,ny))
print(d[H-1][W-1])

Solution (Python):
h,w = map(int,input().split())
s = [input()for _ in range(h)]
dp = [[10**8]*(w+1) for _ in range(h+1)]
dp[0][0] = 0
for i in range(h):
    for j in range(w): 
        if s[i][j] == '.':
            if i+1 < h and s[i+1][j] == '
            else:dp[i+1][j] = min(dp[i+1][j],dp[i][j])
            if j+1 < w and s[i][j+1] == '
            else:dp[i][j+1] = min(dp[i][j+1],dp[i][j])
        else:
            dp[i+1][j]  = min(dp[i+1][j] ,dp[i][j])
            dp[i][j+1] = min(dp[i][j+1],dp[i][j])

            
        
ans = dp[h-1][w-1]
if s[0][0] == '
print(ans)

Solution (Python):
H, W = map(int, input().split())
grid = [input() for _ in range(H)]
dp = [[float('inf')] * W for i in range(H)]
dp[0][0] = 1 if grid[0][0] == '

for i in range(H):
    for j in range(W):
        for y, x in ((1,0), (0,1)):
            ni, nj = i + y, j + x
            if ni >= H or nj >= W:
                continue
            c = 0
            if grid[ni][nj] == '
                c = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + c)

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

Solution (Python):
h,w = map(int,input().split())
s = [list(str(input())) for i in range(h)]
dp = [[10 ** 10] * w for i in range(h)]
if s[0][0] == "
else:dp[0][0] = 0
for i in range(w):
    for j in range(h):
        if s[j][i] == "
            s[j][i] = 1
        else:
            s[j][i] = 0
for i in range(1,h+w-1):
    for j in range(i+1):
        if 0 < j < h and 0 <= i-j < w:
            dp[j][i-j] = min(dp[j-1][i-j]+(s[j][i-j]&~s[j-1][i-j]),dp[j][i-j])
        if 0 < i-j < w and 0 <= j < h:
            dp[j][i-j] = min(dp[j][i-j-1]+(s[j][i-j]&~s[j][i-j-1]),dp[j][i-j])
print(dp[h-1][w-1])

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

s = []
for _ in range(h):
	s.append(list(input()))

inf = float('inf')
dp = [[inf for _ in range(w)] for _ in range(h)]
dp[0][0] = 0

for i in range(h):
	for j in range(w):
		if s[i][j] == ".":
			if j > 0 and i > 0:
				dp[i][j] = min(dp[i-1][j],dp[i][j-1])
			elif j > 0 and i == 0:
				dp[i][j] = dp[i][j-1]
			elif j == 0 and i > 0:
				dp[i][j] = dp[i-1][j]
		else:
			tmp1 = inf
			tmp2 = inf
			if i > 0:
				tmp1 = dp[i-1][j]
				if s[i-1][j] == ".":
					tmp1 += 1
			if j > 0:
				tmp2 = dp[i][j-1]
				if s[i][j-1] == ".":
					tmp2 += 1
			dp[i][j] = min(tmp1,tmp2)
			if i==0 and j==0:
				dp[i][j] = 1

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

Solution (Python):
from itertools import product
from collections import deque

class ZeroOneBFS:
    def __init__(self, N):
        self.N = N 
        self.E = [[] for _ in range(N)]
        
    def add_edge(self, init, end, weight, undirected=False):
        assert weight in [0, 1]
        self.E[init].append((end, weight))
        if undirected: self.E[end].append((init, weight))
        
    def distance(self, s):
        INF = float('inf')
        E, N = self.E, self.N
        dist = [INF] * N 
        prev = [-1] * N 
        dist[s] = 0
        dq = deque([(0, s)]) 
        n_visited = 0 
        while dq:
            d, v = dq.popleft() 
            if dist[v] < d: continue 
            for u, c in E[v]:
                temp = d + c
                if dist[u] > temp:
                    dist[u] = temp; prev[u] = v
                    if c == 0: dq.appendleft((temp, u))
                    else: dq.append((temp, u))
            n_visited += 1
            if n_visited == N: break
        self.dist, self.prev = dist, prev
        return dist
    
    def shortest_path(self, t):
        P = []
        prev = self.prev
        while True:
            P.append(t)
            t = prev[t]
            if t == -1: break
        return P[::-1]

H, W = map(int, input().split())
zobfs = ZeroOneBFS(H * W)
 
def vtx(i, j): return i*W + j
def coord(n): return divmod(n, W)
 
grid = [input() for _ in range(H)] 
E = [[] for _ in range(H * W)]
ans = 0 if grid[0][0] == '.' else 1
for i, j in product(range(H), range(W)):
    v = vtx(i, j)
    check = [vtx(i+dx, j+dy) for dx, dy in [(1, 0), (0, 1)] if i+dx <= H-1 and j+dy <= W-1]
    for u in check:
        x, y = coord(u)
        if grid[i][j] == '.' and grid[x][y] == '
            zobfs.add_edge(v, u, 1)
        else:
            zobfs.add_edge(v, u, 0)
dist = zobfs.distance(0)
ans += dist[vtx(H-1, W-1)]
print(ans)

Solution (Python):
h , w = map(int,input().split())
p = []
for i in range(h):
    kar = list(input())
    p.append(kar)
d = [[0 for i in range(w)] for j in range(h)]
for i in range(w):
    if i == 0:
        if p[0][0] == "
            d[0][0] = 1
    else:
        if p[0][i-1] == "." and p[0][i] == "
            d[0][i] += 1
        d[0][i] += d[0][i-1]
for i in range(1,h):
    if p[i-1][0] == "." and p[i][0] == "
        d[i][0] += 1
    d[i][0] += d[i-1][0]    

for x in range(1,w):
    for y in range(1,h):
        m = 0
        s = 0
        if p[y][x-1] == "." and p[y][x] == "
            m += 1
        m += d[y][x-1]
        if p[y-1][x] == "." and p[y][x] == "
            s += 1
        s += d[y-1][x]
        d[y][x] = min(m,s)
print(d[h-1][w-1])

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    H, W = map(int, rl().split())
    s = [list(input()) for _ in range(H)]
    
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = s[0][0] == '
    for i in range(1, W):
        dp[0][i] = dp[0][i - 1] + (s[0][i - 1] == '.' and s[0][i] == '
    for i in range(1, H):
        dp[i][0] = dp[i - 1][0] + (s[i - 1][0] == '.' and s[i][0] == '
    
    for i in range(1, H):
        for j in range(1, W):
            dy = s[i - 1][j] == '.' and s[i][j] == '
            dx = s[i][j - 1] == '.' and s[i][j] == '
            dp[i][j] = min(dp[i - 1][j] + dy, dp[i][j - 1] + dx)
    print(dp[-1][-1])


if __name__ == '__main__':
    solve()

Solution (Python):
MOD = 10**9 + 7
INT_MAX = (1 << 63) - 1


if __name__ == "__main__":
    

    
    m, n = map(int, input().split())
    grid = [input().strip() for _ in range(m)]

    dp = [float('inf')] * n
    for i, row in enumerate(grid):
        for j, val in enumerate(row):
            if i == 0 and j == 0:
                dp[j] = grid[0][0] == '
            else:
                temp = INT_MAX
                if i > 0:
                    temp = min(dp[j] + (val == '
                if j > 0:
                    temp = min(dp[j-1] + (val == '
                dp[j] = temp
        

    print(dp[-1])

Solution (Python):

import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    H,W = LMIIS()
    table = [0]*H
    for i in range(H):
        table[i] = input()
    num_route = [[0]*W for _ in range(H)]

    if table[0][0] == '
        num_route[0][0] = 1

    for i in range(1,W):
        if table[0][i] == '
            num_route[0][i] = num_route[0][i-1] + 1
        else:
            num_route[0][i] = num_route[0][i-1]

    for i in range(1,H):
        if table[i][0] == '
            num_route[i][0] = num_route[i-1][0] + 1
        else:
            num_route[i][0] = num_route[i-1][0]
    
    for i in range(1,H):
        for j in range(1,W):
            l = u = 0
            if table[i][j] == '
                l = num_route[i][j-1] + 1
            else:
                l = num_route[i][j-1]
            if table[i][j] == '
                u = num_route[i-1][j] + 1
            else:
                u = num_route[i-1][j]
            num_route[i][j] = min(l,u)
            
    
    print(num_route[H-1][W-1])

if __name__ == '__main__':
    main()

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    H, W = Scanner.map_int()
    S = Scanner.string_list(H)
    dp = [[0 for _ in range(W)] for _ in range(H)]
    if S[0][0] == '
        dp[0][0] = 1
    for w in range(W - 1):
        dp[0][w + 1] = dp[0][w] + (S[0][w + 1] != S[0][w])
    for h in range(H - 1):
        dp[h + 1][0] = dp[h][0] + (S[h + 1][0] != S[h][0])
    for h in range(H - 1):
        for w in range(W - 1):
            s = dp[h][w + 1] + (S[h + 1][w + 1] != S[h][w + 1])
            t = dp[h + 1][w] + (S[h + 1][w + 1] != S[h + 1][w])
            dp[h + 1][w + 1] = min(s, t)

    print(Math.round_up(dp[H - 1][W - 1], 2))


def main():
    
    solve()


if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict

BIG = 10000

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

s = ['']*h
for i in range(h):
    s[i] = input()

cost = [[0]*w]*h

if s[0][0] == '
    cost[0][0] = 1

for r in range(h):
    for c in range(w):
        if (r, c) == (0, 0): continue
        x = BIG
        if s[r][c] == '.':
            if r > 0:
                x = min(x, cost[r-1][c])
            if c > 0:
                x = min(x, cost[r][c-1])
        else:  
            if r > 0:
                if s[r-1][c] == '
                    x = min(x, cost[r-1][c])
                else:
                    x = min(x, cost[r-1][c]+1)
            if c > 0:
                if s[r][c-1] == '
                    x = min(x, cost[r][c-1])
                else:
                    x = min(x, cost[r][c-1]+1)
        cost[r][c] = x

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

Solution (Python):


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

meizu=[]
for _ in range(H):
    meizu.append(list(input()))

dp=[[10**9 for w in range(W)] for h in range(H)]
dp[0][0]=1 if meizu[0][0]=="

for h in range(H):
    for w in range(W):
        if h!=H-1:
            dp[h+1][w]=min(dp[h+1][w],dp[h][w]+1 if meizu[h+1][w]=="
        if w!=W-1:
            dp[h][w+1]=min(dp[h][w+1],dp[h][w]+1 if meizu[h][w+1]=="
    
print(dp[-1][-1])

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

INF = H + W

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

if S[0][0] == '
    dp[0][0] = 1
else:
    dp[0][0] = 0

for i in range(H):
    for j in range(W):
        if i - 1 >= 0:
            if S[i][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 - 1 >= 0:
            if S[i][j] == '
                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])

Solution (Python):

h,w=map(int,input().split())
s=[input() for i in range(h)]
inf=10**18
dp=[[inf]*(w+1) for i in range(h+1)]
if s[0][0]=="
  dp[0][0]=1
else:
  dp[0][0]=0

for i in range(h):
  for j in range(w):
    for y,x in ([1,0],[0,1]):
      ni,nj=i+y,j+x
      if ni>=h or nj>=w:continue
      c=0
      if s[ni][nj]=="
      dp[ni][nj]=min(dp[ni][nj],dp[i][j]+c)

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

Solution (Python):
def check(i, j):
    if s[i][j] == '.':
        return 1
    else:
        return 0
H, W = map(int, input().split())
s = [input() for i in range(H)]
dp = [[0]*W for i in range(H)]
dp[0][0] = int(s[0][0] == '
for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            if i > 0 and j > 0:
                dp[i][j] = min(dp[i-1][j], dp[i][j-1])
            elif i > 0:
                dp[i][j] = dp[i-1][j]
            elif j > 0:
                dp[i][j] = dp[i][j-1]
        else:
            if i > 0 and j > 0:
                dp[i][j] = min(dp[i-1][j]+check(i-1, j), dp[i][j-1]+check(i, j-1))
            elif i > 0:
                dp[i][j] = dp[i-1][j]+check(i-1, j)
            elif j > 0:
                dp[i][j] = dp[i][j-1]+check(i, j-1)
print(dp[H-1][W-1])

Solution (Python):
H, W = map(int, input().split())
arr = [input().rstrip() for h in range(H)]
ans = [[0] * W for _ in range(H)]
for h, row in enumerate(arr):
    for w, val in enumerate(row):
        if h == 0 and w == 0:
            if arr[h][w] == "
                ans[h][w] = 1
            continue
        nv = 1e+9
        if h > 0:
            nv = ans[h-1][w]
            if arr[h-1][w] == "." and val == "
                nv += 1
        if w > 0:
            tmp = ans[h][w-1]
            if arr[h][w-1] == "." and val == "
                tmp += 1
            nv = min(nv, tmp)
        ans[h][w] = nv
print(ans[-1][-1])

Solution (Python):
h,w=map(int,input().split())
m=[[1 if c=="

n=[i[:] for i in m]
for i in range(1,h):
  n[i][0]=n[i-1][0]+int(m[i-1][0]-n[i][0]==-1)
for j in range(1,w):
  n[0][j]=n[0][j-1]+int(m[0][j-1]-n[0][j]==-1)
for i in range(1,h):
  for j in range(1,w):
    n[i][j]=min(n[i-1][j]+int(m[i-1][j]-n[i][j]==-1),n[i][j-1]+int(m[i][j-1]-n[i][j]==-1))
print(n[-1][-1])

Solution (Python):
import queue

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

cost = [[99999999 for j in range(w)] for i in range(h)]

cost[0][0] = 0
xq = queue.Queue()
yq = queue.Queue()
xq.put(0)
yq.put(0)

while xq.qsize() > 0:
    x = xq.get()
    y = yq.get()

    if y + 1 < h:
        tmp = cost[y][x] + (1 if s[y][x] == '.' and s[y + 1][x] == '
        if tmp < cost[y + 1][x]:
            cost[y + 1][x] = tmp
            xq.put(x)
            yq.put(y + 1)
    if x + 1 < w:
        tmp = cost[y][x] + (1 if s[y][x] == '.' and s[y][x + 1] == '
        if tmp < cost[y][x + 1]:
            cost[y][x + 1] = tmp
            xq.put(x + 1)
            yq.put(y)

print((0 if s[0][0] == '.' else 1) + cost[h - 1][w - 1])

Solution (Python):
h, w = map(int, input().split())
S = [input() for _ in range(h)]
dp = [[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    if i == 0 and j == 0:
      if S[0][0] == "
        dp[0][0] = 1
      continue
    if i:
      u = dp[i-1][j]
      if S[i-1][j] == "." and S[i][j] == "
        u += 1
    else:
      u = float("inf")
    if j:
      v = dp[i][j-1]
      if S[i][j-1] == "." and S[i][j] == "
        v += 1
    else:
      v = float("inf")
    dp[i][j] = min(u, v)
print(dp[-1][-1])

Solution (Python):
def solve():
    H, W = map(int, input().split())
    l = [list(input()) for _ in range(H)]
    q = [(0, 0)]
    sc = [[H*W]*W for _ in range(H)]
    sc[0][0] = 0 if l[0][0]=="." else 1

    while q:
        y, x = q.pop()
        for y1, x1 in [(y+1, x), (y, x+1)]:
            if not (0<=x1<W and 0<=y1<H):continue
            if l[y][x]=="
                if sc[y1][x1] > sc[y][x]:
                    sc[y1][x1] = sc[y][x]
                    q.append((y1, x1))
            else:
                v = sc[y][x]+1 if l[y1][x1]=="
                if sc[y1][x1] > v:
                    sc[y1][x1] = v
                    q.append((y1, x1))

    print(sc[-1][-1])


if __name__ == "__main__":
    solve()

Solution (Python):
def solve():
    H, W = map(int, input().split())
    l = [list(input()) for _ in range(H)]
    q = [(0, 0)]
    sc = [[H*W]*W for _ in range(H)]
    sc[0][0] = 0 if l[0][0]=="." else 1

    while q:
        y, x = q.pop()
        for y1, x1 in [(y+1, x), (y, x+1)]:
            if not (0<=x1<W and 0<=y1<H):continue
            if l[y][x]=="
                if sc[y1][x1] > sc[y][x]:
                    sc[y1][x1] = sc[y][x]
                    q.append((y1, x1))
            else:
                v = sc[y][x]+1 if l[y1][x1]=="
                if sc[y1][x1] > v:
                    sc[y1][x1] = v
                    q.append((y1, x1))

    print(sc[-1][-1])


if __name__ == "__main__":
    solve()

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

cnts = [[0 for _ in range(W)] for _ in range(H)]
if S[0][0] == "
    cnts[0][0]  = 1
for h in range(H):
    for w in range(W):
        if h == 0 and w == 0:
            continue

        c1, c2 = float('inf'), float('inf')
        if 0 <= h - 1:
            c1 = cnts[h - 1][w]
            if S[h][w] == "
                c1 += 1

        if 0 <= w - 1:
            c2 = cnts[h][w - 1]
            if S[h][w] == "
                c2 += 1

        cnts[h][w] = min(c1, c2)
print(cnts[H-1][W-1])

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    INF = 10**9
    H, W = input_nums()
    ss = [input() for _ in range(H)]
    dp = [[0]*W for _ in range(H)]
    if ss[0][0] == '
    for h in range(H):
        for w in range(W):
            if (h, w) == (0, 0): continue
            u = l = INF
            if h-1>=0:
                u = dp[h-1][w]
                if ss[h][w] == '
            if w-1>=0:
                l = dp[h][w-1]
                if ss[h][w] == '
            dp[h][w] = min(u, l)
    print(dp[H-1][W-1])

if __name__ == '__main__':
    main()

Solution (Python):
h,w=map(int,input().split())
G=[["
for i in range(1,h+1):
  G[i]=["
for i in range(h+2):
  for j in range(w+2):
    if G[i][j]==".":
      G[i][j]=0
    else:
      G[i][j]=1
dp=[[1000]*(w+2) for i in range(h+2)]
dp[1][1]=0
for i in range(1,h+1):
  for j in range(1,w+1):
    if i==1 and j==1:
      continue
    dp[i][j]=min(dp[i-1][j]+(G[i-1][j]+G[i][j])%2,dp[i][j-1]+(G[i][j-1]+G[i][j])%2)
ans=dp[h][w]
if G[1][1]==1:
  ans=ans+1
print((ans+1)//2)

Solution (Python):
inputs = [s.strip() for s in open(0).readlines()]
import numpy as np
class Grid():
    def __init__(self, grid, w=0, h=0, function=lambda x: x):
        self.w = w = w if w else len(grid[0])
        self.h = h = h if h else len(grid)
        dtype = type(function(grid[0][0]))
        self.grid = np.empty((h, w), dtype=dtype)
        for i, row in zip(range(h), grid):
            for j, val in zip(range(w), row):
                self.grid[i][j] = function(val)
    
    def is_valid_x(self, x):
        return 0 <= x < self.w
    def is_valid_y(self, y):
        return 0 <= y < self.h
    def is_valid_xy(self, x, y):
        return self.is_valid_x(x) and self.is_valid_y(y) 
    
    def __iter__(self):
        return iter(self.grid)
    def __repr__(self):
        return '\n'.join([' '.join(map(str, row)) for row in self.grid])
    def __getitem__(self, x):
        return self.grid[x]
    def __setitem__(self, x, val):
        self.grid[x] = val

h, w = map(int, inputs[0].split())
grid = Grid(inputs[1:]).grid

h, w = map(int, inputs[0].split())
grid = Grid(inputs[1:])

dp = Grid(np.zeros((h, w), np.int))
dp[0, 0] = int(grid[0, 0] == '
for i in range(1, w):
    dp[0, i] = dp[0, i-1] + int(grid[0, i-1] + grid[0, i] == '.
for i in range(1, h):
    dp[i, 0] = dp[i-1, 0] + int(grid[i-1, 0] + grid[i, 0] == '.
x = y = 1
while not (x == w-1 and y == h-1):
    for i in range(x, w):
        dp[y, i] = min(dp[y, i-1] + int(grid[y, i-1] + grid[y, i] == '.
                       dp[y-1, i] + int(grid[y-1, i] + grid[y, i] == '.
    for i in range(y+1, h):
        dp[i, x] = min(dp[i-1, x] + int(grid[i-1, x] + grid[i, x] == '.
                       dp[i, x-1] + int(grid[i, x-1] + grid[i, x] == '.
    x, y = min(x+1, w-1), min(y+1, h-1)
print(min(dp[y-1, x] + int(grid[y-1, x] + grid[y, x] == '.
          dp[y, x-1] + int(grid[y, x-1] + grid[y, x] == '.