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: Wrong Answer

Solution code:
from collections import deque

def BWS(q,s,x,y,xx,yy,state,reach):
    if 0 <= xx <= h-1 and 0 <= yy <= w-1:
        d = 0
        if s[xx][yy] == "#" and state == 1:
            d += 1
        if reach[xx][yy] == -1 or reach[xx][yy] > reach[x][y] + d:
            p = 0
            if s[xx][yy] == "#":
                p = 1
            reach[xx][yy] = reach[x][y] + d
            q.append([xx,yy,[1,-1][p]])
    return q,reach

h,w = map(int,input().split())
s = [0]*h
for i in range(h):
    s[i] = list(input())
q = deque([])
reach = [[-1 for j in range(w)] for i in range(h)]
reach[0][0] = 0
r = 0
if s[0][0] == "#":
    r += 1
    q.append([0,0,-1])
else:
    q.append([0,0,1])
while q: #state == 1→"."  -1→"#"
    [x,y,state] = q.popleft()
    q,reach = BWS(q,s,x,y,x+1,y,state,reach)
    q,reach = BWS(q,s,x,y,x-1,y,state,reach)
    q,reach = BWS(q,s,x,y,x,y+1,state,reach)
    q,reach = BWS(q,s,x,y,x,y-1,state,reach)
print(reach[-1][-1] + r)

Submission status: Wrong Answer

Solution code:
from collections import deque

def BWS(q,s,x,y,xx,yy,state,reach):
    if 0 <= xx <= h-1 and 0 <= yy <= w-1:
        d = 0
        if s[xx][yy] == "." and state == -1:
            d += 1
        if s[xx][yy] == "#" and state == 1:
            d += 1
        if reach[xx][yy] == -1 or reach[xx][yy] > reach[x][y] + d:
            p = 0
            if s[xx][yy] == "#":
                p = 1
            reach[xx][yy] = reach[x][y] + d
            q.append([xx,yy,[1,-1][p]])
    return q,reach

h,w = map(int,input().split())
s = [0]*h
for i in range(h):
    s[i] = list(input())
q = deque([])
reach = [[-1 for j in range(w)] for i in range(h)]
reach[0][0] = 0
r = 0
if reach[-1][-1] == "#":
    r += 1
if s[0][0] == "#":
    r += 1
    q.append([0,0,-1])
else:
    q.append([0,0,1])
while q: #state == 1→"."  -1→"#"
    [x,y,state] = q.popleft()
    q,reach = BWS(q,s,x,y,x+1,y,state,reach)
    q,reach = BWS(q,s,x,y,x-1,y,state,reach)
    q,reach = BWS(q,s,x,y,x,y+1,state,reach)
    q,reach = BWS(q,s,x,y,x,y-1,state,reach)
