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 collections import deque
h, w = map(int, input().split())
mas = [list(input()) for _ in range(h)]
mass = [[] for _ in range(h)]

for i, row in enumerate(mas):
    for j, v in enumerate(row):
        if i == 0 and j == 0:
            mass[i].append(mas[i][j])
            continue
        if i == 0 and mas[i][j-1] == "#":
            mass[i].append(".")
            continue
        if i == 0:
            mass[i].append(mas[i][j])
            continue

        if j == 0 and mas[i-1][j] == "#":
            mass[i].append(".")
            continue
        if j == 0:
            mass[i].append(mas[i][j])
            continue

        if mas[i-1][j] == mas[i][j-1] == "#":
            mass[i].append(".")
        else:
            mass[i].append(mas[i][j])

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:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
count=0
x=0
y=0
pos=[[0,0,0]]
def stansaku(kyori,pos):
  while len(pos)>0:
    x,y,count = pos.pop(0)
    if x<h-1 and s[x+1][y]==".":
      pos.append([x+1,y,count+1])
      if kyori[0]<(count+1):
        kyori[0]=count+1
        kyori[1]=x+1
        kyori[2]=y
    if y<w-1 and s[x][y+1]==".":
      pos.append([x,y+1,count+1])
      if kyori[0]<(count+1):
   	    kyori[0]=count+1
   	    kyori[1]=x
   	    kyori[2]=y+1  
  return kyori

def ktansaku(kyori,pos):
  while len(pos)>0:
    x,y,count = pos.pop(0)
    if x<h-1 and s[x+1][y]=="#":
      pos.append([x+1,y,count+1])
      if kyori[0]<(count+1):
        kyori[0]=count+1
        kyori[1]=x+1
        kyori[2]=y
    if y<w-1 and s[x][y+1]=="#":
      pos.append([x,y+1,count+1])
      if kyori[0]<(count+1):
   	    kyori[0]=count+1
   	    kyori[1]=x
   	    kyori[2]=y+1  
  return kyori

    
if s[0][0]==".":
  flag=0
else:
  flag=1
  
ans=0
anss=[]
while ans<=h+w-3:
  if flag==1:
    count+=1
    if x==0 and y==0:
      pos=[[0,0,0]]
      anss=ktansaku([0,0,0],pos)
      ans+=anss[0]
      x=anss[1]
      y=anss[2]
      flag=0
    else:
      pos=[[x,y,0]]
      anss=ktansaku([x,y,0],pos)
      ans+=anss[0]
      x=anss[1]
      y=anss[2]
      flag=0
  else:
    if x==0 and y==0:
      pos=[[0,0,0]]
      anss=stansaku([0,0,0],pos)
      ans+=anss[0]
      x=anss[1]
      y=anss[2]
      flag=1
    else:
      pos=[[x,y,0]]
      anss=stansaku([x,y,0],pos)
      ans+=anss[0]
      x=anss[1]
      y=anss[2]
      flag=1
print(count)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
for i in range(h):
    s=list(map(str,input().split())) #str

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]
next_list = []
now_list = [(-1,0)]
ok = "."
cnt = 0
goal = 0
while goal == 0:
    next_list = set()
    for x, y in now_list:
        if x == W-1 and y == H-1:
            goal = 1
            break
        for nx, ny in ((1,0),(0,1)):
            nx += x
            ny += y
            if nx >= W or ny >= H or nx < 0 or ny < 0:
                continue
            if field[ny][nx] == ok:
                next_list.add((nx,ny))
    if goal == 1:
        pass
    elif len(next_list) > 0:
        now_list = list(next_list)
    else:
        if ok == ".":
            cnt += 1
            ok = "#"
        else:
            ok = "."


print(cnt)

Submission status: Wrong Answer

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

#入力
H, W = map(int,input().split())
s = [list(map(str,list(input()))) for i in range(H)] 

#初期値
count1 = 0
count2 = 0
conti = 0
j = 0
k = 0

if s[j][k]=='#':
    count1 += 1
    count2 += 1


while  j+1<H or k+1<W:
    if j+1!=H and s[j+1][k] == '.':
        j += 1
        conti = 0
    elif k+1!=W and s[j][k+1] == '.':
        k += 1
        conti = 0
    elif j+1!=H and s[j+1][k] == '#' :
        if conti == 0:
            count1 += 1
            conti = 1
        else:
            conti += 1
            j+=1
    elif k+1!=W and s[j][k+1] == '#':
        if conti == 0:
            count1 += 1
            conti = 1
        else:
            conti += 1
            k+=1

