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:
# input
H, W = [int(_) for _ in input().split(' ')]
cheese = [[float(1) if a=='.' else float('inf') for a in input()] for _ in range(H)]
#print(H,W)
#print(cheese)
START = [0,0]
START.append(0)
GOAL = [H-1, W-1]

#process
point = []
point.insert(0, START)
minimum = float('inf')

while True:
    #print(point)
    # break condition
    if not minimum==float('inf'):
        length = len(point)
        c = 0
        for i in range(length):
            if point[i-c][2]>=minimum:
                #print('nyan')
                point.pop(i-c)
                c += 1
    # que has element or not
    if len(point)==0:
        break
    h, w, count = point.pop()
    if h==GOAL[0] and w==GOAL[1]:
        if minimum>count:
            minimum = count
        if count==0:
            break

    # explore
    for i in range(0,2):
        h_, w_, count_ = h + [1,0][i], w + [0,1][i], count
        if h_>=0 and h_<H and w_>=0 and w_<W:
            if cheese[h_][w_]==float('inf'):
                count_+=1
            point.insert(0, [h_,w_,count_])

print(minimum)

Submission status: Wrong Answer

Solution code:

H, W = map(int, input().split())
s = [[0] * W]
minus = 0
tmp = [list(input()) for _ in range(H)]
for i in range(H):
    s.append(tmp[i])
if s[1][0] == '#':
    minus = 1
    for i in range(W):
        s[0][i] = 1
else:
    s[0][0] = 0


for i in range(1, H + 1):
    for j in range(W):
        if s[i][j] == '#':
            if j != 0 and i == 1:
                s[i][j] = s[i][j - 1] + 1
            elif j != 0:
                s[i][j] = min(s[i - 1][j] + 1, s[i][j - 1] + 1)
            else:
                s[i][j] = s[i - 1][j] + 1
        else:
            index = 0
            if j != 0 and i == 1:
                s[i][j] = s[i][j - 1]
            elif j != 0:
                s[i][j] = min(s[i - 1][j], s[i][j - 1])
            else:
                s[i][j] = s[i - 1][j]
print(s[-1][-1] - minus)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
table = []
for i in range(H):
    table.append([(True if x == "." else False) for x in list(input())])

colormap = [[0 for _ in range(W)] for _ in range(H)]
# colormap[0][0] = 0 if table[0][0] else 1

def update(i,j):

    tmp_A = 200
    tmp_B = 200
    
    if 0 < i < H and j < W:
        if table[i-1][j] == table[i][j]:
            tmp_A = colormap[i-1][j]
        else:
            tmp_A = colormap[i-1][j] + 1
        
    if i < H and 0 < j < W:
        if table[i][j-1] == table[i][j]:
            tmp_B = colormap[i][j-1]
        else:
            tmp_B = colormap[i][j-1] + 1
    
    colormap[i][j] = min(tmp_A, tmp_B)

for k in range(1, H + W - 1):
    # print("k={}".format(k))
    for i in range(k + 1):
        if i < H and k - i < W:
            # print("({},{})".format(i, k -i), end="")
            update(i, k - i)
    # print(colormap)

