Problem:


Score : 400 points

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
Each square is painted black or white.

The grid is said to be good if and only if the following condition is satisfied:

From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.

Note that (1, 1) and (H, W) must be white if the grid is good.

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

H W
s_{11} s_{12} \cdots s_{1W}
s_{21} s_{22} \cdots s_{2W}
 \vdots
s_{H1} s_{H2} \cdots s_{HW}

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

4 4
..##
#...
###.
###.

Sample Output 3

0

No operation may be needed.

Sample Input 4

5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

Sample Output 4

4



Sample input:
3 3
.##
.#.
##.


Sample output:
1

Submission status: Wrong Answer

Solution code:
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix

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

u = []
v = []
c = []

d = [[1, 0], [0, 1]]

for i in range(1, h + 1):
    for j in range(1, w + 1):
        for dh, dw in d:
            nh = i + dh
            nw = j + dw
            u.append((w + 2) * i + j)
            v.append((w + 2) * nh + nw)
            if s[nh][nw] == "#":
                c.append(1)
            else:
                c.append(0)

start = (w + 2) * 1 + 1
goal = (w + 2) * h + w
n = (w + 2) * (h + 2)
graph = csr_matrix((c, (u, v)), shape=(n, n))
dist = dijkstra(graph, indices=start).astype(int)
ans = dist[goal]
if s[1][1] == "#":
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
info = [input() for i in range(h)]
INF = 10 ** 5

dp = [[INF] * w for i in range(h)]
dp[0][0] =  0

for i in range(h):
    for j in range(w):
        for ii, jj in [(i, j + 1), (i + 1, j)] :
            if not(0 <= ii < h and 0 <= jj < w):
                continue
            if info[i][j] != info[ii][jj] and info[ii][jj] == "#":
                dp[ii][jj] = min(dp[i][j] + 1, dp[ii][jj])
            else:
                dp[ii][jj] = min(dp[i][j], dp[ii][jj])
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
INF = 100000

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

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

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

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

Submission status: Wrong Answer

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


def solve(*args: str) -> str:
    h, w = map(int, args[0].split())
    black = [[True if s == '#' else False for s in S] for S in args[1:]]

    inf = 10**12
    counts = [[inf]*w for _ in range(h)]
    Q = deque([(0, 0, 0)])
    while Q:
        x, y, n = Q.popleft()
        if h <= y or w <= x:
            continue

        if black[y][x]:
            n += 1

        if n < counts[y][x]:
            counts[y][x] = n
            for dx, dy in [(1, 0), (0, 1)]:
                Q.append((x+dx, y+dy, n))

    return str(counts[-1][-1])


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
grid = [input() for _ in range(H)]
dp = [[10**9] * (W+1) for _ in range(H+1)]

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

Submission status: Wrong Answer

Solution code:
def main():
    from heapq import heappush, heappop

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

    h = []
    heappush(h, (int(s[0][0] == '#'), 0, 0))
    cost = [[H * W] * W for _ in range(H)]
    cost[0][0] = int(s[0][0] == '#')

    while h:
        cnt, r, c = heappop(h)
        if cnt > cost[r][c]:
            continue

        if r == H - 1 and c == W - 1:
            print(cnt)
            return

        if r < H - 1:
            if s[r + 1][c] == '#':
                if cnt + 1 < cost[r + 1][c]:
                    heappush(h, (cnt + 1, r + 1, c))
                    cost[r + 1][c] = cnt + 1
            else:
                if cnt < cost[r + 1][c]:
                    heappush(h, (cnt, r + 1, c))
                    cost[r + 1][c] = cnt
        if c < W - 1:
            if s[r][c + 1] == '#':
                if cnt + 1 < cost[r][c + 1]:
                    heappush(h, (cnt + 1, r, c + 1))
                    cost[r][c + 1] = cnt + 1
            else:
                if cnt < cost[r][c + 1]:
                    heappush(h, (cnt, r, c + 1))
                    cost[r][c + 1] = cnt