j = 0
k = 0
    
while  j+1<H or k+1<W:
    if k+1!=W and s[j][k+1] == '.':
        k += 1
        conti = 0
    elif j+1!=H and s[j+1][k] == '.':
        j += 1
        conti = 0
    elif k+1!=W and s[j][k+1] == '#':
        if conti == 0:
            count2 += 1
            conti = 1
        else:
            conti += 1
        k+=1
    elif j+1!=H and s[j+1][k] == '#' :
        count2 += 1
        j+=1
    

if count1<count2:
    count=count1
else:
    count=count2

print(count)

Submission status: Wrong Answer

Solution code:
import copy

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


change_list = copy.deepcopy(root_list)
DP_list = []

for i, line in enumerate(root_list):
    DP_line = []
    str_pt = root_list[i][0]
    if i == 0:
        DP_line.append(0) # 初期値
    else:
        if root_list[i-1][0] == line[0]: # 1文字目が上下一致
            DP_line.append(DP_list[i-1][0])
        else:
            DP_line.append(DP_list[i-1][0] + 1)

    for j, pt in enumerate(line[1:], 1): # 先頭は抜かす
        if i == 0: # 1行目
            if str_pt == pt: # 前の文字と一致
                DP_line.append(DP_line[j-1]) # 前の値を入れる
            else:
                DP_line.append(DP_line[j-1] + 1) # 違ったら1足す
                str_pt = pt
        else: # 2行目以降（上下左右比較）
            # 横比較
            if str_pt == pt:
                yoko_pt = DP_line[j-1]
            else:
                yoko_pt = DP_line[j-1] + 1
            
            # 縦比較
            if pt == root_list[i-1][j]:
                tate_pt = DP_list[i-1][j]
            else:
                tate_pt = DP_list[i-1][j] + 1
            
            DP_line.append(min(yoko_pt, tate_pt))
            str_pt = pt

    DP_list.append(DP_line)

result = DP_list[H-1][W-1] // 2

if root_list[0][0] == "#":
    result += 1
if root_list[H-1][W-1] == "#":
    result += 1
if  root_list[0][0] == "#" and root_list[H-1][W-1]:
    result -= 1

print(result)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
ans = []
for i in range(h):
    ans.append([9999999999] * w)
arr = []
for i in range(h):
    s = list(input())
    arr.append(s)
ch = 0
if arr[0][0] == '#':
    ans[0][0] = 1
    ch = 1
else: ans[0][0] = 0
for i in range(h):
    for j in range(w):
        if i == 0 and j == 0: continue
        if j - 1 >= 0: ans[i][j] = min(ans[i][j], ans[i][j - 1]) 
        if i - 1 >= 0: ans[i][j] = min(ans[i][j], ans[i - 1][j])
        if ch == 0 and arr[i][j] == '#': 
            ans[i][j] -= -1
            ch = 1
        if arr[i][j] == '.' and ch == 1: ch = 0
print(ans[h - 1][w - 1])

Submission status: Wrong Answer

Solution code:
# Code for A - Range Flip Find Route
# Use input() to fetch data from STDIN

import numpy as np
H, W = map(int, input().split())

S = [list(str(input())) for i in range(H)]
S = np.array(S)

dp = np.zeros((H, W), dtype=np.int)

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

for i in range(1, W):
    if S[0, i] == "#":
        if S[0, i] == S[0, i-1]:
            dp[0, i] = dp[0, i-1]
        else:
            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] == "#":
        if S[i, 0] == S[i-1, 0]:
            dp[i, 0] = dp[i-1, 0]
        else:
            dp[i, 0] = dp[i-1, 0]+1
    else:
        dp[i, 0] = dp[i-1, 0]

# print(dp)

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

        if S[i, j] == "#":
            if S[i, j] == S[i, j-1]:
                dp[i, j] = dp[i, j-1]
            else:
                dp[i, j] = dp[i, j-1]+1
        else:
            dp[i, j] = dp[i, j-1]

        dp[i, j] = min(dp[i, j], tmp1)
# print(dp)
print(dp[-1, -1])

Submission status: Wrong Answer

Solution code:
import numpy as np
h, w = map(int, input().split())
s = [input() for _ in range(h)]

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