# 必要反転回数
plus = 1 if not table[0][0] else 0
print(colormap[H-1][W-1] // 2 + plus)

Submission status: Wrong Answer

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

def solve(grid, h, w):
    if h == 0 or w == 0:
        return 0
    color = grid[0][0]
    cnt = 0
    if color == '#':
        cnt += 1
    dist = [[-1] * w for _ in range(h)]
    dist[0][0] = 0
    q = []
    q.append((0, 0))
    current_color_cluster = [(0, 0)]
    while q and dist[h - 1][w - 1] == -1:
        new_q = []
        for i, j in q:
            for ii, jj in ((i + 1, j), (i, j + 1)):
                if 0 <= ii < h and 0 <= jj < w and dist[ii][jj] == -1 and grid[ii][jj] == color:
                    dist[ii][jj] = cnt
                    new_q.append((ii, jj))
        if new_q:
            current_color_cluster.extend(new_q)
            q = new_q
        elif dist[h - 1][w - 1] == -1:
            if color == '#':
                cnt += 1
            color = '#' if color == '.' else '.'
            q = current_color_cluster
            current_color_cluster = []
    return dist[h - 1][w - 1]

print(solve(grid, h, w))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

h,w = [int(x) for x in input().split()]
m = []
for _ in range(h):
    m.append(list(input()))
ans = 0
if m[0][0]=="#":
    m[0][0]="."
    ans+=1
if m[h-1][w-1]=="#":
    m[h-1][w-1]="."
    ans += 1


q = deque()
flag = 1
while True:
    d = [[-1]*w for x in range(h)]
    q.append([0,0])
    d[0][0] = 0
    while q:
        p = q.popleft()
        if p[0]==h-1 and p[1]==w-1:
            flag = 0
            break
        for dy,dx in [[1,0],[0,1]]:
            ny = p[0]+dy
            nx = p[1]+dx
            if 0<=ny<h and 0<=nx<w and d[ny][nx]==-1:
                if m[ny][nx]==".":
                    d[ny][nx] = 1
                    q.append([ny,nx])
                else:
                    d[ny][nx] = 1
                    m[ny][nx]="."
    if flag:
        ans += 1
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

d = deque([[0,0,s[0][0]]])
distance = [[float("inf")]*W for _ in range(H)]
if s[0][0] == ".":
    distance[0][0] = 0
else:
    distance[0][0] = 1

while d:
    y, x, c = d.popleft()
    for new_y, new_x in ([int(y)+1, int(x)], [int(y), int(x)+1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1:
            if s[new_y][new_x] == s[0][0] and c != s[0][0]:
                new_cost = distance[y][x] + 1
            else:
                new_cost = distance[y][x]
            if new_cost < distance[new_y][new_x]:
                distance[new_y][new_x] = new_cost
                d.append([new_y, new_x, s[new_y][new_x]])

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

Submission status: Wrong Answer

Solution code:
#template
def inputlist(): return [int(i) for i in input().split()]
#template
#issueから始める
H,W = inputlist()
maze = ['0']*H
for i in range(H):
    maze[i] = list(input())
dp = [[0]*W for _ in range(H)]
mm = H+W-2
for i in range(1,W):
    if maze[0][i] == '#':
        dp[0][i] = dp[0][i-1]+2
    else:
        dp[0][i] = dp[0][i-1]+1
for i in range(1,H):
    if maze[i][0] == '#':
        dp[i][0] = dp[i-1][0]+2
    else:
        dp[i][0] = dp[i-1][0]+1
for x in range(1,H):
    for y in range(1,W):
        if maze[x][y] == '#':
            dp[x][y] = min(dp[x-1][y],dp[x][y-1])+2
        else:
            dp[x][y] = min(dp[x-1][y],dp[x][y-1])+1
if maze[0][0] == "#":
    dp[H-1][W-1]+=1
print(dp[H-1][W-1]-mm)

Submission status: Wrong Answer

Solution code:
import sys

def change(row, count, H, W):
    print(H, W)
    if H == 0 and W == 0:
        pass
    elif H == 0:
        if row[H][W-1] == "#":
            count += 1
        count = change(row, count, H, W-1)
    elif W == 0:
        if row[H-1][W] == "#":
            count += 1
        count = change(row, count, H-1, W)
    else:
        if row[H-1][W] == ".":
            count = change(row, count, H-1, W)
        elif row[H][W-1] == ".":
            count = change(row, count, H, W-1)
        elif row[H][W-2] == ".":
            count += 1
            count = change(row, count, H, W-1)
        else:
            count += 1
            count = change(row, count, H-1, W)
    print(count)
    return count

(H, W) = sys.stdin.readline().split()
H = int(H)
W = int(W)
row = []
for _ in range(H):
    row.append(list(sys.stdin.readline()))
count = 0
if row[0][0] == "#":
    count += 1
    row[0][0] = "."
if row[H-1][W-1] == "#":
    count += 1
    row[H-1][W-1] = "."
print(change(row, count, H-1, W-1))

Submission status: Wrong Answer

Solution code:
from collections import deque

h,w = map(int,input().split())
F = [input() for _ in range(h)]
direct = [[0,1],[1,0]]
que = deque()
que.append([0,0])
INF = 10**5
dp = [[INF]*w for _ in range(h)]


dp[0][0] = 0

while len(que)>0:  
  x,y = que.popleft()    
  for dy,dx in direct:    
    ny = y+dy
    nx = x+dx
    if (0 <= nx < w) and (0 <= ny < h):
      if F[ny][nx] == F[y][x]:    
        if dp[ny][nx] > dp[y][x]:
          dp[ny][nx] = dp[y][x]
          que.append([ny,nx])
      else:        
        if dp[ny][nx] > dp[y][x] + 1 :        
          dp[ny][nx] = dp[y][x] + 1          
          que.append([ny,nx])   


ans = dp[-1][-1]//2
if F[0][0] == '#' or F[-1][-1] == '#':
  ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

H,W = [int(i) for i in input().split()]

s=[]

for i in range(H):
    s.append(str(input()))
    
# s[i][0] to s[i][W-1] 

pat_mat=[]

for i in range(H):
    if i ==0:
        if s[0][0] == ".":
            pat_mat.append([0])
        else:
            pat_mat.append([1])
        if W>1:
            for j in range(1,W):
                if s[0][j] == ".":
                    pat_mat[0].append(pat_mat[0][j-1])
                else:
                    if s[0][j-1]==s[0][j]:
                        pat_mat[0].append(int(pat_mat[0][j-1]))
                    else:
                        pat_mat[0].append(int(pat_mat[0][j-1]+1))
    else:
        if s[i][0] == ".":
            pat_mat.append([pat_mat[i-1][0]])
        else:
            if s[i-1][j]==s[i][j]:
                pat_mat.append([pat_mat[i-1][0]])
            else:
                pat_mat.append([pat_mat[i-1][0]+1])
        if W>1:
            for j in range(1,W):
                if s[i][j] == ".":
                    pat_mat[i].append(min(pat_mat[i][j-1],pat_mat[i-1][j]))
                else:
                    if s[i][j-1]==s[i][j]:
                        cand_l = pat_mat[i][j-1]
                    else:
                        cand_l = pat_mat[i][j-1]+1
                    if s[i][j]==s[i-1][j]:
                        cand_t= pat_mat[i-1][j]
                    else:
                        cand_t= pat_mat[i-1][j]+1

                    pat_mat[i].append(min(cand_l,cand_t))
                        
                    
print(int(pat_mat[H-1][W-1]))

Submission status: Wrong Answer

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

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


H,W = m()

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

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

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

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

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

if po[0][0] == ".":
    aa[0][0] = [2,3]
else:
    aa[0][0] = [1,0]

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

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


print(min(aaa[-1][-1][0],aa[-1][-1][0]))

Submission status: Wrong Answer

Solution code:
def flip_sq(sy, sx):
    gy = sy
    gx = sx
    while True:
        if sx == W - 1:
            if sy < H - 1 and tiles[sy+1][sx] == "#":
                sy += 1
            else:
                break
        else:
            if tiles[sy][sx+1] == "#":
                sx += 1
                if sy < H - 1 and tiles[sy+1][sx] == "#":
                    sy += 1
            else:
                break
    for y in range(gy, sy+1):
        for x in range(gx, sx+1):
            tiles[y][x] = flip[tiles[y][x]]

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}
flip = {"#":".", ".":"#"}
count = []
for i in range(H):
    count.append([])
    for j in range(W):
        count[i].append(0)

count[0][0] = tile[tiles[0][0]]
if tile[tiles[0][0]]:
    flip_sq(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]]
        if tile[tiles[y][x]]:
            flip_sq(y, x)

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

Submission status: Wrong Answer

Solution code:
import copy
import sys
h,w = map(int,input().split())
c = [list(input()) for i in range(h)]
ans = 0
flag = 0
check = 0
for i in range(h):
    if len(c[i]) == c[i].count('#'):
        check += 1
        
if check == h:
    print(1)
    sys.exit()
    
def bfs(count):
    global ans
    C = copy.deepcopy(c)
    
    queue = []
    visited = [[0 for i in range(w)] for i in range(h)]

    queue.append([0,0])
    visited[0][0] = 1

    dy_dx = [[1,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] != "#" and visited[y][x] == 0:
                    visited[y][x] = visited[now[0]][now[1]]+1
                    queue.append([y,x])
        if visited[h-1][w-1] != 0:
            print(ans)
            sys.exit()
            
    for i in range(h):
        for j in range(w):
            if visited[i][j] != 0:
                if i+1 < h:
                  c[i+1][j] = '.'
                if j+1 < w:
                  c[i][j+1] = '.'
    ans += 1
        
if c[0][0] == '#':
  c[0][0] == '.'
  ans += 1


for i in range(10000):
    bfs(i)

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[0][0]=="#")

dist = [[-1] * W for _ in range(H)]
dist[0][0] = 0
queue = deque()
queue.append((0,0))
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))
flg2=(c[-1][-1]=="#")
aa=flg+flg2-(flg==1 and flg2==1)
print((dist[-1][-1])//2+aa)           
#print(np.array(dist))

Submission status: Wrong Answer

Solution code:
import sys
from heapq import heappop, heappush
sys.setrecursionlimit(10000000)

h, w = map(int, input().split())
c = [input() for _ in range(h)]
 
d=[[100000]*w for _ in range(h)]

que = []
if c[0][0] == '.':
    heappush(que, (0, 0, 0))
    d[0][0] = 0
else:
    heappush(que, (1, 0, 0))
    d[0][0] = 1


while len(que):
    dist, px, py = heappop(que)
    if d[px][py] < dist:
        break
    if d[px][py] < dist:
        continue
    for dx, dy in [(0, 1), (1, 0)]:
        qx, qy = px + dx, py + dy
        if not (0 <= qx < h and 0 <= qy < w):
          continue
        if d[qx][qy] > dist + (c[qx][qy]=='#'):
            d[qx][qy] = dist + (c[qx][qy]=='#')
            heappush(que, (d[qx][qy], qx, qy))
    
print(d[h-1][w-1])

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[]
for i in range(n):
    s=list(input())
    l.append(s)
flag=0
save=l.copy()
#print(l)
black=0
white=0
for i in range(0,n):
    for j in range(0,m):
        if(l[i][j]=="#"):
            black+=1
        else:
            white+=1
if(l[0][0]=="#"):
    l[0][0]=1 
else:
    l[0][0]=0;
count=0
for i in range(1,n):
    if(l[i][0]=="#"):
        l[i][0]=l[i-1][0]+2
    else:
        l[i][0]=l[i-1][0]
for i in range(1,m):
    if( l[0][i]=="#"):
        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]=="#"):
            #print("test")
            l[i][j]=min(l[i-1][j],l[i][j-1])+1
        else:
            l[i][j]=min(l[i-1][j],l[i][j-1])