print(-(-reach[-1][-1]//2) + r)

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
grid = [list(input()) for _ in range(h)]
dpgrid = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        d = 1 if grid[i][j] == "#" else 0
        if (i == 0) & (j == 0):
            dpgrid[i][j] = d
        elif (i == 0):
            dpgrid[i][j] = d + dpgrid[i][j-1]
        elif (j == 0):
            dpgrid[i][j] = d + dpgrid[i-1][j]
        else:
            dpgrid[i][j] = d + min(dpgrid[i-1][j], dpgrid[i][j-1])
print(dpgrid[h-1][w-1])

Submission status: Wrong Answer

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

dp = [[10**9 for _ in range(W)] for __ in range(H)]

#solve
def dfs(i, j, score, lc):
    if i < H and i >= 0 and j < W and j >= 0:
        if lc == '.' and g[i][j] == '#':
            score += 1
        dp[i][j] = min(dp[i][j], score)

        dfs(i+1, j, score, g[i][j])
        dfs(i, j+1, score, g[i][j])        
    
dfs(0, 0, 0, '.')
print(dp)
print(dp[H-1][W-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import *

def bfs():
    q = deque([(0, 0)])
    dist = [[-1]*W for _ in range(H)]
    
    if s[0][0]=='#':
        dist[0][0] = 1
    else:
        dist[0][0] = 0
    
    while q:
        cx, cy = q.popleft()
        
        for nx, ny in [(cx+1, cy), (cx, cy+1)]:
            if 0<=nx<H and 0<=ny<W and dist[nx][ny]==-1:
                if s[cx][cy]=='.' and s[nx][ny]=='#':
                    dist[nx][ny] = dist[cx][cy]+1
                    q.append((nx, ny))
                else:
                    dist[nx][ny] = dist[cx][cy]
                    q.appendleft((nx, ny))
        
    return dist[H-1][W-1]

H, W = map(int, input().split())
s = [input()[:-1] for _ in range(H)]
print(bfs())

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import *

def bfs():
    q = deque([(0, 0)])
    dist = [[-1]*W for _ in range(H)]
    
    if s[0][0]=='#':
        dist[0][0] = 1
    else:
        dist[0][0] = 0
    
    while q:
        cx, cy = q.popleft()
        
        for nx, ny in [(cx-1, cy), (cx+1, cy), (cx, cy-1), (cx, cy+1)]:
            if 0<=nx<H and 0<=ny<W and dist[nx][ny]==-1:
                if s[cx][cy]=='.' and s[nx][ny]=='#':
                    dist[nx][ny] = dist[cx][cy]+1
                    q.append((nx, ny))
                else:
                    dist[nx][ny] = dist[cx][cy]
                    q.appendleft((nx, ny))
        
    return dist[H-1][W-1]

H, W = map(int, input().split())
s = [input()[:-1] for _ in range(H)]
print(bfs())

Submission status: Wrong Answer

Solution code:
H,W=list(map(int,input().split()))
data=[list(input()) for i in range(H)]
l=[[float("Inf")]*W for i in range(H)]
if data[0][0] =="#":
   l[0][0]=1
else:
   l[0][0]=0
for i in range(H):
   if i>0:
      if data[i][0] == "#":
         l[i][0]=l[i-1][0]+1
      else:
          l[i][0]=l[i-1][0]
   for j in range(1,W):
      if data[i][j] == ".":
         l[i][j]=min(l[i][j-1],l[i-1][j])
      else:
         l[i][j]=min(l[i][j-1],l[i-1][j])+1
print(l[H-1][W-1])

Submission status: Wrong Answer

Solution code:
INF = 10**9
def solve(h, w, s):
    dp = [[INF] * w for r in range(h)]
    dp[0][0] = int(s[0][0] == "#")
    for r in range(h):
        for c in range(w): 
            for dr, dc in [(-1, 0), (0, -1)]:
                nr, nc = r+dr, c+dc
                if (0 <= nr < h) and (0 <= nc < w):
                    dp[r][c] = min(dp[r][c], dp[nr][nc] + (s[r][c] != s[nr][nc]))
    return dp[h-1][w-1]

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

Submission status: Wrong Answer

Solution code:
INF = 10**9
def solve(h, w, s):
    dp = [[INF] * w for r in range(h)]
    dp[0][0] = int(s[0][0] == "#")
    for r in range(h):
        for c in range(w):
            for dr, dc in [(-1, 0), (0, -1)]:
                nr, nc = r+dr, c+dc
                if (0 <= nr < h) and (0 <= nc < w):
                    dp[r][c] = min(dp[r][c], dp[nr][nc] + (s[r][c] == "#"))
    return dp[h-1][w-1]

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

Submission status: Wrong Answer

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

inf = 10**9 + 7
dp = [[inf] * W for _ in range(H)]
if (L[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):
            continue
        a = dp[i][j - 1]
        b = dp[i - 1][j]
        if (L[i][j] == '#'):
            a += 1
            b += 1
        dp[i][j] = min(dp[i][j], a, b)

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

Submission status: Wrong Answer

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

inf = 10**9 + 7
dp = [[inf] * W for _ in range(H)]
if (L[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):
            continue
        if (i - 1 < 0):
            b = inf
        else:
            b = dp[i - 1][j]
        if (j - 1 < 0):
            a = inf
        else:
            a = dp[i][j - 1]

        if (L[i][j] == '#'):
            a += 1
            b += 1
        dp[i][j] = min(dp[i][j], a, b)

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

Submission status: Wrong Answer

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

nowblack=False
dp=[[float("INF")]*W for _ in range(H)]
if S[0][0]=="#":
    dp[0][0]=1
else:
    dp[0][0]=0
for h in range(H):
    for w in range(W):
        if 0<=h-1<H:
            if S[h][w]==".":
                dp[h][w]=min(dp[h][w], dp[h-1][w])
            else:
                if dp[h-1][w]=="#":
                    dp[h][w]=min(dp[h][w], dp[h-1][w])
                else:
                    dp[h][w]=min(dp[h][w], dp[h-1][w]+1)


        if 0<=w-1<W:
            if S[h][w]==".":
                dp[h][w]=min(dp[h][w], dp[h][w-1])
            else:
                if dp[h][w-1]=="#":
                    dp[h][w]=min(dp[h][w], dp[h][w-1])
                else:
                    dp[h][w]=min(dp[h][w], dp[h][w-1]+1)

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from queue import Queue

def II(): return int(input())
def MII(): return map(int, input().split())
def LII(): return list(map(int, input().split()))

dx = [0, 1]
dy = [1, 0]

def main():
    H, W = MII()
    S = [list(input()) for _ in range(H)]
    C = [[0 for _ in range(W)] for _ in range(H)]

    for h in range(H):
        for w in range(W):
            t = 1 if S[h][w] == '#' else 0
            if 0 < h and 0 < w:
                C[h][w] = min(C[h-1][w], C[h][w-1]) + t
            elif w == 0:
                C[h][w] = C[h-1][w] + t
            elif h == 0:
                C[h][w] = C[h][w-1] + t
            

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


inf = 10 ** 18
mod = 10 ** 9 + 7

h,w = lr()
s = [input() for i in range(h)]
dis = [[0 for i in range(w+1)]for j in range(h+1)] # 障害物に当たる最小数
for i in range(2,w+1):
    dis[0][i] = inf
for i in range(2,h+1):
    dis[i][0] = inf
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            dis[i+1][j+1] = min(dis[i][j+1], dis[i+1][j])
        else:
            dis[i+1][j+1] = min(dis[i][j+1]+1, dis[i+1][j]+1)
print(dis[h][w])

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
s=[]
dp=[[10**9]*W for _ in range(H)]
dp[0][0]=0
for _ in range(H):
    s.append(list(input()))
q=[(0,0)]
for i in range(H):
    for j in range(W):
        if j!=W-1:
            if s[i][j]=="." and s[i][j+1]=="#":
                dp[i][j+1]=min(dp[i][j]+1,dp[i][j+1])
            else:
                dp[i][j+1]=min(dp[i][j],dp[i][j+1])
        if i!=H-1:
            if s[i][j]=="." and s[i+1][j]=="#":
                dp[i+1][j]=min(dp[i][j]+1,dp[i+1][j])
            else:
                dp[i+1][j]=min(dp[i][j],dp[i+1][j])
ans=dp[H-1][W-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
h, w = map(int,input().split())
dp = np.full((h, w), 10**9)
for i in range(h):
	target = input()
	if i == 0:
		for j in range(w):
			if j == 0:
				if target[j] == "#":
					dp[i][j] = 1
				else:
					dp[i][j] = 0
			else:
				if target[j] == "#":
					dp[i][j] = dp[i][j-1] + 1
				else:
					dp[i][j] = dp[i][j-1]
	else:
		for j in range(w):
			if j == 0:
				if target[j] == "#":
					dp[i][j] = dp[i-1][j] + 1
				else:
					dp[i][j] = dp[i-1][j]
			else:
				if target[j] == "#":
					dp[i][j] = min(dp[i][j-1]+1, dp[i-1][j]+1)
				else:
					dp[i][j] = min(dp[i][j-1], dp[i-1][j])
print(min(dp[h-1][w-1], h+w-dp[h-1][w-1]))

Submission status: Wrong Answer

Solution code:
import numpy as np
h, w = map(int,input().split())
dp = np.full((h, w), 10**9)
for i in range(h):
	target = input()
	if i == 0:
		for j in range(w):
			if j == 0:
				if target[j] == "#":
					dp[i][j] = 1
				else:
					dp[i][j] = 0
			else:
				if target[j] == "#":
					dp[i][j] = dp[i][j-1] + 1
				else:
					dp[i][j] = dp[i][j-1]
	else:
		for j in range(w):
			if j == 0:
				if target[j] == "#":
					dp[i][j] = dp[i-1][j] + 1
				else:
					dp[i][j] = dp[i-1][j]
			else:
				if target[j] == "#":
					dp[i][j] = min(dp[i][j-1]+1, dp[i-1][j]+1)
				else:
					dp[i][j] = min(dp[i][j-1], dp[i-1][j])
print(dp[h-1][w-1])

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())

L=[]
l=["@" for i in range(W+2)]
L.append(l)
for i in range(H):
    s=["@"]+list(input())+["@"]
    L.append(s)
L.append(l)
#print(L)


import copy
l=copy.deepcopy(L)

import heapq
Q=[]
X=[1,0]
Y=[0,1]
L[1][1]=0
for xy in range(2):
    if l[1+X[xy]][1+Y[xy]]==l[1][1]:
        Q.append([0,1+X[xy],1+Y[xy]])
    elif l[1+X[xy]][1+Y[xy]]!=l[1][1]:
        if l[1+X[xy]][1+Y[xy]]=="#":
            Q.append([1,1+X[xy],1+Y[xy]])
        else:
            Q.append([0,1+X[xy],1+Y[xy]])
heapq.heapify(Q)

for i in range(10**6):
    if len(Q)==0:
        break
    else:
        q=heapq.heappop(Q)
        if L[q[1]][q[2]]=="." or L[q[1]][q[2]]=="#":
            L[q[1]][q[2]]=q[0]
            for xy in range(2):
                if L[q[1]+X[xy]][q[2]+Y[xy]]==l[q[1]][q[2]]:
                    Q.append([q[0],q[1]+X[xy],q[2]+Y[xy]])
                elif L[q[1]+X[xy]][q[2]+Y[xy]]!=l[q[1]][q[2]]:
                    if L[q[1]+X[xy]][q[2]+Y[xy]]=="#":
                        Q.append([q[0]+1,q[1]+X[xy],q[2]+Y[xy]])
                    else:
                        Q.append([q[0],q[1]+X[xy],q[2]+Y[xy]])
ans=0
if l[1][1]=="#":
    ans=1
print(ans+L[H][W])
#print(L)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())

L=[]
l=["@" for i in range(W+2)]
L.append(l)
for i in range(H):
    s=["@"]+list(input())+["@"]
    L.append(s)
L.append(l)
#print(L)
ans=0
if L[1][1]=="#":
    ans+=1
    L[1][1]="."
if L[H][W]=="#":
    ans+=1
    L[H][W]="."

import heapq
Q=[]
X=[1,0]
Y=[0,1]
L[1][1]=0
for xy in range(2):
    if L[1+X[xy]][1+Y[xy]]==".":
        Q.append([0,1+X[xy],1+Y[xy]])
    elif L[1+X[xy]][1+Y[xy]]=="#":
        Q.append([1,1+X[xy],1+Y[xy]])
heapq.heapify(Q)

for i in range(10**6):
    if len(Q)==0:
        break
    else:
        q=heapq.heappop(Q)
        if L[q[1]][q[2]]=="." or L[q[1]][q[2]]=="#":
            L[q[1]][q[2]]=q[0]
            for xy in range(2):
                if L[q[1]+X[xy]][q[2]+Y[xy]]==".":
                    Q.append([q[0],q[1]+X[xy],q[2]+Y[xy]])
                elif L[q[1]+X[xy]][q[2]+Y[xy]]=="#":
                    Q.append([q[0]+1,q[1]+X[xy],q[2]+Y[xy]])
print(ans+L[H][W])

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())

L=[]
l=["@" for i in range(W+2)]
L.append(l)
for i in range(H):
    s=["@"]+list(input())+["@"]
    L.append(s)
L.append(l)
#print(L)
ans=0
if L[1][1]=="#":
    ans+=1
    L[1][1]="."
if L[H][W]=="#":
    ans+=1
    L[H][W]="."

import heapq
Q=[]
X=[1,-1,0,0]
Y=[0,0,1,-1]
L[1][1]=0
for xy in range(4):
    if L[1+X[xy]][1+Y[xy]]==".":
        Q.append([0,1+X[xy],1+Y[xy]])
    elif L[1+X[xy]][1+Y[xy]]=="#":
        Q.append([1,1+X[xy],1+Y[xy]])
heapq.heapify(Q)

for i in range(10**6):
    if len(Q)==0:
        break
    else:
        q=heapq.heappop(Q)
        if L[q[1]][q[2]]=="." or L[q[1]][q[2]]=="#":
            L[q[1]][q[2]]=q[0]
            for xy in range(4):
                if L[q[1]+X[xy]][q[2]+Y[xy]]==".":
                    Q.append([q[0],q[1]+X[xy],q[2]+Y[xy]])
                elif L[q[1]+X[xy]][q[2]+Y[xy]]=="#":
                    Q.append([q[0]+1,q[1]+X[xy],q[2]+Y[xy]])
print(ans+L[H][W])

Submission status: Wrong Answer

Solution code:
def solve(S,H,W):
    INF = H + W
    DP = [[INF for _ in range(W)] for _ in range(W)]
    if S[0][0] == "#":
        DP[0][0] = 1
    else:
        DP[0][0] = 0

    for w in range(1,W):
        if S[0][w] == "#":
            DP[0][w] = DP[0][w-1] + 1
        else:
            DP[0][w] = DP[0][w-1]

    for h in range(1,H):
        if S[h][0] == "#":
            DP[h][0] = DP[h-1][0] + 1
        else:
            DP[h][0] = DP[h-1][0]

    for h in range(1,H):
        for w in range(1,W):
            if S[h][w] == "#":
                DP[h][w] = min(DP[h-1][w],DP[h][w-1]) + 1
            else:
                DP[h][w] = min(DP[h-1][w],DP[h][w-1])

    return DP[H-1][W-1]

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

    print(solve(S,H,W))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

H,W = map(int, input().split())
s = [None for _ in range(H)]
for i in range(H):
    s[i] = input()

dp = [[0]*(W+1) for _ in range(H+1)]
# if s[0][0] == '#':
#     dp[1][1] += 1
for i in range(H):
    for j in range(W):
        tmp = 0
        if s[i][j] == '#':
            tmp += 1
        if i == 0:
            dp[i+1][j+1] = dp[i+1][j]+tmp
        elif j == 0:
            dp[i+1][j+1] = dp[i][j+1]+tmp
        else:
            dp[i+1][j+1] = min(dp[i][j+1],dp[i+1][j])+tmp
print(dp[H][W])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

H,W = map(int, input().split())
s = [None for _ in range(H)]
for i in range(H):
    s[i] = input()

dp = [[0]*(W+1) for _ in range(H+1)]
if s[0][0] == '#':
    dp[1][1] += 1
for i in range(H):
    for j in range(W):
        tmp = 0
        if s[i][j] == '#':
            tmp += 1
        if i == 0:
            dp[i+1][j+1] = dp[i+1][j]+tmp
        elif j == 0:
            dp[i+1][j+1] = dp[i][j+1]+tmp
        else:
            dp[i+1][j+1] = min(dp[i][j+1],dp[i+1][j])+tmp
print(dp[H][W])

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S = [list(input()) for h in range(H)]
table = [[0 for w in range(W)] for h in range(H)]

table[0][0] = int(S[0][0]=='#')
for i in range(1, H):
    table[i][0] = table[i-1][0] + int(S[i][0]=='#')
for j in range(1, W):
    table[0][j] = table[0][j-1] + int(S[0][j]=='#')
for i in range(1, H):
    for j in range(1, W):
        table[i][j] = min(table[i-1][j], table[i][j-1]) + int(S[i][j]=='#')

print(table[-1][-1])

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w = map(int,input().split())
grid = []
dx = [0,1]
dy = [1,0]
for _ in range(h):
    grid.append(input())
d = [[float("inf") for _ in range(w)] for _ in range(h)]
if grid[0][0] == ".":
    d[0][0] = 0
else:
    d[0][0] = 1
dq = deque([(0,0)])
while dq:
    x,y = dq.popleft()
    for i in range(2):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx <= w-1 and 0 <= ny <= h-1:
            if grid[ny][nx] == "#":
                if d[ny][nx] > d[y][x]+1:
                    d[ny][nx] = d[y][x]+1
                    dq.append((nx,ny))
            else:
                if d[ny][nx] > d[y][x]:
                    d[ny][nx] = d[y][x]
                    dq.append((nx,ny))
print(d[h-1][w-1])

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
dp = [[float("inf")] * w for i in range(h)]
dp[0][0] = 1 if s[0][0] == "#" else 0
for i in range(h):
    for j in range(w):
        if i > 0 and j > 0:
            m = min(dp[i - 1][j], dp[i][j - 1])
        elif i > 0:
            m = dp[i - 1][j]
        elif j > 0:
            m = dp[i][j - 1]
        else:
            m = 0
        dp[i][j] = m + 1 if s[i][j] == "#" else m
print(dp[h - 1][w - 1])

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
dp = [[float("inf")] * w for i in range(h)]
for i in range(h):
    for j in range(w):
        if i > 0 and j > 0:
            m = min(dp[i - 1][j], dp[i][j - 1])
        elif i > 0:
            m = dp[i - 1][j]
        elif j > 0:
            m = dp[i][j - 1]
        else:
            m = 0
        dp[i][j] = m + 1 if s[i][j] == "#" else m
print(dp[h - 1][w - 1])

Submission status: Wrong Answer

Solution code:
import copy
import heapq
H, W = map(int, input().split())
S = []
cnt_list = []
for _ in range(H):
    S.append(list(input()))

def move(i,j,cnt,frag):
    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:
        print(c_cnt)
        cnt_list.append(c_cnt)

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

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

print(cnt_list)
heapq.heapify(cnt_list)
print((heapq.heappop(cnt_list))//2 + a)

Submission status: Wrong Answer

Solution code:
[h,w] = [int(i) for i in input().split()]
s = []
for i in range(h):
    s.append(input())
"""
dp[i][j] = そこに来るまでの#の最小数
"""
dp = [[1000]*w for i in range(h)]
if s[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0
tmp = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            tmp = 1
        if i != 0 and j != 0:
            dp[i][j] = min(dp[i][j],dp[i-1][j]+tmp)
            dp[i][j] = min(dp[i][j],dp[i][j-1]+tmp)
        elif i != 0:
            dp[i][j] = min(dp[i][j],dp[i-1][j]+tmp)
        else:
            dp[i][j] = min(dp[i][j],dp[i][j-1]+tmp)
        tmp = 0
print(dp[h-1][w-1])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[]
for _ in range(h):
    s.append(list(input()))
a=[[0]]
for i in range(1,h):
    t=a[i-1][0]
    if s[i][0]!=s[i-1][0]:
        a.append([t+1])
    else:
        a.append([t])

for j in range(1,w):
    t=a[0][j-1]
    if s[0][j]!=s[0][j-1]:
        a[0].append(t+1)
    else:
        a[0].append(t)

for i in range(1,h):
    for j in range(1,w):
        up=a[i-1][j]
        left=a[i][j-1]
        if s[i][j]!=s[i-1][j]:
            up += 1
        if s[i][j]!=s[i][j-1]:
            left += 1
        mn=min(up,left)
        a[i].append(mn)
ans=a[h-1][w-1]
ans=(ans+1)//2
if s[0][0]=='#':
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
L = [input() for i in range(h)]
dp = [[10**9]*w for i in range(h)]
if L[0][0] == ".":
    dp[0][0] = 0
else:
    dp[0][0] = 1
for i in range(h-1):
    for j in range(w-1):
        if L[i][j] == "." and L[i+1][j] == "#":
            dp[i+1][j] = min(dp[i][j] +1,dp[i+1][j])
            #print(dp,i,j)
        else:
            dp[i+1][j] = min(dp[i][j] ,dp[i+1][j])
            
        if L[i][j] == "." and L[i][j+1] == "#":
            dp[i][j+1] = min(dp[i][j] +1,dp[i][j+1])
        else:
            dp[i][j+1] = min(dp[i][j],dp[i][j+1])
        
if L[h-1][w-1] == "#":
    dp[h-1][w-1] = min(dp[h-2][w-1] +1,dp[h-2][w-1] +1)
else:
    dp[h-1][w-1] = min(dp[h-2][w-1],dp[h-2][w-1])

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

Submission status: Wrong Answer

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

count = [[100000] * W for _ in range(W)]
count[0][0] = 0 if S[0][0] == "." else 1


for i in range(H):
    for j in range(W):
        if i + j == 0:
            continue
        X, Y = 10**6, 10**6
        if i > 0:
            if S[i][j] == "#":
                X = count[i-1][j] + 1
            else:
                X = count[i-1][j]
        if j > 0:
            if S[i][j] == "#":
                Y = count[i][j-1] + 1
            else:
                Y = count[i][j-1]
        count[i][j] = min(X, Y)

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log

mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

h,w=mii()

mat=[list(ss()) for _ in range(h)]

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

for i in range(h):
    for j in range(w):
        if i==0:
            if j==0:
                if mat[i][j]=="#":
                    chk[i][j]+=1
            else:
                if mat[i][j]!=mat[i][j-1]:
                    chk[i][j]=chk[i][j-1]+1
                else:
                    chk[i][j]=chk[i][j-1]
        
        else:
            if j==0:
                if mat[i][j]!=mat[i-1][j]:
                    chk[i][j]=chk[i-1][j]+1
                else:
                    chk[i][j]=chk[i-1][j]
            
            else:
                if mat[i][j]==mat[i-1][j] and mat[i][j]==mat[i][j-1]:
                    chk[i][j]=min(chk[i-1][j],chk[i][j-1])
                    
                elif mat[i][j]!=mat[i-1][j]:
                    chk[i][j]=min(chk[i-1][j]+1,chk[i][j-1])
                else:
                    chk[i][j]=min(chk[i-1][j],1+chk[i][j-1])
print(chk[-1][-1])
#print(chk)

Submission status: Wrong Answer

Solution code:
def rewrite(S,H,W):
    INF = H+W
    D = [[INF for _ in range(W)] for _ in range(H)]
    D[0][0] = 0
    if S[0][0] == "#":
        D[0][0] += 1

    for h in range(1,H):
        D[h][0] = D[h-1][0]
        if S[h][0] == "#":
            D[h][0] += 1

    for w in range(1,W):
        D[0][w] = D[0][w-1]
        if S[0][w] == "#":
            D[0][w] += 1

    for h in range(1,H):
        for w in range(1,W):
            D[h][w] = min(D[h-1][w],D[h][w-1])
            if S[h][w] == "#":
                D[h][w] += 1

    return D[H-1][W-1]

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

    print(rewrite(S,H,W))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))
m = [list(input()) for _ in range(H)]
scores = [[None for _ in range(W)] for _ in range(H)]
def calc(i, j):
#     print(f'{i}, {j}')
    global m, scores, H, W
    if scores[i][j] is not None:
        return scores[i][j]
    if i == H - 1 and j == W - 1:
        score = 1 if m[i][j] == '#' else 0
        scores[i][j] = score
        return score
    diff = []
    if j < W - 1:
        diff.append((0, 1))
    if i < H - 1:
        diff.append((1, 0))
#     print(f'diff: {diff}')
    score_cand = []
    for di, dj in diff:
        additional = 0
        if (m[i][j] == '.' and m[i+di][j+dj] == '#') or (i == 0 and j == 0 and m[0][0] == '#'):
            additional += 1 
        score = calc(i+di, j+dj) + additional
        score_cand.append(score)
    score = min(score_cand)
    scores[i][j] = score
    return score
print(calc(0, 0))

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))
m = [list(input()) for _ in range(H)]
scores = [[None for _ in range(W)] for _ in range(H)]
def calc(i, j):
#     print(f'{i}, {j}')
    global m, scores, H, W
    if i == H - 1 and j == W - 1:
        score = 1 if m[i][j] == '#' else 0
        scores[i][j] = score
        return score
    if scores[i][j] is not None:
        return scores[i][j]
    diff = []
    if j < W - 1:
        diff.append((0, 1))
    if i < H - 1:
        diff.append((1, 0))
#     print(f'diff: {diff}')
    score_cand = []
    for di, dj in diff:
        additional = 1 if m[i][j] == '.' and m[i+di][j+dj] == '#' else 0
        score = calc(i+di, j+dj) + additional
        score_cand.append(score)
    score = min(score_cand)
    scores[i][j] = score
    return score
print(calc(0, 0))

Submission status: Wrong Answer

Solution code:
def calc(i, j):
#     print(f'{i}, {j}')
    global m, scores, H, W
    if i == H - 1 and j == W - 1:
        score = 1 if m[i][j] == '#' else 0
        scores[i][j] = score
        return score
    if scores[i][j] is not None:
        return scores[i][j]
    diff = []
    if j < W - 1:
        diff.append((0, 1))
    if i < H - 1:
        diff.append((1, 0))
#     print(f'diff: {diff}')
    score_cand = []
    for di, dj in diff:
        additional = 1 if m[i][j] == '.' and m[i+di][j+dj] == '#' else 0
        score = calc(i+di, j+dj) + additional
        score_cand.append(score)
    score = min(score_cand)
    scores[i][j] = score
    return score

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())

tmp = [input() for _ in range(H)]
S = []
for i in tmp:
    k = []
    for j in i:
        if j == '#':
            k.append(1)
        else:
            k.append(0)
    S.append(k)

dp = [[1000 for _ in range(W)] for __ in range(H)]

dp[0][0] = S[0][0]
for i in range(H):
    for j in range(W):
        if j+1 < W:
            dp[i][j+1] = min(dp[i][j+1], dp[i][j]+S[i][j+1])
        w = 2
        while j+w < W and S[i][j+w] and S[i][j+1]:
            dp[i][j+w] = min(dp[i][j+w], dp[i][j]+1)
            w += 1
        if i+1 < H:
            dp[i+1][j] = min(dp[i+1][j], dp[i][j]+S[i+1][j])
        h = 2
        while i+h < H and S[i+h][j] and S[i+1][j]:
            dp[i+h][j] = min(dp[i+h][j], dp[i][j]+1)
            h += 1

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