if s[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0
    
for i in range(h - 1):
    if s[i + 1][0] == "." or s[i][0] == s[i + 1][0]:
        dp[i + 1][0] = dp[i][0]
    else:
        dp[i + 1][0] = dp[i][0] + 1
for j in range(w - 1):
    if s[0][j + 1] == "." or s[0][j] == s[0][j + 1]:
        dp[0][j + 1] = dp[0][j]
    else:
        dp[0][j + 1] = dp[0][j] + 1

for i in range(h - 1):
    for j in range(w - 1):
        if s[i + 1][j + 1] == "." or (s[i][j + 1] == s[i + 1][j + 1] and s[i + 1][j + 1] == s[i + 1][j]):
            dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j])
        elif s[i][j + 1] == s[i + 1][j + 1] and s[i + 1][j + 1] != s[i + 1][j]:
            dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j] + 1)
        elif s[i][j + 1] != s[i + 1][j + 1] and s[i + 1][j + 1] == s[i + 1][j]:
            dp[i + 1][j + 1] = min(dp[i][j + 1] + 1, dp[i + 1][j])
        else:
            dp[i + 1][j + 1] = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1)
    
if s[h - 1][w - 1] == "#" and (s[0][0] == "#" and dp[h - 2][w - 1] != 1 and dp[h - 1][w - 2] != 1):
    dp[h- 1][w - 1] += 1

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

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
count=0
x=0
y=0
pos=[[0,0,0]]
def stansaku(kyori,pos):
  while len(pos)>0:
    x,y,count = pos.pop(0)
    if x<h-1 and s[x+1][y]==".":
      pos.append([x+1,y,count+1])
      if kyori[0]<(count+1):
        kyori[0]=count+1
        kyori[1]=x+1
        kyori[2]=y
    if y<w-1 and s[x][y+1]==".":
      pos.append([x,y+1,count+1])
      if kyori[0]<(count+1):
   	    kyori[0]=count+1
   	    kyori[1]=x
   	    kyori[2]=y+1  
  return kyori

def ktansaku(kyori,pos):
  while len(pos)>0:
    x,y,count = pos.pop(0)
    if x<h-1 and s[x+1][y]=="#":
      pos.append([x+1,y,count+1])
      if kyori[0]<(count+1):
        kyori[0]=count+1
        kyori[1]=x+1
        kyori[2]=y
    if y<w-1 and s[x][y+1]=="#":
      pos.append([x,y+1,count+1])
      if kyori[0]<(count+1):
   	    kyori[0]=count+1
   	    kyori[1]=x
   	    kyori[2]=y+1  
  return kyori

    
if s[0][0]==".":
  flag=0
else:
  flag=1

ans=0
anss=[]
while ans<=h+w-2:
  if flag==1:
    count+=1
    if x==0 and y==0:
      pos=[[0,0,0]]
      anss=ktansaku([0,0,0],pos)
      ans+=anss[0]
      x=anss[1]
      y=anss[2]
      flag=0
    else:
      pos=[[x,y,0]]
      anss=ktansaku([x,y,0],pos)
      ans+=anss[0]
      x=anss[1]
      y=anss[2]
      flag=0
  else:
    if x==0 and y==0:
      pos=[[0,0,0]]
      anss=stansaku([0,0,0],pos)
      ans+=anss[0]
      x=anss[1]
      y=anss[2]
      flag=1
    else:
      pos=[[x,y,0]]
      anss=stansaku([x,y,0],pos)
      ans+=anss[0]
      x=anss[1]
      y=anss[2]
      flag=1
print(count-1)

Submission status: Wrong Answer

Solution code:
import itertools
k = input()

heigh, wight = k.split(' ')
heigh, wight = int(heigh), int(wight)

list_ = []
for i in range(0, int(heigh)):
  f = input()
  list_.append(list(f))
list_.reverse()
sg = '0' * (heigh - 1) + '1' * (wight -1)
l_ = [int(i) for i in sg]
result_counts = [500]
max_ = 0
if heigh > wight:
  max_ = heigh
else:
  max_ = wight

for l__ in list(itertools.combinations(l_, len(l_))):
    count = 0
    x, y = 0, 0
    now = list_[heigh - y - 1][x]
    for s in l__:
        if s == 1:
            x = x + 1
        else:
            y = y + 1
        next_ = list_[heigh - y - 1][x]
        if now == '#' and next_== '.':
            count += 1
        if count > max_:
            continue
        if count > min(result_counts):
            continue
        now = next_
    if now == '#':
        count += 1
    result_counts.append(count)
    if count == 0:
        break