mb=l[n-1][m-1]
#print(mb)
#print(white)
if(l[0][0]==1):
    if mb>white:
        print(mb+1)
    else:
        print(mb)
else:
    print(mb)

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):
        if j == 0:
            left_side = 0
        else:
            left_side = dp[i, j-1]
        dp[i, j] = max(dp[i-1, j], left_side) + (field[i][j] == ".")*1
print(int(h+w - 1 - dp[h-1, w-1]))

Submission status: Wrong Answer

Solution code:
h, w = (int(x) for x in input().split())
X = [input().split() for i in range(h)]
ans = 0

i = 0
j = 0
hFlag = False
wFlag = False
lim = 0

while lim < (h-1)+(w-1):
    #現在地の判定
    # print(X[i][0][j])
    if X[i][0][j] == "#":
        ans+=1

    if i+1 >= h:
        hFlag = True
    if j+1 >= w:
        wFlag = True
    if hFlag == True and wFlag == True:
        break

    if hFlag == True:
        j += 1
    elif wFlag == True:
        i += 1
    else:
        if X[i+1][0][j] == ".":
            # print("a")#消す
            i+=1
            continue
        elif X[i][0][j+1] == ".":
            # print("b")#消す
            j+=1
            continue

        if i+2 < h:
            if X[i+2][0][j] == ".":
                # print("c")#消す
                i+=2
                ans+=1
            elif X[i+1][0][j+1] == ".":
                # print("d")#消す
                i+=1
                j+=1
                ans+=1
            else:
                i+=1
        elif j+2 < w:
            if X[i][0][j+2] == ".":
                # print("e")#消す
                j+=2
                ans+=1
            elif X[i+1][0][j+1] == ".":
                # print("f")#消す
                i+=1
                j+=1
                ans+=1
            else:
                # print("g")#消す
                i+=1
        else:
            # print("h")#消す
            i+=1
    lim += 1

