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())
s = [list(input()) for _ in range(h)]
dp = [[float('inf') for i in range(w)] for j in range(h)]
cnt = 0
total = 0
for i in range(w):
    if s[0][i] == '#':
        cnt += 1
        if total == 0:
            total = 1
    dp[0][i] = total
    if s[0][i] == '.':
        if cnt > 0:
            total += 1
            cnt = 0
total = 0
cnt = 0
for i in range(h):
    if s[i][0] == '#':
        cnt += 1
        if total == 0:
            total = 1
    dp[i][0] = total
    if s[i][0] == '.':
        if cnt > 0:
            total += 1
            cnt = 0
for i in range(1,h):
    for j in range(1,w):
        if s[i][j] == '#':
            if s[i-1][j] == '#' and s[i][j-1] == '#':
                dp[i][j] = min(dp[i-1][j], dp[i][j-1])
            elif s[i-1][j] == '#':
                dp[i][j] = min(dp[i-1][j], dp[i][j-1] + 1)
            elif s[i][j-1] == '#':
                dp[i][j] = min(dp[i][j-1], dp[i-1][j] + 1)
            else:
                dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1)
        else:
            dp[i][j] = min(dp[i-1][j], dp[i][j-1])
print(dp[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),(-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] == c[i][j]:
                dist[ni][nj] =dist[i][j]
                queue.appendleft((ni,nj))
            else:
                dist[ni][nj] =dist[i][j] +1
                queue.append((ni,nj))
                              
            #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)//2+flg)

Submission status: Wrong Answer

Solution code:
from collections import deque
def inputs(): return [int(x) for x in input().split()]
h,w = inputs()
meiro=[list(input()) for _ in range(h)]
visit = [[-1]*w for _ in range(h)]
#if meiro[0][0]=="#":
#  a = 1
#else:
#  a=0
stk = deque([[(0,0),(-1,-1),0]])
while stk:
    v,p,cnt = stk.pop()
    vx,vy = v
    px,py = p
    if w<=vx or h<=vy or (visit[vy][vx]!=-1 and visit[vy][vx]<cnt):
        continue
    if py==-1 and px==-1:
      if meiro[vy][vx]=="#":
        visit[vy][vx]=1
        cnt+=1
      else:
        visit[vy][vx]=0
    elif meiro[vy][vx]=="#"  and meiro[py][px]=="#":
        visit[vy][vx] = cnt
    elif meiro[vy][vx]=="#" and meiro[py][px]!="#":
        visit[vy][vx] = cnt+1
        cnt+=1
    elif meiro[vy][vx]==".":
        visit[vy][vx]=cnt
#    if vy==h-1 and vx==w-1:
#      print(visit[h-1][w-1])
#      exit()
    dx = [0,1]
    dy = [1,0]
    if meiro[vy][vx]=="#":
        if vy+1<h and vx+1<w and meiro[vy+1][vx]=="#" and meiro[vy][vx+1]=="#":
            stk.append([(vx,vy+1),(vx,vy),cnt])
            stk.append([(vx+1,vy),(vx,vy),cnt])
        elif vy+1<h and meiro[vy+1][vx]=="#":
            stk.append([(vx,vy+1),(vx,vy),cnt])
        elif vx+1<w and meiro[vy][vx+1]=="#":
            stk.append([(vx+1,vy),(vx,vy),cnt])
        elif vy+1<h and meiro[vy+1][vx]==".":
            stk.append([(vx,vy+1),(vx,vy),cnt])
        elif vx+1<w and meiro[vy][vx+1]==".":
            stk.append([(vx+1,vy),(vx,vy),cnt])
    elif meiro[vy][vx]==".":
        if vy+1<h and vx+1<w and meiro[vy+1][vx]=="." and meiro[vy][vx+1]==".":
            stk.append([(vx,vy+1),(vx,vy),cnt])
            stk.append([(vx+1,vy),(vx,vy),cnt])
        elif vy+1<h and meiro[vy+1][vx]==".":
            stk.append([(vx,vy+1),(vx,vy),cnt])
        elif vx+1<w and meiro[vy][vx+1]==".":
            stk.append([(vx+1,vy),(vx,vy),cnt])
        elif vy+1<h and meiro[vy+1][vx]=="#":
            stk.append([(vx,vy+1),(vx,vy),cnt])
        elif vx+1<w and meiro[vy][vx+1]=="#":
            stk.append([(vx+1,vy),(vx,vy),cnt])
print(visit[h-1][w-1])

Submission status: Wrong Answer

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

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),(-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] == c[i][j]:
                dist[ni][nj] =dist[i][j]
                queue.appendleft((ni,nj))
            else:
                dist[ni][nj] =dist[i][j] +1
                queue.append((ni,nj))
                              
            #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)//2)           