print(min(result_counts))

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 = [[10000]*w for _ in range(h)]

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

while len(que):
    dist, px, py, flg = heappop(que)
    if d[h-1][w-1] < dist:
      break
    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 + (flg and c[qx][qy]=='#'):
            if flg:
              d[qx][qy] = min(d[qx][qy], dist + (c[qx][qy]=='#'))
              heappush(que, (d[qx][qy], qx, qy, False))
            else:
              d[qx][qy] = min(d[qx][qy], dist)
              heappush(que, (d[qx][qy], qx, qy, True))

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

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
A = []
for i in range(H):
    b = list(input())
    A.append(b)
dp = [[10**9 for i in range(W)] for i in range(H)]


if A[0][0] == ".":
    dp[0][0] = 0
else:
    dp[0][0] = 1

p = []
for i in range(H):
    for j in range(W):
        p.append([i,j])

t = 0
i = 0
j = 0
while(True):
    if i < H-1 and A[i+1][j] == ".":
        dp[i+1][j] = min(dp[i+1][j],dp[i][j])
    if j < W-1 and A[i][j+1] == ".":
        dp[i][j+1] = min(dp[i][j+1],dp[i][j])
    if i < H-1 and A[i+1][j] == "#":
        dp[i+1][j] = min(dp[i+1][j], dp[i][j]+1)
    if j < W-1 and A[i][j+1] == "#":
        dp[i][j+1] = min(dp[i][j+1], dp[i][j]+1)

    i = p[t][0]
    j = p[t][1]
    t += 1
    if i == H-1 and j == W-1:
        break

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

Submission status: Wrong Answer

Solution code:
import collections

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

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

b = [[float("inf")]*(w+2) for _ in range(h+2)]
b[1][1] = 1 if s[1][1] == "#" else 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 b[ny][nx] == float("inf") and s[ny][nx] != " ":
            b[ny][nx] = min(b[y][x], b[ny-1][nx], b[ny][nx-1])
            if s[ny][nx] == "#":
                b[ny][nx] += 1

            q.appendleft([nx, ny])

"""
for _b in b:
    print(_b)
"""

print(b[h][w])

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

Solution code:
import itertools

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

for i in range(H):
    garden.append(list(input().replace(".","1").replace("#","0")))

#print(garden)
dct=[(1,0),(0,1),(-1,0),(0,-1)]
#garden=[[1,0,1,0,1],[0,1,0,1,0],[1,0,1,0,1],[0,1,0,1,0],[1,0,1,0,1]]
#H,W=5,5
start=(0,0)
goal=(H-1,W-1)
path=[start]
d=[ [i+j for i in range(W)] for j in range(H)]
#print(d)
done=[(0,0)]
for i, j in itertools.product(range(H),range(W)):
    color=garden[i][j]
    for each in dct:
        di,dj=each[0],each[1]
        if i+di<H and j+dj< W and i+di>=0 and j+dj>=0 and  garden[i+di][j+dj]==color:
            d[i+di][j+dj]=min(d[i+di][j+dj],d[i][j])
            done.append((i+di,j+dj))
            #print((i,j),(i+di,j+dj))
            #print("same")
        elif i+di<H and j+dj< W and i+di>=0 and j+dj>=0 :
            d[i+di][j+dj]=min(d[i+di][j+dj],d[i][j]+1)
            done.append((i+di,j+dj))
            #print((i,j),(i+di,j+dj))
            #print("not same")
    #print(d)
if int(garden[0][0])*int(garden[H-1][W-1])==1:
  print(int((d[H-1][W-1]+1)/2))
else:
  print(int((d[H-1][W-1]+1)/2)+1)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S=[['' for i in range(W)] for j in range(H)]
for i in range(H):
  S[i]=list(input())
index=[[0 for i in range(W)] for j in range(H)]
count=0
if S[0][0]=="#":
  count+=1
  S[0][0]="."
if S[H-1][W-1]=="#":
  count+=1
  S[H-1][W-1]="."