print(ans)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
table=[[0]*W for i in range(H)]
for i in range(H):
  s=str(input())
  for j in range(W):
    if s[j]=='#':
      table[i][j]=1
    else:
      pass
for i in range(H):
  for j in range(W):
    if i==0:
      if j==0:
        pass
      else:
        table[i][j]+=table[i][j-1]
    else:
      if j>=1:
        x,y,z=table[i-1][j],table[i][j-1],table[i-1][j-1]
        table[i][j]+=min(x,y,z+1)
      else:
        table[i][j]+=table[i-1][j]
ans=table[H-1][W-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(10**7)
import numpy as np


def DFS(x,y,num):
    S[x][y]="."
    D[x][y]=num
    for a,b in ((0,1),(0,-1),(1,0),(-1,0)):
        X,Y=x+a,y+b
        if S[X][Y]=="#" and 1<=X<=h and 1<=Y<=w:
            DFS(X,Y,num)


def BFS(x,y):
    color[x][y]="gray"
    D[x][y]=0 if S[x][y]!="#" else 1
    queue=deque([[x,y]])

    while len(queue)!=0:
        a,b=queue.popleft()
        for c,d in [[0,1],[1,0]]:
            X,Y=a+c,b+d
            if color[X][Y]=="white" and (1<=X<=h and 1<=Y<=w):
                color[X][Y]="gray"
                if S[X][Y]=="#":
                    D[X][Y]=min(D[X-1][Y]+1,D[X][Y-1]+1)
                    DFS(X,Y,D[X][Y])
                elif S[X][Y]=="." and D[X][Y]==inf:
                    D[X][Y]=min(D[X-1][Y],D[X][Y-1])
                queue.append([X,Y])
                #print(np.array(D))
        color[a][b]="black"
    #print(np.array(D))
    return D[h][w]


h,w=map(int,input().split())
S=[["#"]+list(input())+["#"] for _ in range(h)]
S.insert(0,["#"]*(w+2))
S.append(["#"]*(w+2))

inf=float("inf")
color=[["white" for _ in range(w+2)] for _ in range(h+2)]
D=[[inf for _ in range(w+2)] for _ in range(h+2)]
P=[["#"]*(w+2) for _ in range(h+2)]
if S==P:print(1);exit()
print(BFS(1,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 + 1) for _ in range(H + 1)]
for i in range(W + 1):
  dp[0][i] = 200
for i in range(H + 1):
  dp[i][0] = 200
for i in range(1, H + 1):
  for j in range(1, W + 1):
    if i != 1 or j != 1:
      dp[i][j] = min(dp[i][j - 1], dp[i - 1][j])
    if l[i - 1][j - 1] == '#':
      dp[i][j] += 1 
print(dp[H][W])

Submission status: Wrong Answer

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

    FROM_LEFT = 0
    FROM_UP = 1

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

    h = []
    x = int(s[0][0] == '#')
    r = 0
    while r < H and s[r][0] == s[0][0]:
        heappush(h, (x, r, 0, FROM_UP))
        r += 1
    c = 0
    while c < W and s[0][c] == s[0][0]:
        heappush(h, (x, 0, c, FROM_LEFT))
        c += 1

    cost = [[H * W] * W for _ in range(H)]
    cost[0][0] = x

    while h:
        cnt, r, c, from_ = heappop(h)
        if r == H - 1 and c == W - 1:
            print(cnt)
            return

        if r < H - 1:
            if s[r][c] == s[r + 1][c] and from_ == FROM_UP:
                pass
            else:
                nr = r + 1
                ncnt = cnt + int(s[r + 1][c] == '#')

                while nr < H and s[nr][c] == s[r + 1][c]:
                    if ncnt < cost[nr][c]:
                        cost[nr][c] = ncnt
                        heappush(h, (ncnt, nr, c, FROM_UP))

                    nr += 1

        if c < W - 1:
            if s[r][c] == s[r][c + 1] and from_ == FROM_LEFT:
                pass
            else:
                nc = c + 1
                ncnt = cnt + int(s[r][c + 1] == '#')

                while nc < W and s[r][nc] == s[r][c + 1]:
                    if ncnt < cost[r][nc]:
                        cost[r][nc] = ncnt
                        heappush(h, (ncnt, r, nc, FROM_LEFT))
                    nc += 1


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())
hw = []
for i in range(h):
    hw.append(input())

todo = deque([(0,0)])
seen = [[200 for j in range(w)] for i in range(h)]
sx = 0
sy = 0
if hw[0][0] == '.':
    seen[0][0] = 0
else:
    seen[0][0] = 1




while todo:
    sx,sy = todo.popleft()
    if sx + 1 < h and seen[sx+1][sy] == 200:
        todo.append((sx+1,sy))
    if sx + 1 < h and hw[sx+1][sy] == '.' and seen[sx+1][sy] > seen[sx][sy]:
        seen[sx+1][sy] = seen[sx][sy]
    if sx + 1 < h and hw[sx+1][sy] == '#' and seen[sx+1][sy] > seen[sx][sy]+1:
        seen[sx+1][sy] = seen[sx][sy]+1
    if sy+1 < w and seen[sx][sy+1] == 200:
        todo.append((sx,sy+1))
    if sy + 1 < w and hw[sx][sy+1] == '.' and seen[sx][sy+1] > seen[sx][sy]:
        seen[sx][sy+1] = seen[sx][sy]
    if sy + 1 < w and hw[sx][sy+1] == '#' and seen[sx][sy+1] > seen[sx][sy]+1:
        seen[sx][sy+1] = seen[sx][sy]+1

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