#print(np.array(dist))
#if dist[gi][gj] <= 2:
#    print('YES')
#else:
#    print('NO')

Submission status: Wrong Answer

Solution code:
import collections
import sys
sys.setrecursionlimit(10**6)

h, w = map(int, input().split())
s = [[" "]*(w+2)]
for _ in range(h):
    s.append([" "] + list(map(str, input())) + [" "])

s.append([" "]*(w+2))

"""
幅優先探索で最短経路を求めて
経路上の「#」の数を数える
"""

r = [[float("inf")]*(w+2) for _ in range(h+2)]
r[1][1] = 0

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

q = collections.deque([[1, 1]])

while len(q) != 0:
    x, y = q.pop()

    for _dx, _dy in zip(dx, dy):
        nx = x + _dx
        ny = y + _dy

        if r[nx][ny] == float("inf") and s[nx][ny] != " ":
            r[nx][ny] = float("inf")
            if s[nx][ny] == "#":
                r[nx][ny] = r[x][y] + 1
            else:
                r[nx][ny] = min(r[nx][ny], r[x][y], r[nx-1][ny], r[nx][ny-1])
            q.appendleft([nx, ny])

"""
for _s in s:
    print(_s)
for _r in r:
    print(_r)
"""

print(r[h][w] + (1 if s[1][1] == "#" else 0))

"""
def dfs(i: int, j: int, c: int) -> int:
    if i == h and j == w:
        return c

    if s[i+1][j] == "#" and s[i][j+1] == "#":
        c += 1

    if r[i+1][j] == 0:
        return dfs(i, j+1, c)
    elif r[i+1][j] <= r[i][j+1]:
        return dfs(i+1, j, c)
    else:
        return dfs(i, j+1, c)

c = 0
if s[1][1] == "#":
    c += 1
if s[h][w] == "#":
    c += 1

print(dfs(1, 1, c))
"""

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w = map(int, input().split())
mass = [input() for _ in range(h)]


inf = 10**9
q = deque([[0, 0]])
depth = [[inf]*w for _ in range(h)]
visited = [[False]*w for _ in range(h)]
if mass[0][0] == "#":
    depth[0][0] = 1
else:
    depth[0][0] = 0

while q:
    x, y = q.popleft()
    visited[x][y] = True
    if x < h-1:
        if visited[x+1][y]:
            pass
        elif mass[x+1][y] == "#":
            if depth[x+1][y] > depth[x][y] + 1:
                depth[x + 1][y] = depth[x][y] + 1
                q.append([x+1, y])

        else:
            if depth[x+1][y] > depth[x][y]:
                depth[x + 1][y] = depth[x][y]
                q.appendleft([x+1, y])

    if y < w-1:
        if visited[x][y+1]:
            pass
        elif mass[x][y+1] == "#":
            if depth[x][y+1] > depth[x][y] + 1:
                depth[x][y+1] = depth[x][y] + 1
                q.append([x, y+1])

        else:
            if depth[x][y+1] > depth[x][y]:
                depth[x][y+1] = depth[x][y]
                q.appendleft([x, y+1])


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

Submission status: Wrong Answer

Solution code:
def minPathSum(grid):
        m = len(grid)
        n = len(grid[0])
        for mi in range(m):
            for ni in range(n):
                options = []
                if mi != 0:
                    options.append(grid[mi-1][ni])
                if ni != 0:
                    options.append(grid[mi][ni-1])
                if options == []:
                    continue
                else:
                    grid[mi][ni] += min(options)
        return grid[-1][-1]

h,w = map(int,input().split())
arr = []
for i in range(h):
	s = input()
	l = []
	for j in s:
		if j == '#':
			l.append(1)
		else:
			l.append(0)
	arr.append(l)

print(minPathSum(arr))

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)

X=[1,0,-1,0]
Y=[0,1,0,-1]

cnt=0
if L[1][1]=="#":
    cnt+=1
    Q=[[1,1]]
    for i in range(10**6):
        if i==len(Q):
            break
        L[Q[i][0]][Q[i][1]]="."
        for j in range(4):
            if L[Q[i][0]+X[j]][Q[i][1]+Y[j]]=="#":
                Q.append([Q[i][0]+X[j],Q[i][1]+Y[j]])
