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:
h,w= map(int,input().split(' '))

grid= [[0 for i in range(w)] for j in range(h)]

for i in range(h):
    row=input()
    for j in range(len(row)):
        if row[j]=='#':
            grid[i][j]=1

btdt= [[-1 for i in range(w)] for j in range(h)]
def go(i,j,count,path):
    global h,w,grid
    if i>=h or j>=w:
        return
    if grid[i][j] == 1:
        count += 1
    if btdt[i][j] != -1 and count >= btdt[i][j]:
        return
    if i==h-1 and j==w-1:
        btdt[i][j]=count
        return
    btdt[i][j] = count
    go(i + 1, j, count,path+"D")
    go(i, j + 1, count,path+"R")

go(0,0,0,"")
print(btdt[i][j])

Submission status: Wrong Answer

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


def F(h, w, f):
    if f == -1:
        if S[h][w] == ".":
            return False
        else:
            return True
    if S[h][w] == "#":
        return False
    else:
        return True


if S[0][0] == "#":
    dp[0][0][0] = 1
    dp[0][0][1] = 0
else:
    dp[0][0][0] = 0
    dp[0][0][1] = 1

for i in range(H):
    for j in range(W):
        if j != 0:
            if S[i][j] == S[i][j - 1]:
                dp[i][j][0] = min(dp[i][j][0], dp[i][j - 1][0])
                dp[i][j][1] = min(dp[i][j][1], dp[i][j - 1][1])
            else:
                dp[i][j][0] = min(dp[i][j][0], dp[i][j - 1][1] + 1)
                dp[i][j][1] = min(dp[i][j][1], dp[i][j - 1][0] + 1)
        if i != 0:
            if S[i][j] == S[i - 1][j]:
                dp[i][j][0] = min(dp[i][j][0], dp[i - 1][j][0])
                dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][1])
            else:
                dp[i][j][0] = min(dp[i][j][0], dp[i - 1][j][1] + 1)
                dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][0] + 1)