if __name__ == '__main__':
    main()

# import sys
#
# input = sys.stdin.readline
# sys.setrecursionlimit(10 ** 7)
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Wrong Answer

Solution code:
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix, lil_matrix

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

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

def co_num(h, w):
    return h * W + w

row = []
col = []
dist = []
for h in range(H):
    for w in range(W):
        if h != 0:
            row.append(co_num(h, w))
            col.append(co_num(h-1, w))
            dist.append(0 if s[h-1][w] == s[h][w] else 1)
        if w != 0:
            row.append(co_num(h, w))
            col.append(co_num(h, w-1))
            dist.append(0 if s[h][w-1] == s[h][w] else 1)
        if h != H-1:
            row.append(co_num(h, w))
            col.append(co_num(h+1, w))
            dist.append(0 if s[h+1][w] == s[h][w] else 1)
        if w != W-1:
            row.append(co_num(h, w))
            col.append(co_num(h, w+1))
            dist.append(0 if s[h][w+1] == s[h][w] else 1)

d = int(dijkstra(csr_matrix((dist, (row, col)), shape=[H*W, H*W]), directed=True, indices=0)[H*W-1])

if s[0][0] == '.' and s[-1][-1] == '.':
    print(d // 2)
elif s[0][0] == '#' and s[-1][-1] == '#':
    print(d // 2 + 1)
else:
    print(d // 2 + 1)

Submission status: Wrong Answer

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

dp = [[100000] * W for _ in range(H)]
if s[0][0] == '#':
    dp[0][0] = 1
else:
    dp[0][0] = 0

q = [(0, 0)]
while q:
    nq = []
    while q:
        y, x = q.pop()
        if y < H - 1:
            t = dp[y][x]
            if s[y + 1][x] == '#':
                t += 1
            if dp[y + 1][x] > t:
                dp[y + 1][x] = t
                nq.append((y + 1, x))
        if x < W - 1:
            t = dp[y][x]
            if s[y][x + 1] == '#':
                t += 1
            if dp[y][x + 1] > t:
                dp[y][x + 1] = t
                nq.append((y, x + 1))
    q = nq

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

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), (0, -1), (-1, 0))
sy = 0
sx = 0
ty = h-1
tx = w-1
st = 0
if c[0][0] == '#':
    st = 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] + st)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
s = [list(input()) for _ in range(h)]
dp = [[0 for _ in range(w)] for i 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 i in range(1,h):
  if s[i][0] == "#":
    dp[i][0] = dp[i-1][0]+1
  else:
    dp[i][0] = dp[i-1][0]
for i in range(1,h):
  for j in range(1,w):
    if s[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])
print(dp[-1][-1])

Submission status: Wrong Answer

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


# dp[y][x] : s[y][x]までの#の個数の最小値
dp = [[1 * 50 for i in range(W)] for i in range(H)]

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

for y in range(H):
    for x in range(W):
        a = 0 if s[y][x] == '.' else 1

        if y == 0 and x == 0:
            val = 0
        elif y == 0:
            val = dp[y][x - 1]
        elif x == 0:
            val = dp[y - 1][x]
        else:
            val = min(dp[y - 1][x], dp[y][x - 1])
        dp[y][x] = val + a
        # dp[y + 1][x] = min(dp[y + 1][x], dp[y][x])
        # dp[y][x + 1] = min(dp[y][x + 1], dp[y][x])

print(dp[-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]
for i in range(1, h):
    dp[i][0] = dp[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][j], dp[i][j-1]+S[i][j])
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
place = []
for i in range(H):
    S = input()
    place.append(S)
ans_list = []
for i in range(H):
    Z = [0] * W
    ans_list.append(Z)
for i in range(H):
    if i == 0:
        count = 0
        for j in range(W):
            if place[0][j] == "#":
                count += 1
            ans_list[0][j] = count
    else:
        count = ans_list[i-1][0]
        for j in range(W):
            chk = 0
            if place[i][j] == "#":
                count += 1
                chk = 1
            ans_list[i][j] = min(count,ans_list[i-1][j]+chk)