if L[H][W]=="#":
    cnt+=1
    Q=[[H,W]]
    for i in range(10**6):
        if i==len(Q):
            break
        L[Q[i][0]][Q[i][1]]="."
        for j in range(4):
            if L[Q[i][0]+X[j]][Q[i][1]+Y[j]]=="#":
                Q.append([Q[i][0]+X[j],Q[i][1]+Y[j]])
C=[[10000 for i in range(W+2)]for j in range(H+2)]
Q=[(0,1,1)]
#print(L)
import heapq
heapq.heapify(Q)
for i in range(10**6):
    if len(Q)==0:
        break
    d,h,w=Q[0]
    heapq.heappop(Q)
    if C[h][w]==10000:
        C[h][w]=d
        for j in range(2):
            if C[h+X[j]][w+Y[j]]==10000 and L[h+X[j]][w+Y[j]]!="*":
                if L[h+X[j]][w+Y[j]]=="." and L[h][w]=="#":
                    heapq.heappush(Q,(d+1,h+X[j],w+Y[j]))
                else:
                    heapq.heappush(Q,(d,h+X[j],w+Y[j]))
print(C[H][W]+cnt)
#print(cnt)
#print(L)
#print(C)
#print(Q)

Submission status: Wrong Answer

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

dp = {}


# Y, X, （使った）Count
def rec(y, x, c):
    if (y, x, c) in dp:
        return dp[(y, x, c)]
    if y == h - 1 and x == w - 1:
        dp[(y, x, c)] = c
        return c
    if y < 0 or h <= y or x < 0 or w <= x:
        print("は？")
        print(10.0 / 0)
    ret = 10**8
    steps = [0, 1, 0]
    for i in range(2):
        next_x, next_y = x + steps[i], y + steps[i + 1]
        if 0 <= next_y < h and 0 <= next_x < w:
            if table[next_y][next_x] == "#":
                ret = min(rec(next_y, next_x, c + 1), ret)
            else:
                ret = min(rec(next_y, next_x, c), ret)
        else:
            continue
    dp[(y, x, c)] = ret
    return ret


print(rec(0, 0, 0))

Submission status: Wrong Answer

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

field = []
for _ in range(h):
    row = list(input())
    if "\n" in row: row.remove("\n")
    field.append(row)

import numpy as np

dp = np.zeros((h, w))

for i in range(h):
    for j in range(w):
        dp[i, j] = max(dp[i-1, j], dp[i, j-1]) + (field[i][j] == ".")*1

