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())
field = [list(input()) for _ in range(H)]
dp = [[10**9] * (W+1) for _ in range(H+1)]

dp[0][1] = 0 if field[0][0] == "." else 1
for h in range(1,H+1):
    for w in range(1,W+1):
        if field[h-1][w-1] == ".":
            dp[h][w] = min(dp[h-1][w], dp[h][w-1])
        else:
            u = dp[h-1][w] if field[h-2][w-1] == "#" else dp[h-1][w] + 1
            l = dp[h][w-1] if field[h-1][w-2] == "#" else dp[h][w-1] + 1
            dp[h][w] = min(u,l)

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

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]
dp = [[10**9] * (W+1) for _ in range(H+1)]
dp[0][1] = 0
for h in range(1,H+1):
    for w in range(1,W+1):
        if field[h-1][w-1] == ".":
            dp[h][w] = min(dp[h-1][w], dp[h][w-1])
        else:
            u = dp[h-1][w] if field[h-2][w-1] == "#" else dp[h-1][w] + 1
            l = dp[h][w-1] if field[h-1][w-2] == "#" else dp[h][w-1] + 1
            dp[h][w] = min(u,l)

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
H,W=map(int,input().split()) 
S=[] 

for _ in range(H):
    S.append(list(input()))

log=[[0]*W for i in range(H)]
log[0][0]=1 if S[0][0]=="#" else 0

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

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

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
h,w = map(int,input().split())
C = [list(input()) for i in range(h)]
ans = 0
check = 0
for i in range(h):
    if len(C[i]) == C[i].count('#'):
        check += 1        
if check == h:
    print(1)
    sys.exit()
s = "#"   
dy_dx = [[1,0],[0,1]]
visited1 = [[0 for i in range(w)] for i in range(h)]

def bfs():
    global ans
    global s
    """
    for i in range(h):
      for j in range(w):
        print(C[i][j],end="")
      print("")
    print("=-------------")
    """
    queue = []
    visited = copy.deepcopy(visited1)

    queue.append([0,0])
    visited[0][0] = 1
  
    while len(queue) > 0:
        now = queue.pop(0)
        for k in range(2):
            y = now[0]+dy_dx[k][0]
            x = now[1]+dy_dx[k][1]
            if 0 <= y < h and 0 <= x < w:
                if C[y][x] != s and visited[y][x] == 0:
                    visited[y][x] = visited[now[0]][now[1]]+1
                    C[y][x] = s
                    C[now[0]][now[1]] = s
                    queue.append([y,x])
                    
    if visited[h-1][w-1] != 0:
        
        print(math.ceil(ans/2))
        sys.exit()
    ans += 1
    if s == '#':
      s = "."
    else:
      s = "#"
    bfs()
        
bfs()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
from collections import deque


H,W=map(int,input().split()) 
S=[] 

for _ in range(H):
    S.append(list(input()))

log=[[10**9]*W for i in range(H)]

def bfs(h,w,num):
    if num>=log[h][w]:
        return
    else:
        log[h][w]=num
    
    if h!=H-1: 
        if S[h+1][w]=="#": 
            q.append([h+1,w,num+1])
        else:
            q.append([h+1,w,num])

    if w!=W-1: 
        if S[h][w+1]=="#": 
            q.append([h,w+1,num+1])
        else:
            q.append([h,w+1,num])

q=[[0,0,1] if S[0][0]=="#" else [0,0,0]] 
q = deque(q)
#print(q)

while q: 
    item=q.popleft()
    bfs(item[0],item[1],item[2])


print(log[-1][-1])

Submission status: Wrong Answer

Solution code:
def main():
  import sys
  b=sys.stdin.buffer
  h,w=map(int,b.readline().split())
  x=w+1
  s=[c>>3for c in b.read()]
  dp=[10000]*h*x
  a=p=s[0]
  dp[0]=b=q=5-a
  for i in range(w):
    t=s[i]
    b+=a^t
    dp[i]=b
    a=t
  for i in range(x,h*x,x):
    t=s[i]
    q+=p^t
    dp[i]=b=q
    a=p=t
    i-=x
    for j in range(i+1,i+w):
      sj=s[j]
      b+=a^sj
      i+=1
      t=dp[i]+(s[i]^sj)
      if b>t:b=t
      dp[j]=b
      a=sj
  print(b+1>>1)