print(ans_list[H-1][W-1])

Submission status: Wrong Answer

Solution code:
H, W = [int(i) for i in input().strip().split(" ")]
tiles = []
for i in range(H):
    tiles.append([i for i in input().strip()])

tile = {"#":1, ".":0}
count = []
for i in range(H):
    count.append([])
    for j in range(W):
        count[i].append(0)

count[0][0] = tile[tiles[0][0]]

for y in range(H):
    for x in range(W):
        if y == 0:
            if x == 0:
                continue
            count[y][x] = count[y][x-1] + tile[tiles[y][x]]
        else:
            if x == 0:
                count[y][x] = count[y-1][x] + tile[tiles[y][x]]
            else:
                count[y][x] = min(count[y-1][x], count[y][x-1]) + tile[tiles[y][x]]

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

Submission status: Wrong Answer

Solution code:
print('atode!!!')

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    H, W = map(int, input().split())
    grid = []
    for _ in range(H):
        grid.append(input().rstrip('\n'))

    d = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    que = deque()
    que.append((0, 0))
    seen = [[-1] * W for _ in range(H)]
    if grid[0][0] == '#':
        seen[0][0] = 1
    else:
        seen[0][0] = 0
    while que:
        h, w = que.popleft()
        for dh, dw in d:
            h_new, w_new = h+dh, w+dw
            if 0 <= h_new < H and 0 <= w_new < W:
                if seen[h_new][w_new] == -1:
                    if grid[h_new][w_new] == grid[h][w]:
                        seen[h_new][w_new] = seen[h][w]
                        que.appendleft((h_new, w_new))
                    else:
                        if grid[h_new][w_new] == '#':
                            seen[h_new][w_new] = seen[h][w] + 1
                        else:
                            seen[h_new][w_new] = seen[h][w]
                        que.append((h_new, w_new))
    print(seen[-1][-1])
    #[print(seen[h]) for h in range(H)]


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

h, w = map(int, input().split())
mp = [list(input()) for i in range(h)]
dp = [[0]*105 for _ in range(105)]
if mp[0][0]=='#':
    dp[0][0]+=1
for i in range(w-1):
    plus=0
    if mp[0][i+1]=='#':
        plus=1
    dp[0][i+1] = dp[0][i]+plus
for i in range(h-1):
    plus=0
    if mp[i+1][0]=='#':
        plus=1
    dp[i+1][0] = dp[i][0]+plus

for i in range(1,h):
    for j in range(1,w):
        plus=0
        if mp[i][j]=='#':
            plus=1
        dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + plus

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
a=[]
dp=[]
for i in range(n):
	a.append(input())
	dp.append([100000]*m)

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