for i in range(H):
  for j in range(W):
    if i==0 and j==0:
      index[i][j]=0
    else:
      up=S[i-1][j]
      left=S[i][j-1]
      if j-1<0:
        leftIndex=1000
      else:
        leftIndex=index[i][j-1]
      if i-1<0:
        upIndex=1000
      else:
        upIndex=index[i-1][j]
      if S[i][j]==".":
        index[i][j]=min(leftIndex,upIndex)
      else:
        index[i][j]=min(leftIndex,upIndex)+1
print(index[H-1][W-1]+count)

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 if S[i - 1][j] == "." else 0))
            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 if S[i][j - 1] == "." else 0))
            else:
                dist[i][j] = min(dist[i][j], dist[i][j - 1])

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

Submission status: Wrong Answer

Solution code:
#coding:utf-8

from collections import deque

def main():
    h, w = map(int, input().split())
    mtrx = []
    for _ in range(h):
        mtrx.append(list(input()))
    
    dire = [[0, 1], [1, 0]]
    cnt = 0
    q = deque([])
    # cnt, flg, i, j
    if mtrx[0][0] == '#':
        cnt += 1
    q.append([cnt, mtrx[0][0], 0, 0])

    minv = 100000000000

    dummy = [[minv for _ in range(w)] for _ in range(h)]
    while len(q) > 0:
        now = q.popleft()
        i = now[2]
        j = now[3]

        for d in dire:
            i_n = i + d[0]
            j_n = j + d[1]
            cnt = now[0]
            if i_n >= h or j_n >= w:
                continue
            if mtrx[i_n][j_n] == '#':
                cnt += 1
            if dummy[i_n][j_n] <= cnt:
                continue
            dummy[i_n][j_n] = cnt
            # print(now)
            # print(mtrx[i_n][j_n])
            next_obj = [cnt, mtrx[i_n][j_n], i_n, j_n]
            q.append(next_obj)
            
        if i == h - 1 and j == w - 1:
            minv = min(minv, now[0])
    print(minv)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
h, w = map(int, input().split())
s = [input() for _ in range(h)]

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

if s[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0
    
for i in range(h - 1):
    if s[i + 1][0] == "." or s[i][0] == s[i + 1][0]:
        dp[i + 1][0] = dp[i][0]
    else:
        dp[i + 1][0] = dp[i][0] + 1
for j in range(w - 1):
    if s[0][j + 1] == "." or s[0][j] == s[0][j + 1]:
        dp[0][j + 1] = dp[0][j]
    else:
        dp[0][j + 1] = dp[0][j] + 1

for i in range(h - 1):
    for j in range(w - 1):
        if s[i + 1][j + 1] == "." or (s[i][j + 1] == s[i + 1][j + 1] and s[i + 1][j + 1] == s[i + 1][j]):
            dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j])
        elif s[i][j + 1] == s[i + 1][j + 1] and s[i + 1][j + 1] != s[i + 1][j]:
            dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j] + 1)
        elif s[i][j + 1] != s[i + 1][j + 1] and s[i + 1][j + 1] == s[i + 1][j]:
            dp[i + 1][j + 1] = min(dp[i][j + 1] + 1, dp[i + 1][j])
        else:
            dp[i + 1][j + 1] = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1)
    
if s[h - 1][w - 1] == "#" and (s[0][0] == "#" and dp[h - 2][w - 1] != 1 and dp[h - 2][w - 1] != 1):
    dp[h- 1][w - 1] += 1

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

Submission status: Wrong Answer

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

w = 0
h = 0
cnt = 1 if A[h][w] == '#' else 0


def ccc(aa, ww):
    count = -1
    for a in aa:
        count += a[ww:].count('.')
    return count


while True:
    right = ccc(A[h:], w + 1)
    bottom = ccc(A[h + 1:], w)
    if right == -1 and bottom == -1:
        if h == H - 1:
            w += 1
        else:
            h += 1
    else:
        if right > bottom:
            w += 1
        else:
            h += 1

    if A[h][w] == '#':
        cnt += 1

    if w == W - 1 and h == H - 1:
        break

print(cnt)

Submission status: Wrong Answer

Solution code:
from sys import stdin,setrecursionlimit
setrecursionlimit(10**6) 
h,w = map(int,stdin.readline().rstrip().split())
li = [stdin.readline().rstrip() for _ in range(h)]
inf = 10**10
lin = [[0 for i in range(w)]for j in range(h)]
for i in range(h):
    for j in range(w):
        if li[i][j] == "#":
            lin[i][j] += 1
        if i == 0 and j == 0:
            continue
        if j-1 >= 0:
            k = lin[i][j-1]
            if lin[i][j-1] == "#" and lin[i][j] == "#":
                lin[i][j] = lin[i][j-1]
                continue
        else:
            k = inf
        if i-1 >= 0:
            l = lin[i-1][j]
            if lin[i-1][j] == "#" and lin[i][j] == "#":
                lin[i][j] = lin[i-1][j]
                continue
        else:
            l = inf
        lin[i][j] += min(k,l)