main()

Submission status: Wrong Answer

Solution code:
from collections import *
from heapq import *
H,W=map(int,input().split())
S=[input() for i in range(H)]
count=0
stack=deque([[0,0,0]])
RuiW=[[0] for i in range(H)]
RuiH=[[0] for i in range(W)]
if S[0][0]=="#":
    RuiW[0][0]=1
    RuiH[0][0]=1

for h in range(H):
    flag=0
    for w in range(1,W):
        RuiW[h].append(RuiW[h][-1]+bool(S[h][w]=="#" and S[h][w-1]=="."))
for w in range(W):
    for h in range(1,H):
        RuiH[w].append(RuiH[w][-1]+bool(S[h][w]=="#" and S[h-1][w]=="."))

Range=range(1,100)
idle_max=10**9
visited=[[idle_max for i in range(W)] for m in range(H)]
visited2=set([0])
#print(RuiW)
#print(RuiH)
dic={}
dic[0]=[[0,0]]
stack=[0]
while stack:
    m=heappop(stack)
    h,w=dic[m].pop()
    if h==H-1 and w==W-1:
        print(m)
        exit()
    if visited[h][w]<m:
        continue
    for i in Range:
        if h+i>=H:
            break
        M=RuiH[w][h+i]-(RuiH[w][h]-RuiH[w][h]%2)+m

        if visited[h+i][w]>M:
            visited[h+i][w]=M
            if M in dic:
                dic[M].append([h+i,w])
            else:
                dic[M]=[[h+i,w]]
            heappush(stack,M)
    for i in Range:
        if w+i>=W:
            break
        M=RuiW[h][w+i]-(RuiW[h][w]-RuiW[h][w]%2)+m
        if visited[h][w+i]>M:
            visited[h][w+i]=M
            if M in dic:
                dic[M].append([h,w+i])
            else:
                dic[M]=[[h,w+i]]
            heappush(stack,M)
print(visited[-1][-1])

Submission status: Wrong Answer

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

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

for i in range(1,W):
  if s[0][i] == '.':
    dp[0][i] = dp[0][i-1]
  else:
    if s[0][i-1] == '.':
      dp[0][i] = dp[0][i-1]+1
    else:
      dp[0][i] = dp[0][i-1]

for i in range(1,H):
  for j in range(W):
    if j == 0:
      if s[i][j] == '.':
        dp[i][j] = dp[i-1][j]
      else:
        if s[i][j-1] == '.':
          dp[i][j] = dp[i-1][j]+1
        else:
          dp[i][j] = dp[i-1][j]
    else:
      if s[i][j] == '.':
        dp[i][j] = min(dp[i-1][j],dp[i][j-1])
      else:
        left = 0
        up = 0
        if s[i-1][j] == '.':
          left = 1
        if s[i][j-1] == '.':
          up = 1
        dp[i][j] = min(dp[i-1][j]+left,dp[i][j-1]+up)
print(dp[-1][-1])

Submission status: Wrong Answer

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

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

for i in range(1,W):
  if s[0][i] == '.':
    dp[0][i] = dp[0][i-1]
  else:
    if s[0][i-1] == '.':
      dp[0][i] = dp[0][i-1]+1
    else:
      dp[0][i] = dp[0][i-1]

for i in range(1,H):
  for j in range(W):
    if j == 0:
      if s[i][j] == '.':
        dp[i][j] = dp[i-1][j]
      else:
        dp[i][j] = dp[i-1][j]+1
    else:
      if s[i][j] == '.':
        dp[i][j] = min(dp[i-1][j],dp[i][j-1])
      else:
        left = 0
        up = 0
        if s[i-1][j] == '.':
          left = 1
        if s[i][j-1] == '.':
          up = 1
        dp[i][j] = min(dp[i-1][j]+left,dp[i][j-1]+up)
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
from collections import *
H,W=map(int,input().split())
S=[input() for i in range(H)]
count=0
stack=deque([[0,0,0]])
RuiW=[[0] for i in range(H)]
RuiH=[[0] for i in range(W)]
if S[0][0]=="#":
    RuiW[0][0]=1
    RuiH[0][0]=1