for i in range(1,n):
	for j in range(1,m):
		if(a[i][j]=='#'):
			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[-1][-1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

H,W = LI()
s = [input() for _ in range(H)]
dx,dy = [1,0],[0,1]
dp = [[inf]*W for _ in range(H)]
if s[0][0] == '#':
    dp[0][0] = 1
else:
    dp[0][0] = 0

h = []
heappush(h, (dp[0][0],0,0))
while h:
    d,y,x = heappop(h)
    for i in range(2):
        nx,ny = x+dx[i], y+dy[i]
        if 0 <= nx < W and 0 <= ny < H:
            if s[ny][nx] == '#' and dp[ny][nx] > d + 1:
                dp[ny][nx] = d + 1
                heappush(h,(dp[ny][nx],ny,nx))
            elif s[ny][nx] == '.' and dp[ny][nx] > d:
                dp[ny][nx] = d
                heappush(h, (dp[ny][nx],ny,nx))
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w = map(int,input().split())
s = []
for i in range(h):
    s0 = list(input())
    s.append(s0)
d = deque()
rl = [[h*w for i in range(w)] for i in range(h)]
if s[0][0] == ".":
    d.append([0,0])
    rl[0][0] = 0
else:
    d.append([0,0])
    rl[0][0] = 1
while len(d):
    a,b = d.popleft()
    if a == h-1:
        if b < w-1:
            if rl[a][b+1] == h*w:
                if s[a][b+1] == "#":
                    rl[a][b+1] = rl[a][b] + 1
                else:
                    rl[a][b+1] = rl[a][b]
                d.append([a,b+1])
            else:
                if s[a][b+1] == "#":
                    rl[a][b+1]= min(rl[a][b+1],rl[a][b] + 1)
                else:
                    rl[a][b+1] = min(rl[a][b+1],rl[a][b])
    else:
        if b == w-1:
            if rl[a+1][b] == h*w:
                if s[a+1][b] == "#":
                    rl[a+1][b] = rl[a][b] + 1
                else:
                    rl[a+1][b] = rl[a][b]
                d.append([a+1,b])
            else:
                if s[a+1][b] == "#":
                    rl[a+1][b]= min(rl[a+1][b],rl[a][b] + 1)
                else:
                    rl[a+1][b] = min(rl[a+1][b],rl[a][b])
        else:
            if rl[a][b+1] == h*w:
                if s[a][b+1] == "#":
                    rl[a][b+1] = rl[a][b] + 1
                else:
                    rl[a][b+1] = rl[a][b]
                d.append([a,b+1])
            else:
                if s[a][b+1] == "#":
                    rl[a][b+1]= min(rl[a][b+1],rl[a][b] + 1)
                else:
                    rl[a][b+1] = min(rl[a][b+1],rl[a][b])
            if rl[a+1][b] == h*w:
                if s[a+1][b] == "#":
                    rl[a+1][b] = rl[a][b] + 1
                else:
                    rl[a+1][b] = rl[a][b]
                d.append([a+1,b])
            else:
                if s[a+1][b] == "#":
                    rl[a+1][b]= min(rl[a+1][b],rl[a][b] + 1)
                else:
                    rl[a+1][b] = min(rl[a+1][b],rl[a][b])
print(rl[h-1][w-1])

Submission status: Wrong Answer

Solution code:
# i, jまでの#の数
h,w = map(int,input().split())
sl = []
for _ in range(h):
    s = list(input())
    sl.append(s)

dp = [[float("inf") for i in range(w)] for j in range(h)]
if sl[0][0] == ".":
    dp[0][0] = 0
else:
    dp[0][0] = 1

for i in range(h):
    for j in range(w):
        if j != w-1:
            if sl[i][j+1] == "#":
                dp[i][j+1] = min(dp[i][j] + 1, dp[i][j+1])
            else:
                dp[i][j + 1] = min(dp[i][j], dp[i][j + 1])
        if i != h-1:
            if sl[i+1][j] == "#":
                dp[i+1][j] = min(dp[i][j] + 1, dp[i+1][j])
            else:
                dp[i+1][j] = min(dp[i][j], dp[i+1][j])

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

Submission status: Wrong Answer

Solution code:

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

def main():
    mod=10**9+7
    H,W=MI()
    s=[[] for _ in range(H+2)]
    s[0]="#"*(W+2)
    for i in range(H):
        s[i+1]="#"+input()+"#"
    s[-1]="#"*(W+2)
            
            
    dx=[0,1]
    dy=[1,0]
    
    inf=10**7
    #dp[i][j]はますi,jにたどり着くときの最小コスト
    dp=[[inf]*(W+2) for _ in range(H+2)]
    
    if s[1][1]==".":
        dp[1][1]=0
    else:
        dp[1][1]=1
        
    for i in range(1,H+1):
        for j in range(1,W+1):
            for k in range(2):
                ii=i+dx[k]
                jj=j+dy[k]
                if s[ii][jj]==".":
                    a=0
                else:
                    a=1
                dp[ii][jj]=min(dp[ii][jj],dp[i][j]+a)
    
    print(dp[H][W])
    

main()

Submission status: Wrong Answer

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

result = [[10**5 for i in range(w)] for j in range(h)]
result[0][0] = 0
for i in range(h):
    for j in range(w):
        if i > 0:
            result[i][j] = result[i-1][j]
        if j > 0:
            result[i][j] = min(result[i][j], result[i][j-1])
        result[i][j] += (sw[i][j]=="#")

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

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    H, W = map(int, input().split())
    s = [input() for _ in range(H)]

    q = deque()
    if s[0][0] == '#':
        q.append((0, 0, 1))
    else:
        q.append((0, 0, 0))

    cost = [[H * W] * W for _ in range(H)]
    cost[0][0] = (s[0][0] == '#')

    while q:
        r, c, cnt = q.popleft()
        if cnt > cost[r][c]:
            continue

        if r == H - 1 and c == W - 1:
            print(cnt)
            return

        if r < H - 1:
            if s[r + 1][c] == '#':
                if cnt + 1 < cost[r + 1][c]:
                    q.append((r + 1, c, cnt + 1))
                    cost[r + 1][c] = cnt + 1
            else:
                if cnt < cost[r + 1][c]:
                    q.appendleft((r + 1, c, cnt))
                    cost[r + 1][c] = cnt
        if c < W - 1:
            if s[r][c + 1] == '#':
                if cnt + 1 < cost[r][c + 1]:
                    q.append((r, c + 1, cnt + 1))
                    cost[r][c + 1] = cnt + 1
            else:
                if cnt < cost[r][c + 1]:
                    q.appendleft((r, c + 1, cnt))
                    cost[r][c + 1] = cnt


if __name__ == '__main__':
    main()

# import sys
#
# input = sys.stdin.readline
# sys.setrecursionlimit(10 ** 7)
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int, input().split())
X = [[1 if a == "#" else 0 for a in input()] for _ in range(H)]