print(lin[h-1][w-1])

Submission status: Wrong Answer

Solution code:
H,W = list(map(int,input().split()))
listA = []
listB = []
def convert_1d_to_2d(l, cols):
    return [l[i:i + cols] for i in range(0, len(l), cols)]
total = 0
A = 0
for i in range(H):
    listA.append(list(input()))
for i in range(W):
    for j in range(H):
        listB.append(listA[j][i])
listB = convert_1d_to_2d(listB,H)
for i in range(W):
    if listB[i][A] == "#":
        if A == H-1:
            total += 1
        else:
            total += 1
            A += 1
    while listB[i][A] == "." and A != H-1:
        if listB[i][A+1] == ".":
            A += 1
        else:
            break
result_A = total+(H-1-A)
for i in range(H):
    if listA[i][A] == "#":
        if A == W-1:
            total += 1
        else:
            total += 1
            A += 1
    while listA[i][A] == "." and A != W-1:
        if listA[i][A+1] == ".":
            A += 1
        else:
            break
result_B = total+(W-1-A)
print(min(result_A,result_B))

Submission status: Wrong Answer

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

ans = 0


def next(cx, cy):
    c = 0
    g, b = 0, 0
    if mp[cy - 1][cx - 1] == "#":
        c = 1
    # end
    if cx != w:
        g = next(cx + 1, cy) + c
    else:
        return c
    if cy != h:
        b = next(cx, cy + 1) + c
    else:
        return c
    return c + min(g, b)


print(mp, next(1, 1))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().rstrip().split())
D = []
for h in range(H):
    D.append(list(input().rstrip()))
# print(*D, sep='\n')
d = {}
for i in range(1, H+W+1):  # i: total
    for h in range(i):
        w = i - h - 1
        if h > H:
            w += H - h
            h = H
        if w >= W or h >= H:
            continue
        if (h-1, w) in d.keys() and (h, w-1) in d.keys():
            d[(h, w)] = min(d[(h-1, w)], d[(h, w-1)])
        elif (h-1, w) in d.keys():
            d[(h, w)] = d[(h-1, w)]
        elif (h, w-1) in d.keys():
            d[(h, w)] = d[(h, w-1)]
        else:
            d[(h, w)] = 0
        if D[h][w] == '#':
            d[(h, w)] += 1
print(d[(H-1, W-1)])

Submission status: Wrong Answer

Solution code:
h, w = [int(x) for x in input().split()]
input_ = [list(input()) for _ in range(h)]
memo = [[0 for i in range(w)] for _ in range(h)]


for i in range(h):
  w_ = input_[i]
  if i == 0:
    for x in range(len(w_)):
      if x == 0:
        if w_[x] == '#':
          memo[i][x] = 1
      else:
        if w_[x] == '#':
          memo[i][x] = memo[i][x-1] + 1
        else:
          memo[i][x] = memo[i][x-1]
  else:
    for x in range(len(w_)):
      if w_[x] == '#':
        if x==0:
          a = 10**9
          b = memo[i-1][x] + 1
        else:
          try:
            a = memo[i][x-1] + 1
          except:
            a = 10**9
          try:
            b = memo[i-1][x] + 1
          except:
            b = 10**9

        memo[i][x] = min(a, b)
      else:
        if x==0:
          a = 10**9
          b = memo[i-1][x]
        else:
          try:
            a = memo[i][x-1]
          except:
            a = 10**9
          try:
            b = memo[i-1][x]
          except:
            b = 10**9
        memo[i][x] = min(a, b)




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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import queue

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

s=(0,0)
dist = [[-1]*W for _ in range(H)]
que = queue.Queue()
que.put(s)
dist[s[0]][s[1]] = 0