for h in range(H):
    flag=0
    for w in range(1,W):
        RuiW[h].append(RuiW[h][-1]+bool(S[h][w]=="#" and S[h][w-1]=="."))
for w in range(W):
    for h in range(1,H):
        RuiH[w].append(RuiH[w][-1]+bool(S[h][w]=="#" and S[h-1][w]=="."))

Range=range(1,100)
idle_max=10**9
visited=[[idle_max for i in range(W)] for m in range(H)]
visited2=set([0])
#print(RuiW)
#print(RuiH)
while stack:
    h,w,m=stack.popleft()
    A=h*100+w+m*10000
    if visited[h][w]<m:
        continue
    if visited[h][w]==m and A in visited2:
        continue
    visited2.add(A)
    for i in Range:
        if h+i>=H:
            break
        M=RuiH[w][h+i]-(RuiH[w][h]-RuiH[w][h]%2)+m

        if visited[h+i][w]>M:
            visited[h+i][w]=M
            stack.append([h+i,w,M])
    for i in Range:
        if w+i>=W:
            break
        M=RuiW[h][w+i]-(RuiW[h][w]-RuiW[h][w]%2)+m
        if visited[h][w+i]>M:
            visited[h][w+i]=M
            stack.append([h,w+i,M])
print(visited[-1][-1])

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
a = lambda x: 1 if x=="#" else 0
S = [list(map(a, list(input()))) for _ in range(h)]
inf = 10*3
dp = [[inf]*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]*(S[0][i]!=S[0][i-1])
for i in range(1, h):
    dp[i][0] = dp[i-1][0] + S[i][0]*(S[i][0]!=S[i-1][0])
for i in range(1, h):
    for j in range(1, w):
        dp[i][j] = min(dp[i-1][j]+S[i][j]*(S[i-1][j]==S[i][j]), dp[i][j-1]+S[i][j]*(S[i][j]!=S[i][j-1]))
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
from collections import *
H,W=map(int,input().split())
S=[input() for i in range(H)]
count=0
stack=deque([[0,0,0,S[0][0]=="#"]])
RuiW=[[0] for i in range(H)]
RuiH=[[0] for i in range(W)]
if S[0][0]=="#":
    RuiW[0][0]=1
    RuiH[0][0]=1

for h in range(H):
    flag=0
    for w in range(1,W):
        RuiW[h].append(RuiW[h][-1]+bool(S[h][w]=="#" and S[h][w-1]=="."))
for w in range(W):
    for h in range(1,H):
        RuiH[w].append(RuiH[w][-1]+bool(S[h][w]=="#" and S[h-1][w]=="."))

Range=range(1,100)
idle_max=10**9
visited=[[idle_max for i in range(W)] for m in range(H)]
visited2=set([0])
#print(RuiW)
#print(RuiH)
while stack:
    h,w,m,Flag=stack.popleft()
    if visited[h][w]<m:
        continue
    if visited[h][w]==m and h*1000+w in visited2:
        continue
    visited2.add(h*1000+w)

    for i in Range:
        if h+i>=H:
            break
        M=RuiH[w][h+i]-(RuiH[w][h]-RuiH[w][h]%2)+m

        if visited[h+i][w]>M:
            visited[h+i][w]=M
            stack.append([h+i,w,M,0])
    for i in Range:
        if w+i>=W:
            break
        M=RuiW[h][w+i]-(RuiW[h][w]-RuiW[h][w]%2)+m
        if visited[h][w+i]>M:
            visited[h][w+i]=M
            stack.append([h,w+i,M,0])
#print(visited)
print(visited[-1][-1])

Submission status: Wrong Answer

Solution code:
from collections import *
H,W=map(int,input().split())
S=[input() for i in range(H)]
count=0
stack=deque([[0,0,0,S[0][0]=="#"]])
RuiW=[[0] for i in range(H)]
RuiH=[[0] for i in range(W)]
if S[0][0]=="#":
    RuiW[0][0]=1
    RuiH[0][0]=1