print((min(dp[H - 1][W - 1]) + 1) // 2)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

def bfs(maze,visited,gx,gy):
    stack = deque([[0,0]])
    if maze[0][0] == '#':
        visited[0][0] = 1 
    else:
        visited[0][0] = 0 
    while stack:
        print(stack)
        print(visited)
        y,x = stack.pop()
        count = visited[y][x]
        
        for j,k in ([1,0],[0,1]):
            new_y,new_x = y+j,x+k
            if  new_x >= gx or  new_y >= gy:
                continue
            elif maze[new_y][new_x] == "#" and maze[y][x] == '#':
                visited[new_y][new_x] = min(count,visited[new_y][new_x])
                stack.appendleft([new_y, new_x])
            elif maze[new_y][new_x] == "#" and maze[y][x] == '.':
                visited[new_y][new_x] = min(count+1,visited[new_y][new_x])
                stack.appendleft([new_y, new_x])
            else:
                visited[new_y][new_x] = min(count,visited[new_y][new_x])
                stack.appendleft([new_y, new_x])
    return visited[gy-1][gx-1]

inf = float('inf')
visited = [[inf]*W for _ in range(H)]
print(bfs(maze,visited,W,H))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h,w=map(int,input().split())
infi=10**10
D=[list(input()) for i in range(h)]
kotae=[[infi]*(w) for _ in range(h)]
# D[i][j]はD[h-1][w-1]まで

cnt=0
for i in range(h):
    if D[i][0]=='#':
        cnt+=1
    kotae[i][0]=cnt
cnt=0
for j in range(w):
    if D[0][j]=='#':
        cnt+=1
    kotae[0][j]=cnt
for i in range(h-1):
    for j in range(w-1):
        kotae[i+1][j+1]=min(kotae[i][j+1], kotae[i+1][j])
        if D[i+1][j+1]=='#':
            kotae[i+1][j+1]+=1
print(kotae[h-1][w-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    ans = 0
    if S[0][0] == '#':
        ans += 1

    q = collections.deque()
    q.append([[0, 0], 0])

    d = [[10 ** 9] * W for j in range(H)]

    while q:
        c = q.pop()
        dist = c[1]

        j = c[0][0]
        i = c[0][1]

        if d[j][i] <= dist:
            continue
        else:
            d[j][i] = dist

        if i != W - 1:
            if S[j][i + 1] == '#':
                q.append([[j, i + 1], dist + 1])
            else:
                q.append([[j, i + 1], dist])

        if j != H - 1:
            if S[j + 1][i] == '#':
                q.append([[j + 1, i], dist + 1])
            else:
                q.append([[j + 1, i], dist])

    ans += d[H - 1][W - 1]

    ans2 = 0
    if S[0][0] == '.':
        ans2 += 1

    q2 = collections.deque()
    q2.append([[0, 0], 0])

    d2 = [[10 ** 9] * W for j in range(H)]

    while q2:
        c = q2.pop()
        dist = c[1]

        j = c[0][0]
        i = c[0][1]

        if d2[j][i] <= dist:
            continue
        else:
            d2[j][i] = dist

        if i != W - 1:
            if S[j][i + 1] == '.':
                q2.append([[j, i + 1], dist + 1])
            else:
                q2.append([[j, i + 1], dist])

        if j != H - 1:
            if S[j + 1][i] == '.':
                q2.append([[j + 1, i], dist + 1])
            else:
                q2.append([[j + 1, i], dist])

    ans2 += d2[H - 1][W - 1]
    print(min(ans, ans2 + 1))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = [int(i) for i in input().split(' ')]

queue = []
for i in range(0,n[0]):
  array = list(input())
  queue.append(array)

count = 0
h = 0
w = 0
path = n[0] + n[1] - 2
path_c = 0
while path_c < path:
    if h < n[0]-1 and queue[h+1][w] == '.':
        h += 1
    elif w < n[1]-1 and queue[h][w+1] == '.':
        w += 1
    elif h < n[0]-1 and queue[h+1][w] == '#':
        h += 1
        count += 1
    elif w < n[1]-1 and queue[h][w+1] == '#':
        w += 1
        count += 1
    path_c += 1

print(count)

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w = map(int, input().split())
c = [list(input()) for _ in range(h)]
if c[h-1][w-1]=='#':
    asd=1
else:
    asd=0
moves = ((1, 0), (0, 1),)
sy=0
sx=0
ty=h-1
tx=w-1
def bfs(sy, sx):
  dist = [[float('inf')]*w for _ in range(h)]
  dist[sy][sx] = 0 
  q = deque([[sy, sx]])
  while q:
    y, x = q.popleft()
    #print(q)
    for dy, dx in moves:
      ny = y + dy 
      nx = x + dx 
      if 0 <= ny < h and 0 <= nx < w and dist[ny][nx] == float('inf'):
        if c[ny][nx] == '#':
          dist[ny][nx] = dist[y][x] + 1
          q.append([ny, nx])
        else:
          dist[ny][nx] = dist[y][x]
          q.appendleft([ny, nx])

  return dist 

dist = (bfs(sy, sx))
print( dist[ty][tx]+asd)

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w = map(int, input().split())
c = [list(input()) for _ in range(h)]
moves = ((1, 0), (0, 1))
sy = 0
sx = 0
ty = h-1
tx = w-1
        
def bfs(sy, sx):
    dist = [[float('inf')]*w for _ in range(h)]
    if c[0][0] == '#':
        dist[0][0] = 1
    q = deque([[sy, sx]])
    while q:
        y, x = q.popleft()
        #print(q)
        for dy, dx in moves:
            ny = y + dy 
            nx = x + dx 
            if 0 <= ny < h and 0 <= nx < w and dist[ny][nx] == float('inf'):
                if c[ny][nx] == '#' and c[y][x] != '#':
                    dist[ny][nx] = dist[y][x] + 1
                    q.append([ny, nx])
                else:
                    dist[ny][nx] = dist[y][x]
                    q.appendleft([ny, nx])
 
    return dist 
 
dist = (bfs(0, 0))
print(dist[ty][tx])

Submission status: Wrong Answer

Solution code:
r,c=list(int(i) for i in input().split())
A=[]
for i in range(r):
    x=input()
    q=list(x)
    A.append(q)
dp=[[0 for j in range(c)]for i in range(r)]
for i in range(r):
    for j in range(c):
        if i>=1 and j>=1:
            if A[i][j]=="#":
                dp[i][j]=min(dp[i-1][j],dp[i][j-1])+1
            else:
                dp[i][j]=min(dp[i-1][j],dp[i][j-1])
        elif i==0 and j!=0:
            if A[i][j]!="#":
                dp[i][j]=dp[i][j-1]
            else:
                dp[i][j]=dp[i][j-1]+1
        elif i!=0 and j==0:
            if A[i][j]!="#":
                dp[i][j]=dp[i-1][j]
            else:
                dp[i][j]=dp[i-1][j]+1
        else:
            if A[i][j]=="#":
                dp[i][j]=1


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

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""


H,W = m()

po = [list(input()) for i in range(H)]

aaa = [[[mod,0] for i in range(W)] for j in range(H)]


if po[0][0] == "#":
    aaa[0][0] = [1,3]
else:
    aaa[0][0] = [0,0]

for i in range(H):
    if i == 0:
        for j in range(1,W):
            if po[i][j] == "#":
                if aaa[i][j-1][1] == 0:
                    aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0]+1)
                    aaa[i][j][1] = 3
                elif aaa[i][j-1][1] == 1 or aaa[i][j-1][1] == 3:
                    aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0])
                    aaa[i][j][1] = 1
            else:
                aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0])

    else:
        for j in range(W):
            if j == 0:
                if po[i][j] == "#":
                    if aaa[i-1][j][1] == 0:
                        aaa[i][j][0] = min(aaa[i][j][0],aaa[i-1][j][0]+1)
                        aaa[i][j][1] = 3
                    elif aaa[i-1][j][1] == 3 or aaa[i-1][j][1] == 2:
                        aaa[i][j][0] = min(aaa[i][j][0],aaa[i-1][j][0])
                        aaa[i][j][1] = 2
                else:
                    aaa[i][j][0] = min(aaa[i][j][0],aaa[i-1][j][0])
            else:
                if po[i][j] == "#":
                    if aaa[i-1][j][1] == 0 or aaa[i-1][j][1] == 1:
                        if aaa[i][j-1][1] == 0 or aaa[i][j-1][1] == 2:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0]+1,aaa[i-1][j][0]+1)
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 1:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0]+1)
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 3:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0]+1)
                            aaa[i][j][1] = 3
                    elif aaa[i-1][j][1] == 2:
                        if aaa[i][j-1][1] == 0 or aaa[i][j-1][1] == 2:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0]+1,aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 1:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 3:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                    elif aaa[i-1][j][1] == 3:
                        if aaa[i][j-1][1] == 0 or aaa[i][j-1][1] == 2:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0]+1,aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 1:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 3:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                else:
                    aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0])