while not que.empty():
    h, w = que.get()
    direction = [(0, 1), (1, 0)]
    for y, x in direction:
        nh=h+y
        nw=w+x
        if nw < 0 or nw > W-1 or nh < 0 or nh > H-1:
            continue
        if dist[h+y][w+x] == -1:
            que.put((h+y, w+x))
            if c[h+y][w+x]=="#":
                dist[h+y][w+x] = dist[h][w]+1
            else:
                dist[h+y][w+x] = dist[h][w]
        else:
            if c[h+y][w+x]=="#":
                dist[h+y][w+x] = min(dist[h][w]+1,dist[h+y][w+x])
            else:
                dist[h+y][w+x] = min(dist[h][w],dist[h+y][w+x])

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

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

Submission status: Wrong Answer

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

ans = 0


def next(cx, cy):
    print(cx, cy)
    print(w, h)
    print("========")
    c = 0
    g, b = 0, 0
    if mp[cy - 1][cx - 1] == "#":
        c = 1
    # end
    if cx != w:
        g = next(cx + 1, cy) + c
    else:
        return c
    if cy != h:
        b = next(cx, cy + 1) + c
    else:
        return c
    return c + min(g, b)


print(mp, next(1, 1))

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
A = []
for i in range(H):
    b = list(input())
    A.append(b)
dp = [[10**9 for i in range(W)] for i in range(H)]


if A[0][0] == ".":
    dp[0][0] = 0
else:
    dp[0][0] = 1

p = []
for i in range(H):
    for j in range(W):
        p.append([i,j])

t = 0
i = 0
j = 0
while(True):
    if i < H-1 and A[i+1][j] == ".":
        dp[i+1][j] = min(dp[i+1][j],dp[i][j])
        if j-1 >= 0:
            dp[i+1][j] = min(dp[i+1][j-1],dp[i+1][j])
    if j < W-1 and A[i][j+1] == ".":
        dp[i][j+1] = min(dp[i][j+1],dp[i][j])
        if i-1 >= 0:
            dp[i][j+1] = min(dp[i][j+1],dp[i-1][j+1])
    if i < H-1 and A[i+1][j] == "#":
        dp[i+1][j] = min(dp[i+1][j], dp[i][j]+1)
        if j-1 >= 0:
            dp[i+1][j] = min(dp[i+1][j-1]+1, dp[i+1][j])
    if j < W-1 and A[i][j+1] == "#":
        dp[i][j+1] = min(dp[i][j+1], dp[i][j]+1)
        if i-1 >= 0:
            dp[i][j+1] = min(dp[i][j+1], dp[i-1][j+1]+1)

    i = p[t][0]
    j = p[t][1]
    t += 1
    if i == H-1 and j == W-1:
        break

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

Submission status: Wrong Answer

Solution code:
import heapq
import sys

readline = sys.stdin.buffer.readline


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

    dp = [[10**9 for _ in range(W)] for _ in range(H)]

    start_point = (0, 0)
    # goal_point = (H - 1, W - 1)
    dr = 0  # 前の進行方向(1,2)

    queue = []
    heapq.heappush(queue, (start_point, dr))

    tests = ((1, 0), (0, 1))

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

    while queue:
        queue_next = []

        for _ in range(len(queue)):
            (y, x), pdr = heapq.heappop(queue)

            for dy, dx in tests:
                ny = y + dy
                nx = x + dx

                if nx >= W or ny >= H:
                    continue

                ndr = 1 if dy == 1 else 2

                if S[ny][nx] == S[y][x] == "#":
                    if (pdr == 1 and ndr == 1) or (pdr == 2 and ndr == 2):
                        if dp[ny][nx] < dp[y][x]:
                            continue
                        else:
                            dp[ny][nx] = dp[y][x]
                            heapq.heappush(queue_next, ((ny, nx), ndr))
                    else:
                        if dp[ny][nx] < dp[y][x] + 1:
                            continue
                        else:
                            dp[ny][nx] = dp[y][x] + 1
                        heapq.heappush(queue_next, ((ny, nx), ndr))

                elif S[ny][nx] == ".":
                    if dp[ny][nx] < dp[y][x]:
                        continue
                    else:
                        dp[ny][nx] = dp[y][x]
                        heapq.heappush(queue_next, ((ny, nx), ndr))

                elif S[y][x] == "." and S[ny][nx] == "#":
                    if dp[ny][nx] < dp[y][x] + 1:
                        continue
                    else:
                        dp[ny][nx] = dp[y][x] + 1
                        heapq.heappush(queue_next, ((ny, nx), ndr))

        queue = queue_next

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


main()
"""
3 5
.###.
.###.
.###.
"""