for h in range(H):
    flag=0
    for w in range(1,W):
        RuiW[h].append(RuiW[h][-1]+bool(S[h][w]=="#" and S[h][w-1]=="."))
for w in range(W):
    for h in range(1,H):
        RuiH[w].append(RuiH[w][-1]+bool(S[h][w]=="#" and S[h-1][w]=="."))

Range=range(1,100)
idle_max=10**9
visited=[[idle_max for i in range(W)] for m in range(H)]
#print(RuiW)
#print(RuiH)
while stack:
    h,w,m,Flag=stack.popleft()
    if visited[h][w]<m:
        continue
    for i in Range:
        if h+i>=H:
            break
        M=RuiH[w][h+i]-(RuiH[w][h]-RuiH[w][h]%2)+m

        if visited[h+i][w]>M:
            visited[h+i][w]=M
            stack.append([h+i,w,M,0])
    for i in Range:
        if w+i>=W:
            break
        M=RuiW[h][w+i]-(RuiW[h][w]-RuiW[h][w]%2)+m
        if visited[h][w+i]>M:
            visited[h][w+i]=M
            stack.append([h,w+i,M,0])
#print(visited)
print(visited[-1][-1])

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
S = [list(input()) for _ in range(h)]
inf = 10*3
dp = [[inf]*w for _ in range(h)]

dp[0][0] = int(S[0][0]=="#")
for i in range(1, w):
    dp[0][i] = dp[0][i-1] + (S[0][i-1]!=S[0][i])
for i in range(1, h):
    dp[i][0] = dp[i-1][0] + (S[i-1][0]!=S[i][0])
for i in range(1, h):
    for j in range(1, w):
        dp[i][j] = min(dp[i-1][j]+(S[i-1][j]!=S[i][j]), dp[i][j-1]+(S[i][j-1]!=S[i][j]))
print(dp[-1][-1])

Submission status: Wrong Answer

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

d[0][0] = ( g[0][0] == "#")
for i in range(h):
    for j in range(w):
        if i + 1 < h:
            d[i+1][j] = min(d[i+1][j], d[i][j]+(g[i+1][j] == '#' and g[i][j] == '.'))
        if j + 1 < w:
            d[i][j+1] = min(d[i][j+1], d[i][j]+(g[i][j+1] == '#' and g[i][j] == '.'))

print(d[-1][-1])

Submission status: Wrong Answer

Solution code:
from decimal import *
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True

BLOCK = '#'
ROAD = '.'


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


def bfs(graph, initial, goal, H, W):
    options = [(1, 0), (0, 1)]
    q = deque([initial])
    dist = [[10**9 for i in range(W)] for i in range(H)]
    dist[initial[0]][initial[1]] = 0 if graph[initial[0]][initial[1]] == ROAD else 1

    while len(q) != 0:
        p = q.popleft()

        for d in options:
            i = p[0] + d[0]
            j = p[1] + d[1]

            if i >= 0 and i < H and j >= 0 and j < W:
                if dist[i][j] == 10**9 or dist[p[0]][p[1]] < dist[i][j]:
                    q.append((i, j))
                    if graph[i][j] == BLOCK:
                        dist[i][j] = dist[p[0]][p[1]] + 1
                    else:
                        dist[i][j] = dist[p[0]][p[1]]

    # print_matrix(dist)
    return dist[goal[0]][goal[1]]


def main():
    H, W = MI()
    graph = [None] * H

    for i in range(H):
        graph[i] = list(S())

    ret = bfs(graph, (0, 0), (H-1, W-1), H, W)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from decimal import *
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True

BLOCK = '#'
ROAD = '.'


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