def BFS(i0=0):
    Q = deque([i0])
    D = [-1] * (H * W)
    D[i0] = X[0][0]
    while Q:
        x = Q.popleft()
        i, j = x // W, x % W
        for di, dj in ((0, 1), (0, -1), (1, 0), (-1, 0)):
            ni, nj = i + di, j + dj
            if 0 <= ni < H and 0 <= nj < W:
                y = ni * W + nj
                if X[ni][nj] == X[i][j]:
                    if D[y] == -1:
                        D[y] = D[x]
                        Q.appendleft(y)
                else:
                    if D[y] == -1:
                        D[y] = D[x] + 1
                        Q.append(y)
                        
    return D

print(BFS(0)[-1] + 1 >> 1)

Submission status: Wrong Answer

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

S = [""] * H

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

dist = [[1000000 for i in range(W)] for j in range(H)]

dist[0][0] = 0 if S[0][0] == "." else 1

for i in range(H):
    for j in range(W):
        if i > 0:
            if S[i][j] == "#":
                dist[i][j] = min(dist[i][j], dist[i - 1][j] + 1)
            else:
                dist[i][j] = min(dist[i][j], dist[i - 1][j])
        if j > 0:
            if S[i][j] == "#":
                dist[i][j] = min(dist[i][j], dist[i][j - 1] + 1)
            else:
                dist[i][j] = min(dist[i][j], dist[i][j - 1])

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

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l=[list(input()) for i in range(h)]
ans=[[0 for i in range(w)] for j in range(h)]
if l[0][0]=='#':
  ans[0][0]=1
for i in range(h):
  for j in range(w):
    if i!=0 or j!=0:
      if i!=0 and j!=0:
        ans[i][j]=min(ans[i-1][j],ans[i][j-1])
      elif i==0 and j!=0:
        ans[i][j]=ans[i][j-1]
      else:
        ans[i][j]=ans[i-1][j]
      if l[i][j]=='#':
        ans[i][j]+=1