print(aaa[-1][-1][0])

Submission status: Wrong Answer

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

BanList = []
for i in range(H):
    BanList.append(list(input()))

R = 0
C = 0

result = 0

if BanList[0][0] == '#':
    BanList[0][0] = '.'
    result += 1

while 'true':
    if R == H - 1 and C == W - 1:
        if BanList[R][C] == '#':
            BanList[R][C] = '.'
            result += 1
        break
    elif R == H - 1:
        if BanList[R][C + 1] == '#':
            BanList[R][C + 1] = '.'
            result += 1
        C += 1
    elif C == W - 1:
        if BanList[R + 1][C] == '#':
            BanList[R + 1][C] = '.'
            result += 1
        R += 1
    elif BanList[R + 1][C] == '.' and BanList[R][C + 1] == '#':
        R += 1
    elif BanList[R + 1][C] == '#' and BanList[R][C + 1] == '.':
        C += 1
    elif BanList[R + 1][C] == '.' and BanList[R][C + 1] == '.':
        if R > C:
            C += 1
        else:
            R += 1
    else:
        if R > C:
            BanList[R][C + 1] = '.'
            C += 1
        else:
            BanList[R + 1][C] = '.'
            R += 1
        result += 1

print(result)

Submission status: Wrong Answer

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

k = s
for i in range(0,h):
    for j in range(0,w):
        if(i == 0 and j == 0):
            if(s[i][j] == '#'):
                k[i][j] = 1
            else:
                k[i][j] = 0

        if(j == 0 and i!=0):
            if(s[i][j] == '#'):
                k[i][j] = k[i-1][j]+ 1
            else:
                k[i][j] = k[i-1][j]
        if(i == 0 and j!= 0):
            # print(i,j)
            if(s[i][j] == '#'):
                k[i][j] = k[i][j-1] + 1
            else:
                k[i][j] = k[i][j-1]

        if(j!=0 and i!=0):
            if( s[i][j] == '#'):
                if(k[i-1][j] <= k[i][j-1]):
                    k[i][j] = k[i-1][j] + 1
                else:
                    k[i][j] = k[i][j-1] + 1
            else:
                if(k[i-1][j] <= k[i][j-1]):
                    k[i][j] = k[i-1][j]
                else:
                    k[i][j] = k[i][j-1]

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