def bfs(graph, initial, goal, H, W):
    options = [(1, 0), (0, 1)]
    q = deque([initial])
    dist = [[10**9 for i in range(W)] for i in range(H)]
    dist[initial[0]][initial[1]] = 0 if graph[initial[0]][initial[1]] == ROAD else 1

    while len(q) != 0:
        p = q.popleft()

        for d in options:
            i = p[0] + d[0]
            j = p[1] + d[1]

            if i >= 0 and i < H and j >= 0 and j < W:
                if graph[i][j] == BLOCK:
                    if dist[p[0]][p[1]] < dist[i][j]:
                        q.append((i, j))
                        dist[i][j] = dist[p[0]][p[1]] + 1
                else:
                    if dist[p[0]][p[1]] < dist[i][j]:
                        q.append((i, j))
                        dist[i][j] = dist[p[0]][p[1]]

    # print_matrix(dist)
    return dist[goal[0]][goal[1]]


def main():
    H, W = MI()
    graph = [None] * H

    for i in range(H):
        graph[i] = list(S())

    ret = bfs(graph, (0, 0), (H-1, W-1), H, W)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

for i in range(1, W):
    if S[0][i] == '.':
        dp[0][i] = dp[0][i-1]
    else:
        if S[0][i-1] == '.':
            dp[0][i] = dp[0][i-1] + 1
        else:
            dp[0][i] = dp[0][i-1]

for j in range(1, H):
    if S[j][0] == '.':
        dp[j][0] = dp[j-1][0]
    else:
        if S[j-1][0] == '.':
            dp[j][0] = dp[j-1][0] + 1
        else:
            dp[j][0] = dp[j-1][0]

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

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

Submission status: Wrong Answer

Solution code:
import copy
n,m=map(int,input().split())
l=[]
for i in range(n):
    s=list(input())
    l.append(s)
flag=0
s=copy.deepcopy(l)
#print(l)
if(l[0][0]=="#"):
    l[0][0]=1 
else:
    l[0][0]=0;
for i in range(1,n):
    if(l[i][0]=="#"):
        if s[i-1][0]=="#":
            l[i][0]=l[i-1][0]
        else:
            l[i][0]=l[i-1][0]+1
            
    else:
        l[i][0]=l[i-1][0]
for i in range(1,m):
    if( l[0][i]=="#"):
        if s[0][i-1]=="#":
            l[0][i]=l[0][i-1]
        else:
            l[0][i]=l[0][i-1]+1
    else:
        l[0][i]=l[0][i-1]

for i in range(1,n):
    for j in range(1,m):
        if(l[i][j]=="#"):
            a1=s[i][j-1]
            a2=s[i-1][j]
            if a1==".":
                l[i][j-1]+=1
            if a2==".":
                l[i-1][j]+=1 
            l[i][j]=min(l[i-1][j],l[i][j-1])
        else:
            l[i][j]=min(l[i-1][j],l[i][j-1])