print(ans[h-1][w-1])

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), (1, 0), (-1, 0), (0, -1)]:
            mx += x
            my += y
            if 0 <= mx < w and 0 <= my < h:
                if s[my][mx] == ".":
                    if b[my][mx] > p:
                        b[my][mx] = p
                        heappush(q, (p, (my, mx)))
                else:
                    if b[my][mx] > p + 1:
                        b[my][mx] = p + 1
                        heappush(q, (p + 1, (my, mx)))
    print(b[-1][-1] if b[-1][-1] else 0)

    return


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

Submission status: Wrong Answer

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

H, W = map(int, input().split())
s = [input() for _ in range(H)]
queue = deque()
queue.append((0, 0))
dist = [[-1] * W for _ in range(H)]
dist[0][0] = 1 if s[0][0] == '#' else 0
while queue:
    i, j = queue.popleft()
    for di, dj in [(1, 0), (0, 1)]:
        ni, nj = i+di, j+dj
        if 0 <= ni < H and 0 <= nj < W and dist[ni][nj] == -1:
            if s[i][j] == '.' and s[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[H-1][W-1])

Submission status: Wrong Answer

Solution code:
#from functools import lru_cache
#memo = lru_cache(None)
#from sys import setrecursionlimit as srl
#srl(2 * 10**5)
from collections import deque

rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())

def solve(R, C, A):
    if R==C==1:
        if A[0][0]:
            return 0
        return 1
    if R==1 or C==1:
        possible = True
        for r, row in enumerate(A):
            for c, val in enumerate(row):
                if not val:
                    possible = False
        return 0 if possible else 1
    # bfs with max of 1
    queue = deque([(0, 0, 0 if A[0][0] else 1)])
    seen = {queue[0]}
    def neighbors(r, c, d):
        for nr, nc in ((r-1, c), (r, c-1), (r+1, c), (r, c+1)):
            if 0 <= nr<R and 0<=nc<C:
                if A[nr][nc]:
                    yield nr, nc, d
                elif d + 1 <= 1:
                    yield nr, nc, d + 1

    while queue:
        r, c, d = queue.popleft()
        if r == R-1 and c == C-1:
            return d
        for nei in neighbors(r, c, d):
            if nei not in seen:
                seen.add(nei)
                if nei[2] > d:
                    queue.append(nei)
                else:
                    queue.appendleft(nei)
    return 2


for _ in xrange(1):
    R, C = rrm()
    A = []
    for _ in xrange(R):
        row = [c=='.' for c in rr()]
        A.append(row)

print solve(R, C, A)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)
h, w = map(int, input().split())
mapping = [0]*h
for i in range(h):
  mapping[i] = input()
ans = [[-1]*w for i in range(h)]
def dp(i, j):
  if ans[i][j] == -1:
    if i == 0 and j == 0:
      if mapping[i][j] == ".":
        ans[i][j] = 0
        return 0
      else:
        ans[i][j] = 1
        return 1
    if i == 0:
      if mapping[i][j] == ".":
        ans[i][j] = dp(i, j-1)
        return ans[i][j]
      else:
        ans[i][j] = dp(i, j-1)+1
        return ans[i][j]
    if j == 0:
      if mapping[i][j] == ".":
        ans[i][j] = dp(i-1, j)
        return ans[i][j]
      else:
        ans[i][j] = dp(i-1, j)+1
        return ans[i][j]
    if mapping[i][j] == ".":
      ans[i][j] = min([dp(i-1, j), dp(i, j-1)])
      return ans[i][j]
    else:
      ans[i][j] = min([dp(i-1, j), dp(i, j-1)])+1
      return ans[i][j]
  else:
    return ans[i][j]
answer = dp(h-1, w-1)
print(answer)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
x=[[0]*w for _ in range(h)]
s=[input() for _ in range(h)]

if s[0][0]=="#": x[0][0]=1
for j in range(1,w):
    if s[0][j]=="#": x[0][j]=x[0][j-1]+1
    else: x[0][j]=x[0][j-1]


for i in range(1,h):
    if s[i][0]=="#": x[i][0]=x[i-1][0]+1
    else: x[i][0]=x[i-1][0]