Submission status: Wrong Answer

Solution code:
from fractions import gcd
# from datetime import date, timedelta
# from heapq import *
import heapq
import math
from collections import defaultdict, Counter, deque
from bisect import *
import itertools
import fractions
import sys
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
# input = sys.stdin.readline


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


def main():

    # print(100, 100)
    # for i in range(100):
    #     s=""
    #     for j in range(100):
    #         if j % 2 == 0:
    #             if i % 2 == 0:
    #                 s += "#"
    #             else:
    #                 s += "."
    #         else:
    #             if i % 2 == 0:
    #                 s += "."
    #             else:
    #                 s += "#"
    #     print(s)



    h, w = map(int, input().split())
    maze = [input() for i in range(h)]
    ans = 0
    # if maze[0][0] == "#":
    #     ans+=1
    

    def Dijkstra_search(sx, sy):
        H = h
        W = w
        visited = [[False for i in range(W)] for j in range(H)]
        d = [[float("inf") for i in range(W)] for j in range(H)]  # now cost
        d[sx][sy] = 0

        queue = []
        heapq.heappush(queue, (0, (sx, sy)))
        while len(queue):
            cost, (x, y) = heapq.heappop(queue)

            if visited[x][y]:
                continue
            visited[x][y] = True

            for dx, dy in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
                nx = x + dx
                ny = y + dy
                if nx >= 0 and ny >= 0 and nx < H and ny < W:
                    if visited[nx][ny]:
                        continue

                    vd=cost
                    if maze[nx][ny] =="." and maze[x][y]=="#":
                        vd+=1
                    if d[nx][ny] > vd:
                        d[nx][ny] = vd
                        heapq.heappush(queue, (vd, (nx, ny)))

        if maze[h - 1][w - 1] == "#":
            d[h-1][w-1]+=1
        return d[h-1][w-1]

    ans += Dijkstra_search(0,0)
    print(ans)
 


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq
h, w = map(int, input().split())
c = [input() for _ in range(h)]
 
d=[[100000]*w for _ in range(h)]
 
def bfs():
    if c[0][0] == '.':
        que = [(0, (0, 0))]
        heapq.heapify(que)
        d[0][0] = 0
    else:
        que = [(1, (0, 0))]
        heapq.heapify(que)
        d[0][0] = 1
    while len(que):
        _, (qx, qy) = heapq.heappop(que)
        for dx,dy in [[1,0], [0,1]]:
            px, py = qx+dx, qy+dy
            if 0<=px<h and 0<=py<w and d[px][py]>d[qx][qy]:
                d[px][py]=d[qx][qy] + (c[px][py]=='#')
                heapq.heappush(que, (d[px][py], (px , py)))