#print(l)
print(l[n-1][m-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque as dq
input = sys.stdin.readline
H, W = map(int, input().split())
S = [list(input())[: -1] for _ in range(H)]
Q = dq([0])
d = [-1, 0, 1, 0]
dp = [float("inf")] * (H * W)
dp[0] = int(S[0][0] == "#")
while len(Q):
  p = Q.popleft()
  for k in range(4):
    y = p // W + d[k]
    x = p % W + d[-1 - k]
    if y in range(H) and (x in range(W)):
      f = (S[p // W][p % W] == ".") and (S[y][x] == "#")
      if dp[y * W + x] > dp[p] + f:
        dp[y * W + x] = dp[p] + f
        if f: Q.append(y * W + x)
        else: Q.appendleft(y * W + x)
print(dp[-1])

Submission status: Wrong Answer

Solution code:
from decimal import *
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True

BLOCK = '#'
ROAD = '.'


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


def bfs(graph, initial, goal, H, W):
    options = [(1, 0), (0, 1)]
    q = deque([initial])
    dist = [[10**9 for i in range(W)] for i in range(H)]
    dist[initial[0]][initial[1]] = 0 if graph[initial[0]][initial[1]] == ROAD else 1

    while len(q) != 0:
        p = q.popleft()

        for d in options:
            i = p[0] + d[0]
            j = p[1] + d[1]

            if i >= 0 and i < H and j >= 0 and j < W:
                if graph[i][j] == BLOCK:
                    if dist[p[0]][p[1]] + 1 < dist[i][j]:
                        q.append((i, j))
                        dist[i][j] = dist[p[0]][p[1]] + 1
                else:
                    if dist[p[0]][p[1]] < dist[i][j]:
                        q.append((i, j))
                        dist[i][j] = dist[p[0]][p[1]]

    # print_matrix(dist)
    return dist[goal[0]][goal[1]]


def main():
    H, W = MI()
    graph = [None] * H

    for i in range(H):
        graph[i] = list(S())

    ret = bfs(graph, (0, 0), (H-1, W-1), H, W)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
h,w = [int(i) for i in input().split()]
s = []
for i in range(h):
  array = list(input())
  s.append(array)
d = [[float("inf")] * (w) for i in range(h)]
d[0][0]=0
if s[0][0]=='#':
  d[0][0]=1
for i in range(1,w):
  if s[0][i]!=s[0][i-1] and s[0][i]!=s[0][0]:
    d[0][i]=d[0][i-1]+1
  else:
    d[0][i]=d[0][i-1]
for i in range(1,h):
  for j in range(w):
    k=0
    u=0
    if j != 0 :
      if s[i][j]!=s[0][0] and s[i][j]!=s[i-1][j]:
        k=1
      if s[i][j]!=s[0][0] and s[i][j]!=s[i][j-1]:
        u=1
      d[i][j] = min(d[i-1][j]+k,d[i][j-1]+u)
    elif j == 0 :
      if s[i][0]!=s[0][0] and s[i][0]!=s[i-1][0]:
        k=1
      d[i][0] = d[i-1][0]+k

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

Submission status: Wrong Answer

Solution code:
from collections import *
H,W=map(int,input().split())
S=[input() for i in range(H)]
count=0
if S[0][0]=="#":
    count+=1

stack=deque([[0,0,0]])
visited=[[100000 for m in range(W)] for i in range(H)]
while stack:
    #print(stack)
    h,w,m=stack.popleft()
    for y,x in [[1,0],[0,1]]:
        Y=h+y;X=w+x

        if Y<H and X<W:
            M=m + (1 if S[Y][X]=="#" else 0)
            if M<visited[Y][X]:
                stack.append([Y,X,M])
                visited[Y][X]=M
#print(visited)
print(visited[-1][-1] + count)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
S=[]
for i in range(h):
    S.append(input())
    
cnt=0
D=[[10**9]*w for _ in range(h)]
D[0][0]=0
for i in range(h):
    for j in range(w):
        for x,y in [(0,1),(1,0)]:
            if i+x>=h or j+y>=w:
                continue
            if S[i][j]!=S[i+x][j+y]:
                D[i+x][j+y]=min(D[i][j]+1,D[i+x][j+y])
            else:
                D[i+x][j+y]=D[i][j]
                
                
if S[0][0]=='#':
    print(1+D[h-1][w-1]//2)
else:
    print(D[h-1][w-1]//2+(S[0][0]!=S[h-1][w-1]))

Submission status: Wrong Answer

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

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

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

for i in range(1, W):
    if S[0][i] == '#':
        dp[0][i] = dp[0][i-1] + 1
    else:
        dp[0][i] = dp[0][i-1]

for j in range(1, H):
    if S[j][0] == '#':
        dp[j][0] = dp[j-1][0] + 1
    else:
        dp[j][0] = dp[j-1][0]

for j in range(1, H):
    for i in range(1, W):
        v = min(dp[j-1][i], dp[j][i-1])
        if S[j][i] == '#':
            dp[j][i] = v + 1
        else:
            dp[j][i] = v
print(dp[H-1][W-1])

Submission status: Wrong Answer

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

cnt=0
D=[[10**9]*w for _ in range(h)]
D[0][0]=0
for i in range(h):
    for j in range(w):
        if j>0:
            D[i][j]=D[i][j-1]+(S[i][j]!=S[i][j-1])
        if i>0:
            D[i][j]=min(D[i][j],D[i-1][j]+(S[i][j]!=S[i-1][j]))   
                
                
if S[0][0]=='#':
    print(1+D[h-1][w-1]//2)
else:
    print((D[h-1][w-1]+1)//2+(S[0][0]!=S[h-1][w-1]))

Submission status: Wrong Answer

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

cnt=0
D=[[10**9]*w for _ in range(h)]
D[0][0]=0
for i in range(h):
    for j in range(w):
        if j>0:
            D[i][j]=D[i][j-1]+(S[i][j]!=S[i][j-1])
        if i>0:
            D[i][j]=min(D[i][j],D[i-1][j]+(S[i][j]!=S[i-1][j]))   
                
                
if D[h-1][w-1]==0 and S[0][0]=='#':
    print(1)
else:
    print((D[h-1][w-1]+1)//2)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
route = []
memo= [0 for z in range(w + h - 2)]
def serch(x,y):
    memo[(x + y)] = s[x][y]
    if x==h-2 and y==w-2:
        route.append(memo)
        return
    elif x==h-2:
        serch(x, y+1)
        return
    elif y==w-2:
        serch(x+1, y)
        return
    #route.append(str(x) + "," + str(y) + "は" + s[x][y])
    serch(x+1, y)
    serch(x, y+1)
    return


serch(0, 0)
cnt = 0
result = 0
for i in range(len(route)):
    for k in range(len(route[i])-1):
        if route[i][k] != route[i][k+1]:
            cnt += 1
    if result <cnt:
        result = cnt
    cnt = 0
print((result+1)//2)

Submission status: Wrong Answer

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

if field[0][0] == '#':
    dist[0][0] += 1

for w in range(1, W):
    if field[0][w] == '#':
        dist[0][w] = dist[0][w-1] + 1
    else:
        dist[0][w] = dist[0][w-1]

for h in range(1, H):
    if field[h][0] == '#':
        dist[h][0] = dist[h-1][0] + 1
    else:
        dist[h][0] = dist[h-1][0]

for h in range(1, H):
    for w in range(1, W):
        if field[h][w] == '#':
            dist[h][w] = min(dist[h][w-1], dist[h-1][w]) + 1
        else:
            dist[h][w] = min(dist[h][w-1], dist[h-1][w])

print(dist[-1][-1])

Submission status: Wrong Answer

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

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

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

for i in range(1, W):
    if S[0][i] == '#':
        dp[0][i] = dp[0][i-1] + 1
    else:
        dp[0][i] = dp[0][i-1]

for j in range(1, H):
    if S[j][0] == '#':
        dp[j][0] = dp[j-1][0] + 1
    else:
        dp[j][0] = dp[j-1][0]

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

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

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 

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

    ans = [[0]*W for _ in range(H)]
    if s[0][0]=='#':
        ans[0][0] = 1
    for i in range(1, W+H-1):
        for y in range(max(0, i-(W-1)), min(i+1, H)):
            x = i-y
            if y==0:
                if s[y][x-1]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y][x-1] + 1
                else:
                    ans[y][x] = ans[y][x-1]
            elif x==0:
                if s[y-1][x]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y-1][x] + 1
                else:
                    ans[y][x] = ans[y-1][x]
            else:
                if (s[y-1][x]=='.' or s[y][x-1]=='.') and s[y][x]=='#':
                    ans[y][x] = min(ans[y-1][x], ans[y][x-1]) + 1
                else:
                    ans[y][x] = min(ans[y-1][x], ans[y][x-1])
    # print(ans)
    print(ans[-1][-1])

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w =  map(int, input().split())
s = [[i for i in list(str(input()))] for i in range(h)]
INF = 10**9
dp = [[INF for _ in range(w)] for _ in range(h)]

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

que = deque()
que.append((0, 0))
is_checked = [[False for _ in range(w)] for _ in range(h)]
while len(que) > 0:
    y, x = que.popleft()
    if y + 1 < h:
        if s[y+1][x] == "#":
            dp[y+1][x] = min(dp[y+1][x], dp[y][x]+1)
        else:
            dp[y+1][x] = min(dp[y+1][x], dp[y][x])
        if not is_checked[y+1][x]:
            que.append((y+1, x))
            is_checked[y+1][x] = True
    if x + 1 < w:
        if s[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+1], dp[y][x])
        if not is_checked[y][x+1]:
            que.append((y, x+1))
            is_checked[y][x+1] = True
print(dp[h-1][w-1])