for i in range(1,h):
    for j in range(1,w):
        if s[i][j]=="#": x[i][j]=min(x[i-1][j],x[i][j-1])+1
        else: x[i][j]=min(x[i-1][j],x[i][j-1])

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

Submission status: Wrong Answer

Solution code:
from collections import deque
t,*g=open(0)
f=g[0][0]=="#"
h,w=map(int,t.split())
vis=[[-1]*w for _ in range(h)]
vis[0][0]=0
q=deque([(0,0)])
while q and vis[-1][-1]<0:
	cy,cx=q.popleft()
	for dy,dx in ((0,1),(1,0)):
		ny,nx=cy+dy,cx+dx
		if not(0<=ny<h and 0<=nx<w) or vis[ny][nx]>=0:
			continue
		if g[ny][nx]==".":
			vis[ny][nx]=vis[cy][cx]
			q.appendleft((ny,nx))
		else:
			vis[ny][nx]=vis[cy][cx] + 1
			q.append((ny, nx))
print(vis[-1][-1]+f)

Submission status: Wrong Answer

Solution code:
#list(map(int,input().split()))
n,m=map(int,input().split())
it=[[0 for i in range(m)] for j in range(n)]
for i in range(n):
    j=input()
    for k in range(m):
        if j[k]=="#":
            it[i][k]=0
        else:
            it[i][k]=1
dp=[[0 for i in range(m)] for j in range(n)]
for i in range(n):
    for j in range(m):
        if it[i][j]==0:
            dp[i][j]=1
        if(i>0 or j>0):
            if(i==0):
                dp[i][j]+=dp[i][j-1]
            elif j==0:
                dp[i][j]+=dp[i-1][j]
            else:
                dp[i][j]+=min(dp[i-1][j],dp[i][j-1])
print(dp[n-1][m-1])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[]
for i in range(h):
	ss=input()
	s.append(ss)
inf=10**4
dp=[[inf]*w for _ in range(h)]
if s[0][0]=="#":
	dp[0][0]=1
else:
	dp[0][0]=0
for i in range(w-1):
	if s[0][i+1]==".":
		dp[0][i+1]=dp[0][i]
	else:
		dp[0][i+1]=dp[0][i]+1
for i in range(1,h):
	if s[i][0]==".":
		dp[i][0]=dp[i-1][0]
	else:
		dp[i][0]=dp[i-1][0]+1
	for j in range(1,w):
		if s[i][j]==".":
			dp[i][j]=min(dp[i][j-1],dp[i-1][j])
		else:
			dp[i][j]=min(dp[i][j-1],dp[i-1][j])+1
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:

R, C = list(map(int, input().split()))
grid = []
for r in range(R):
    grid.append(list(input().rstrip()))

from functools import lru_cache
@lru_cache(maxsize=None)
def minDist(r, c):
    if r == -1:
        return float('inf')
    if c == -1:
        return float('inf')
    color = int(grid[r][c] == '#')
    if r == 0 and c == 0:
        return color
    return color + min(minDist(r - 1, c), minDist(r, c - 1))

print(minDist(R - 1, C - 1))

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w = map(int, input().split())
S = [input() for _ in range(h)]
stack = deque()
seen = [[False]*w for _ in range(h)]
stack.append((int(S[0][0]=="#"), 0, 0))
seen[0][0] = True
while stack:
  cnt, y, x = stack.pop()
  if y==h-1 and x==w-1:
    print(cnt)
    break
  if y+1<h:
    if not seen[y+1][x]:
      seen[y+1][x] = True
      if S[y+1][x] == "#":
        stack.appendleft((cnt+1, y+1, x))
      else:
        stack.append((cnt, y+1, x))
  if x+1 < w:
    if not seen[y][x+1]:
      seen[y][x+1] = True
      if S[y][x+1] == "#":
        stack.appendleft((cnt+1, y, x+1))
      else:
        stack.append((cnt, y, x+1))