bfs()
print(d[h-1][w-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
import numpy as np

H, W = map(int, input().split())
c = [input() for _ in range(H)]
si = sj =0
flg=(c[si][sj]=="#")

#= gi = gj = 0
#for i in range(H):
#    for j in range(W):
#        if c[i][j] == 's':
#            si, sj = i, j
#        if c[i][j] == 'g':
#            gi, gj = i, j
dist = [[-1] * W for _ in range(H)]
dist[si][sj] = 0
queue = deque()
queue.append((si, sj))
move = [(1, 0), (0, 1)]
while queue:
    i, j = queue.popleft()
    for di, dj in move:
        ni, nj = i+di, j+dj
        if 0 <= ni < H and 0 <= nj < W and dist[ni][nj] == -1:
            if c[ni][nj] == '#':
                dist[ni][nj] = dist[i][j] + 1
                queue.append((ni, nj))
            else:
                dist[ni][nj] = dist[i][j]
                queue.appendleft((ni, nj))

                
print(dist[-1][-1]+1*(flg))           
#print(np.array(dist))
#if dist[gi][gj] <= 2:
#    print('YES')
#else:
#    print('NO')

Submission status: Wrong Answer

Solution code:
h,w = (int(x) for x in input().split())
ab = []
for _ in range(h):
    a = input()
    a = a+'#'
    ab.append(list(a))
ab.append(list('#'*w))
dp = [[100000000000000000 for a in range(w+1)] for b in range(h+1)]
if ab[0][0] == '#':
    dp[0][0] = 1
else:
    dp[0][0] = 0
step = [[0, 0]]
 
while len(step) > 0:
    #print(step)
    y, x = step.pop(0)
    
    if h-1<y or w-1<x :continue
    if dp[y+1][x] == 100000000000000000:
        step.append([y+1, x])
    if dp[y][x+1] == 100000000000000000:
        step.append([y, x+1])
    
    if ab[y+1][x] == '#':
        dp[y+1][x] = min(dp[y][x] + 1, dp[y+1][x])
    else:
        dp[y+1][x] = min(dp[y][x], dp[y+1][x])
    if ab[y][x+1] == '#':
        dp[y][x+1] = min(dp[y][x] + 1, dp[y][x+1])
    else:
        dp[y][x+1] = min(dp[y][x],dp[y][x+1])
 
print(dp[h-1][w-1])

Submission status: Wrong Answer

Solution code:
h,w = list(map(int,input().split()))
s = []
hc = 0
wc = 0
res = 0

for i in range(h):
    s.append(list(input()))

h = h-1
w = w-1

while True:
    if s[hc][wc] == "#":
        res += 1
    
    if hc == h and wc == w:
        break
    
    if hc < h and wc == w:        
        hc += 1
    elif hc == h and wc < w: 
        wc += 1
    else:
        if s[hc+1][wc] == ".":
            hc += 1
        elif s[hc][wc+1] == ".":
            wc += 1
        else:
            wc += 1

hc = h
wc = w
res1 = 0
while True:
    if s[hc][wc] == "#":
        res1 += 1
    
    if hc == 0 and wc == 0:
        break
    
    if hc > 0 and wc == 0:        
        hc -= 1
    elif hc == h and wc > 0: 
        wc -= 1
    else:
        if s[hc-1][wc] == ".":
            hc -= 1
        elif s[hc][wc-1] == ".":
            wc -= 1
        else:
            hc -= 1

if res < res1:
    print(res)
else:
    print(res1)

Submission status: Wrong Answer

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

M = [[0] * W for _ in range(H)]
colors = [[''] * W for _ in range(H)]
for i in range(H):
    colors[i] = list(input())
    for j in range(W):
        if i == 0:
            if j == 0:
                M[i][j] = 0 if colors[i][j] == '.' else 1
            else:
                M[i][j] = M[i][j-1] if colors[i][j] == '.' else M[i][j-1] + 1
        else:
            if j == 0:
                M[i][j] = M[i-1][j] if colors[i][j] == '.' else M[i-1][j] + 1
            else:
                if colors[i][j] == '.':
                    M[i][j] = min([M[i-1][j], M[i][j-1]])
                else:
                    top = M[i-1][j] if colors[i-1][j] == '#' else M[i-1][j] + 1
                    left = M[i][j-1] if colors[i][j-1] == '#' else M[i][j-1] + 1
                    M[i][j] = min(top, left)

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

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""


H,W = m()

po = [list(input()) for i in range(H)]

aaa = [[[mod,0] for i in range(W)] for j in range(H)]


if po[0][0] == "#":
    aaa[0][0] = [1,3]
else:
    aaa[0][0] = [0,0]

for i in range(H):
    if i == 0:
        for j in range(1,W):
            if po[i][j] == "#":
                if aaa[i][j-1][1] == 0:
                    aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0]+1)
                    aaa[i][j][1] = 3
                elif aaa[i][j-1][1] == 1 or aaa[i][j-1][1] == 3:
                    aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0])
                    aaa[i][j][1] = 1
            else:
                aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0])

    else:
        for j in range(W):
            if j == 0:
                if po[i][j] == "#":
                    if aaa[i-1][j][1] == 0:
                        aaa[i][j][0] = min(aaa[i][j][0],aaa[i-1][j][0]+1)
                        aaa[i][j][1] = 3
                    elif aaa[i-1][j][1] == 3 or aaa[i-1][j][1] == 2:
                        aaa[i][j][0] = min(aaa[i][j][0],aaa[i-1][j][0])
                        aaa[i][j][1] = 2
                else:
                    aaa[i][j][0] = min(aaa[i][j][0],aaa[i-1][j][0])
            else:
                if po[i][j] == "#":
                    if aaa[i-1][j][1] == 0 or aaa[i-1][j][1] == 1:
                        if aaa[i][j-1][1] == 0:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0]+1,aaa[i-1][j][0]+1)
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 1:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0]+1)
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 3:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0]+1)
                            aaa[i][j][1] = 3
                    elif aaa[i-1][j][1] == 2:
                        if aaa[i][j-1][1] == 0:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0]+1,aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 1:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 3:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                    elif aaa[i-1][j][1] == 3:
                        if aaa[i][j-1][1] == 0:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0]+1,aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 1:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                        elif aaa[i][j-1][1] == 3:
                            aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0])
                            aaa[i][j][1] = 3
                else:
                    aaa[i][j][0] = min(aaa[i][j][0],aaa[i][j-1][0],aaa[i-1][j][0])