print(int(h+w - 1 - dp[h-1, w-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque


def bfs():
    d = [[float("inf")] * w for i in range(h)]

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

    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0

    while que:
        p = que.popleft()
        if p[0] == gx and p[1] == gy:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]

            if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and d[nx][ny] == float("inf"):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1

    return d[gx][gy]


h, w = map(int, input().split())
maze = [list(input()) for i in range(h)]
sx, sy = 0, 0
gx, gy = h - 1, w - 1

white = 0
for i in range(h):
    for j in range(w):
        if maze[i][j] == ".":
            white += 1

res = bfs()
if 0 < res < float("inf"):
    print(white - res - 1)
else:
    print(-1)

Submission status: Wrong Answer

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

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

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

if arr[0][0] == "#":
  dp[0][0] = 1
  
for h in range(H):
  for w in range(W):
    if h == 0:
      if w != 0:
        a = arr[h][w]
        b = arr[h][w-1]
        if a == b or a == ".":
          dp[h][w] = dp[h][w-1]
        else:
          dp[h][w] = dp[h][w-1] + 1
 
    else:      
      if w == 0:
        a = arr[h][w]
        b = arr[h-1][w]
        if  a == b or a == ".":
          dp[h][w] = dp[h-1][w]
        else:
          dp[h][w] = dp[h-1][w] + 1

      else:
        a = arr[h][w] 
        b = arr[h][w-1]
        c = arr[h-1][w]
        if a == b or a == "." or a == c:
          dp[h][w] = min(dp[h][w-1],dp[h-1][w])
        else:
          dp[h][w] = min(dp[h][w-1] + 1,dp[h-1][w]+1)
    
print(dp[H-1][W-1])

Submission status: Wrong Answer

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

for h in range(H):
    for w in range(W):
        if w>0 and h>0:
            if A[h][w]==".":
                A[h][w]=min(A[h][w-1],A[h-1][w])
            else:
                A[h][w]=1+min(A[h][w-1],A[h-1][w])
        elif w > 0:
            if A[h][w]==".":
                A[h][w]=A[h][w-1]
            else:
                A[h][w]=1+A[h][w-1]
        elif h>0:
            if A[h][w]==".":
                A[h][w]=A[h-1][w]
            else:
                A[h][w]=1+A[h-1][w]
        else:
            if A[h][w]==".":
                A[h][w]=0
            else:
                A[h][w]=1

        
#         print(A)

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def build_grid(H, W, intv, _type, space=True, padding=False):
    # 入力がスペース区切りかどうか
    if space:
        _input = lambda: input().split()
    else:
        _input = lambda: input()
    _list = lambda: list(map(_type, _input()))
    # 余白の有無
    if padding:
        offset = 1
    else:
        offset = 0
    grid = list2d(H+offset*2, W+offset*2, intv)
    for i in range(offset, H+offset):
        row = _list()
        for j in range(offset, W+offset):
            grid[i][j] = row[j-offset]
    return grid

H, W = MAP()
grid = build_grid(H, W, '*', str, space=0, padding=1)

prevh = list2d(H+2, W+2, 0)
prevw = list2d(H+2, W+2, 0)
for i in range(1, H+1):
    for j in range(W, 0, -1):
        if grid[i][j] == '#':
            prevw[i][j] = prevw[i][j+1] + 1
        else:
            prevw[i][j] = 0
for j in range(1, W+1):
    for i in range(H, 0, -1):
        if grid[i][j] == '#':
            prevh[i][j] = prevh[i+1][j] + 1
        else:
            prevh[i][j] = 0

dp = list2d(H+2, W+2, INF)
dp[0][0] = 0
grid[0][0] = grid[0][1] = grid[1][0] = '.'
for i in range(H+1):
    for j in range(W+1):
        if grid[i][j+1] == '.':
            dp[i][j+1] = min(dp[i][j+1], dp[i][j])
        elif grid[i][j+1] == '#':
            nxtw = j+1 + prevw[i][j+1] - 1
            nxth = i + prevh[i][nxtw] - 1
            dp[nxth][nxtw] = min(dp[nxth][nxtw], dp[i][j] + 1)
            nxth = i + prevh[i][j+1] - 1
            nxtw = j+1 + prevw[nxth][j+1] - 1
            dp[nxth][nxtw] = min(dp[nxth][nxtw], dp[i][j] + 1)
        if grid[i+1][j] == '.':
            dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        elif grid[i+1][j] == '#':
            nxtw = j + prevw[i+1][j] - 1
            nxth = i+1 + prevh[i+1][nxtw] - 1
            dp[nxth][nxtw] = min(dp[nxth][nxtw], dp[i][j] + 1)
            nxth = i+1 + prevh[i+1][j] - 1
            nxtw = j + prevw[nxth][j] - 1
            dp[nxth][nxtw] = min(dp[nxth][nxtw], dp[i][j] + 1)
ans = dp[H][W]
print(ans)

Submission status: Wrong Answer

Solution code:
white = 0
black = 1
temp = []
def resolve(board, H, W):
    now = [0, 0]
    while now != [H-1, W-1]:
        now = check(board, now)
    return len(temp)
def check(board, now):
    if now[0] > len(board) -2 or board[now[0]+1][now[1]]==black:
        if now[1] > len(board[0]) -2 or board[now[0]][now[1]+1]==black:
            if now[1] < len(board[0]) -1:
                temp.append([now[0], now[1]+1])
                return [now[0], now[1]+1]
            elif now[0] < len(board) -1:
                temp.append([now[0]+1, now[1]])
                return [now[0]+1, now[1]]
        else:
            return [now[0], now[1]+1]
    else:
        return [now[0]+1, now[1]]
def change(board, r0, c0, r1, c1):
    for r in range(r0, r1 + 1):
        for c in range(c0, c1 + 1):
            board[r][c] = 1 - board[r][c]
def main():
    H, W = map(int, input().split())
    board = [[white for i in range(W)] for j in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                board[i][j] = white
            else:
                board[i][j] = black
    n = resolve(board, H, W)
    print(n)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)
from collections import deque

def main():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    visited = [[10000000] * W for _ in range(H)]
    dh = [0,1]
    dw = [1,0]
    if S[0][0] == '#':
        visited[0][0] = 1
        pre = False
    else:
        visited[0][0] = 0
        pre = True
    
    for h in range(H):
        for w in range(W):
            if h + 1 > H - 1 and w + 1 > W - 1:
                continue
            if S[h][w] == '.':
                if h + 1 > H - 1:
                    if S[h][w + 1] == '.':
                        visited[h][w+1] = visited[h][w]
                    else:
                        visited[h][w+1] = visited[h][w] + 1
                elif w + 1 > W - 1:
                    if S[h+1][w] == '.':
                        visited[h+1][w] = visited[h][w]
                    else:
                        visited[h+1][w] = visited[h][w] + 1
                else:
                    if S[h+1][w] == '.':
                        visited[h+1][w] = visited[h][w]
                    else:
                        visited[h+1][w] = visited[h][w] + 1

                    if S[h][w + 1] == '.':
                        visited[h][w+1] = visited[h][w]
                    else:
                        visited[h][w+1] = visited[h][w] + 1

            else:
                if h + 1 > H - 1:
                    visited[h][w+1] = visited[h][w]
                elif w + 1 > W - 1:
                    visited[h+1][w] = visited[h][w]
                else:
                    visited[h+1][w] = visited[h][w]
                    visited[h][w+1] = visited[h][w]

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
H,W = [int(i.strip()) for i in input().split(' ')]

MAP = [list(input())for _ in range(H)]

F = [[0] * 10] * 10

arrive = {}
if MAP[0][0] == '#':
    arrive[(0,0)] = 1
else:
    arrive[(0,0)] = 0

while True:
    A = {}
    for p , v in arrive.items():
        if p[0]+1 < H:
            if MAP[p[0]+1][p[1]] == '.':
                if not (p[0]+1 , p[1]) in A:
                    A[(p[0]+1 , p[1])] = v 
                else:
                    t = A[(p[0]+1 , p[1])]
                    A[(p[0]+1 , p[1])] = v if v < t else t
            else:
                if not (p[0]+1 , p[1]) in A:
                    A[(p[0]+1, p[1])] = v+1
                else:
                    t = A[(p[0]+1 , p[1])]
                    A[(p[0]+1 , p[1])] = v + 1 if v + 1 < t else t

        if p[1] + 1 < W:
            if MAP[p[0]][p[1] + 1] == '.':
                if not ( p[0] , p[1] + 1) in A:
                    A[( p[0] , p[1] + 1)] = v 
                else:
                    t = A[( p[0] , p[1] + 1)]
                    A[( p[0] , p[1] + 1)] = v if v < t else t
            else:
                if not ( p[0] , p[1] + 1) in A:
                    A[( p[0] , p[1] + 1)] = v+1
                else:
                    t = A[( p[0] , p[1] + 1)]
                    A[( p[0] , p[1] + 1)] = v + 1 if v + 1 < t else t

    if (H-1,W-1) in A:
        print(A[(H-1,W-1)])
        exit()

    else:
        arrive = A.copy()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    h,w = LI()
    s = SR(h)
    b = [[inf] * w for i in range(h)]
    q = [(s[0][0] == "#", (0, 0))]
    b[0][0] = s[0][0] == "#"
    while q:
        p, xy = heappop(q)
        y, x = xy
        for mx, my in [(0, 1), (0, -1), (-1, 0), (1, 0)]:
            txm = mx + x
            tym = my + y
            xm = txm
            ym = tym
            if 0 <= xm < w and 0 <= ym < h:
                for xm in range(xm, w):
                    if s[ym][xm] == ".":
                        if b[ym][xm] > p:
                            b[ym][xm] = p
                            heappush(q, (p, (ym, xm)))
                        else:
                            break
                    else:
                        break
                xm = txm
                ym = tym
                for xm in range(xm, -1, -1):
                    if s[ym][xm] == ".":
                        if b[ym][xm] > p:
                            b[ym][xm] = p
                            heappush(q, (p, (ym, xm)))
                        else:
                            break
                    else:
                        break
                xm = txm
                ym = tym
                for ym in range(ym, h):
                    if s[ym][xm] == ".":
                        if b[ym][xm] > p:
                            b[ym][xm] = p
                            heappush(q, (p, (ym, xm)))
                        else:
                            break
                    else:
                        break
                xm = txm
                ym = tym
                for ym in range(ym, -1, -1):
                    if s[ym][xm] == ".":
                        if b[ym][xm] > p:
                            b[ym][xm] = p
                            heappush(q, (p, (ym, xm)))
                        else:
                            break
                    else:
                        break
                xm = txm
                ym = tym
                for xm in range(xm, w):
                    if s[ym][xm] == "#":
                        if b[ym][xm] > p + 1:
                            b[ym][xm] = p + 1
                            heappush(q, (p + 1, (ym, xm)))
                    else:
                        break
                xm = txm
                ym = tym
                for ym in range(ym, h):
                    if s[ym][xm] == "#":
                        if b[ym][xm] > p + 1:
                            b[ym][xm] = p + 1
                            heappush(q, (p + 1, (ym, xm)))
                    else:
                        break
                xm = txm
                ym = tym
                for xm in range(xm, -1, -1):
                    if s[ym][xm] == "#":
                        if b[ym][xm] > p + 1:
                            b[ym][xm] = p + 1
                            heappush(q, (p + 1, (ym, xm)))
                    else:
                        break
                xm = txm
                ym = tym
                for ym in range(ym, -1, -1):
                    if s[ym][xm] == "#":
                        if b[ym][xm] > p + 1:
                            b[ym][xm] = p + 1
                            heappush(q, (p + 1, (ym, xm)))
                    else:
                        break


    print(b[-1][-1] if b[-1][-1] else 0)

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys


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


def main():
    H, W = map(int, input().split())

    S = [list(input()) for _ in range(H)]
    # print(S)
    check = [[10 ** 9 for i in range(W)] for j in range(H)]
    if S[0][0] == "#":
        check[0][0] = 1
    else:
        check[0][0] = 0
    for i in range(1, W):
        left = check[0][i - 1]
        if S[0][i] == "#":
            check[0][i] = left + 1
        else:
            check[0][i] = left
    for i in range(1, H):
        up = check[i - 1][0]
        if S[i][0] == "#":
            check[i][0] = up + 1
        else:
            check[i][0] = up
    for i in range(1, H):
        for j in range(1, W):
            # print(S[i][j])
            left = check[i][j - 1]
            up = check[i - 1][j]
            if S[i][j] == "#":
                check[i][j] = min(left, up) + 1
            else:
                check[i][j] = min(left, up)
    # print(check)
    print(check[H - 1][W - 1])


main()

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),(-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] == c[i][j]:
                dist[ni][nj] =dist[i][j]
                queue.appendleft((ni,nj))
            else:
                dist[ni][nj] =dist[i][j] +1
                queue.append((ni,nj))
                              
            #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]//2+1*(flg))           
#print(np.array(dist))
#if dist[gi][gj] <= 2:
#    print('YES')
#else:
#    print('NO')

Submission status: Wrong Answer

Solution code:
h,w= map(int, input().split())
import numpy as np
x=np.zeros((h,w))
y=np.ones((h,w))
for i in range(h):
    s=input()
    for j in range(w):
        if i==0:
            if j==0:
                if s[j]=='.':
                    x[i][j]=0
                    y[i][j]=2
                else:
                    x[i][j]=1
                    y[i][j]=1
            else:
                if s[j]=='.':
                    x[i][j]=x[i][j-1]
                    y[i][j]=y[i][j-1]+1
                else:
                    x[i][j]=x[i][j-1]+1
                    y[i][j]=y[i][j-1]
        else:
            if j==0:
                if s[j]=='.':
                    x[i][j]=x[i-1][j]
                    y[i][j]=y[i-1][j]+1
                else:
                    x[i][j]=x[i-1][j]+1
                    y[i][j]=y[i-1][j]
            else:
                if s[j]=='.':
                    x[i][j]=min(x[i-1][j],x[i][j-1])
                    y[i][j]=min(y[i-1][j],y[i][j-1])+1
                else:
                    x[i][j]=min(x[i-1][j],x[i][j-1])+1
                    y[i][j]=min(y[i-1][j],y[i][j-1])
print(int(min((x[h-1][w-1]),(y[h-1][w-1]))))

Submission status: Wrong Answer

Solution code:
H, W = map(int,input().split())
l = [list(input()) for _ in range(H)]
#2D DP
dp = [[0] * W for _ in range(H)]
if l[0][0] == '#':
  dp[0][0] = 1 
for j in range(W - 1):
    dp[0][j + 1] = dp[0][j]
for i in range(1, H):
  dp[i][0] = dp[i - 1][0]
  for j in range(W - 1):
    dp[i][j + 1] = min(dp[i][j], dp[i - 1][j])
    if l[i][j + 1] == '#':
      dp[i][j + 1] += 1 
print(dp[H - 1][W - 1])