print(aaa[-1][-1][0])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def BellmanFord(g, s):
    V = list(g.keys())
    for v in g.values():
        V.extend(list(v.keys()))
    V = set(V)

    dist = defaultdict(lambda: sys.maxsize)
    pred = defaultdict(lambda: -1)

    dist[s] = 0

    N = len(V)
    for i in range(N+1):
        for u in g:
            for v in g[u]:
                if dist[v] > dist[u] + g[u][v]:
                    if i == N:
                        return 'inf'
                    dist[v] = dist[u] + g[u][v]
                    pred[v] = u
    return dist



@mt
def slv(H, W, S):
    g = defaultdict(dict)
    for i in range(H):
        for j in range(W):
            for x, y in ((0, 1), (1, 0), (0, -1), (-1, 0)):
                if 0 <= i + x < H and 0 <= j+y < W:
                    if S[i][j] == '.' and S[i+x][j+y] == '#':
                        c = 1
                    else:
                        c = 0
                    g[(i, j)][(i+x, j+y)] = c

    def bfs(s=(0, 0)):
        q = deque()
        q.append(s)
        done = {s}
        d = defaultdict(lambda: sys.maxsize)
        d[s] = 0
        while q:
            u = q.popleft()
            for v, c in g[u].items():
                if d[u] + c < d[v]:
                    d[v] = d[u] + c
                    if c == 0:
                        q.appendleft(v)
                    else:
                        q.append(v)

        return d

    return bfs()[(H-1, W-1)] + (1 if S[0][0] == '#' else 0)


def main():
    H, W = read_int_n()
    S = [read_str() for _ in range(H)]
    print(slv(H, W, S))

    # H = 100
    # W = 100
    # S = [''.join(random.choices('.#', k=W)) for _ in range(H)]
    # print(slv(H, W, S))


if __name__ == '__main__':
    main()