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:
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)]

if S[0][0] == '.':
    D[0][0] = 0
else:
    D[0][0] = 1

for i in range(1, h):
    if S[i][0] == '#' and S[i-1][0] == '.':
        D[i][0] = D[i-1][0] + 1
    else:
        D[i][0] = D[i-1][0]

for j in range(1, w):
    if S[0][j] == '#' and S[0][j-1] == '.':
        D[0][j] = D[0][j-1] + 1
    else:
        D[0][j] = D[0][j-1]

for i in range(1, h):
    for j in range(1, w):
        s = S[i][j]
        su = S[i-1][j]
        sl = S[i][j-1]
        if s == '.':
            D[i][j] = min(D[i-1][j], D[i][j-1])
        elif s == su:
            D[i][j] = min(D[i-1][j], D[i][j-1]+1)
        elif s == sl:
            D[i][j] = min(D[i-1][j]+1, D[i][j-1])
        else:
            D[i][j] = min(D[i-1][j]+1, D[i][j-1]+1)

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

Submission status: Wrong Answer

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

import sys

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
#print(S)
now='.'
ans=0
x,y=0,0
if S[x][y]=='#':
  ans+=1
  now='#'
  
def findroot(x,y,S,d,now):
  d1,X1,Y1=d,x,y
  if x<H-1 and S[x+1][y]==now:
    d1,X1,Y1=findroot(x+1,y,S,d+1,now)
  d2,X2,Y2=d,x,y
  if y<W-1 and S[x][y+1]==now:
    d2,X2,Y2=findroot(x,y+1,S,d+1,now)
  if d1>d2:
    return d1,X1,Y1
  else:
    return d2,X2,Y2
kouho=[(0,0)]
#print(kouho)
while 1:
  nextkouho=set()
  for x,y in kouho:
    if x<H-1 and S[x+1][y]==now:
      _,nextx1,nexty1=findroot(x+1,y,S,0,now)
      #print('under',nextx1,nexty1)
      nextkouho.add((nextx1,nexty1))
    else:
      nextkouho.add((x,y))
    if y<W-1 and S[x][y+1]==now:
      _,nextx2,nexty2=findroot(x,y+1,S,0,now)
      #print('right',nextx2,nexty2)
      nextkouho.add((nextx2,nexty2))
    else:
      nextkouho.add((x,y))
  if (H-1,W-1) in nextkouho:
    break
  if now=='#':
    now='.'
  else:
    ans+=1
    now='#'
  kouho=list(nextkouho)
  #print(nextkouho,kouho,now,ans)
print(ans)

Submission status: Wrong Answer

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

for i in range(H):
    for j in range(W):
        # (0,0)
        if i == 0 and j == 0:
            if s[i][j] == "#":
                dp[i][j] = 1
        # 1行目
        elif i == 0:
            if s[i][j-1] == "#":
                dp[i][j] = dp[i][j-1]
            else:
                if s[i][j] == "#":
                    dp[i][j] = dp[i][j-1] + 1
                else:
                    dp[i][j] = dp[i][j-1]
        # 1列目
        elif j == 0:
            if s[i-1][j] == "#":
                dp[i][j] = dp[i-1][j]
            else:
                if s[i][j] == "#":
                    dp[i][j] = dp[i-1][j] + 1
                else:
                    dp[i][j] = dp[i-1][j]
        else:
            # 上か左どちらの値が小さいか
            # 上側の方が小さい
            if dp[i-1][j] > dp[i][j-1]:
                if dp[i][j-1] == "#":
                    dp[i][j] = dp[i][j-1]
                else:
                    if s[i][j] == "#":
                        dp[i][j] = dp[i][j-1] + 1
                    else:
                        dp[i][j] = dp[i][j-1]
            # 左側の方が小さい
            elif dp[i-1][j] < dp[i][j-1]:
                if dp[i-1][j] == "#":
                    dp[i][j] = dp[i-1][j]
                else:
                    if s[i][j] == "#":
                        dp[i][j] = dp[i-1][j] + 1
                    else:
                        dp[i][j] = dp[i-1][j]
            # 等しい
            else:
                if s[i-1][j] == "#" or s[i][j-1] == "#":
                    dp[i][j] = dp[i-1][j]
                else:
                    if s[i][j] == "#":
                        dp[i][j] = dp[i-1][j] + 1
                    else:
                        dp[i][j] = dp[i-1][j]

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

Submission status: Wrong Answer

Solution code:
import heapq

h,w=map(int,input().split())
S=[]
for i in range(h):
    S.append(input())
    
visited=[[0]*w for _ in range(h)]

q=[(0,0,0)]

heapq.heapify(q)

cnt=0

while q:
    cnt,px,py=heapq.heappop(q)

    if px==h-1 and py==w-1:
        if cnt%2==0:
            print(cnt//2)
        else:
            print(cnt//2+1)
        break
    for x,y in [(1,0),(0,1)]:
        if x+px>=h or y+py>=w:
            continue
        if S[px+x][py+y]!=S[px][py]:
            heapq.heappush(q,(cnt+1,px+x,py+y))
        else:
            heapq.heappush(q,(cnt,px+x,py+y))

Submission status: Wrong Answer

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

dp=[]
#print(s)

for h in range(H):
    dp.append([0]*W)
    for w in range(W):
        #print(s[h][w],dp[h][w])
        if s[h][w]==".":
            if h!=0 and w==0:
                dp[h][w]=dp[h-1][w]
            elif h==0 and w!=0:
                dp[h][w]=dp[h][w-1]
            elif h==0 and w==0:
                dp[h][w]=0
            else:
                dp[h][w]=min(dp[h-1][w],dp[h][w-1])
        else:
            if h!=0 and w==0:
                dp[h][w]+=dp[h-1][w]+1
            elif h==0 and w!=0:
                dp[h][w]+=dp[h][w-1]+1
            elif h==0 and w==0:
                dp[0][0]=1
            else:
                dp[h][w]=min(dp[h-1][w],dp[h][w-1])+1
        #print(h,w)
        #for j in dp:
         #   print(j)
        #print(s[h][w],dp[h][w])
#print(dp)
print(dp[H-1][W-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int, input().split())
m = [list(input()) for i in range(0, H)]
counts = [[-1] * W for i in range(0, H)]
si, sj = 0, 0
if m[si][sj] == '#':
    counts[si][sj] = 1
else:
    counts[si][sj] = 0
q = deque([[si, sj]])
while q:
    i, j = q.popleft()
    for movei, movej in [[1, 0], [0, 1]]:
        ni, nj = i + movei, j + movej
        if (H <= ni or W <= nj):
            continue
        if m[ni][nj] == '#':
            c = counts[i][j] + 1
        else:
            c = counts[i][j]
        if counts[ni][nj] < 0:
            counts[ni][nj] = c
            if ni < H and nj < W:        
                q.append([ni, nj])
        else:
            if counts[ni][nj] > c:
                counts[ni][nj] = c
print(counts[H-1][W-1])

Submission status: Wrong Answer

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


def black_white(x):
    if x == '.':  # 白ならば1
        return 1
    elif x == '#':
        return -1  # 黒ならば-1


for i in range(H):
    fields.append(list(map(black_white, list(input()))))

if fields[0][0] == -1:
    cnt = 1
else:
    cnt = 0
positions = [(0, 0, cnt)]
cnts = []
while len(positions) > 0:
    r, c, cnt = positions.pop(0)
    if r == H-1 and c == W-1:
        cnts.append(cnt)
        continue
    if r+1 <= H-1 and fields[r+1][c] == 1:
        positions.append((r+1, c, cnt))
    elif r + 1 <= H-1:
        tmp_r = r + 1
        while tmp_r + 1 <= H-1 and fields[tmp_r+1][c] == -1:
            tmp_r += 1
        positions.append((tmp_r, c, cnt+1))
    if c+1 <= W-1 and fields[r][c+1] == 1:
        positions.append((r, c+1, cnt))
    elif c+1 <= W-1:
        tmp_c = c+1
        while tmp_c + 1 <= W-1 and fields[r][tmp_c+1] == -1:
            tmp_c += 1
        positions.append((r, tmp_c, cnt+1))

print(min(cnts))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)

H, W = map(int, input().split())
S = [input() for _ in range(H)]
cost = [[0]*W for _ in range(H)]
for w in range(W):
  if S[0][w] == '#':
    cost[0][w] = cost[0][w-1] + 1
  else:
    cost[0][w] = cost[0][w-1]
for h in range(H):
  if S[h][0] == '#':
    cost[h][0] = cost[h-1][0] + 1
  else:
    cost[h][0] = cost[h-1][0]

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

print(cost[-1][-1])

Submission status: Wrong Answer

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

d = (h - 1) + (w - 1)
r = 0
x = 0
y = 0

for i in range(d + 1):
    if s[x][y] == '#':
        r = r + 1
    if x != h - 1 and y != w - 1:
        if s[x + 1][y] =='.':
            x = x + 1
        else:
            y = y + 1
    elif x != h - 1:
        x = x + 1
    elif y != w - 1:
        y = y + 1
          
print(r)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int, input().split())
grid = [list(input()) for _ in range(H)]
asd=0
if grid[H-1][W-1]=='#':
    asd=1
else:
    asd=0
if  grid[0][0]=='#':
    asd+=1

grid[H-1][W-1]='g'
INF = 500**2
sy=sx=gy=gx=0
sy, sx = 0,0
gy, gx = H-1,W-1
current = deque([])
current.append((sy, sx))
dp = [[INF] * (W) for _ in range(H)]
dy = [1,0,]
dx = [0,1,]
dp[sy][sx] = 0
while len(current) > 0:
  cy, cx = current.popleft()
  if cy == gy and cx == gx:
    break
  for i in range(2):
    ny, nx = cy+dy[i], cx+dx[i]
    if 0 <= ny <= H-1 and 0 <= nx <= W-1 and dp[ny][nx] == INF:
      if grid[ny][nx] == '#':
        dp[ny][nx] = dp[cy][cx] + 1
        current.append((ny, nx))
      else:
        dp[ny][nx] = dp[cy][cx]
        current.appendleft((ny, nx))
print(dp[gy][gx]+asd)

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

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

Submission status: Wrong Answer

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


q = deque([[0, 0]])
depth = [[0]*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

visited[0][0] = True
while q:
    x, y = q.popleft()

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

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

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

Submission status: Wrong Answer

Solution code:
# import numpy as np
# import math
# import copy
from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    INF = 10 ** 9 + 7

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

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

    for i in range(1,H):
        cost = 0
        if s[i][0] == '#':
            cost += 1
        dp[i][0] = min(dp[i][0],dp[i-1][0] + cost)

    for j in range(1,W):
        cost = 0
        if s[0][j] == '#':
            cost += 1
        dp[0][j] = min(dp[0][j],dp[0][j-1] + cost)

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

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



main()

Submission status: Wrong Answer

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

import sys

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

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
h,w= [int(x) for x in input().split()]
maze = [[] for _ in range(h)]
for i in range(h):
  c = input()
  for j in range(w):
    maze[i].append(c[j])
#print(maze)

maps = [[100 for _ in range(w)] for _ in range(h)]

if maze[0][0] == ".":
  maps[0][0] = 0
else:
  maps[0][0] = 1

count = 0
for i in range(1,h):
  if maze[i][0] ==".":
    maps[i][0] = maps[i-1][0]
  else:
    if maze[i-1][0] == "#":
      maps[i][0] = maps[i-1][0]
    else:
      maps[i][0] = maps[i-1][0]+1
    
for i in range(1,w):
  if maze[0][i] ==".":
    maps[0][i] = maps[0][i-1]
  else:
    if maze[0][i-1] == "#":
      maps[0][i] = maps[0][i-1]
    else:
      maps[0][i] = maps[0][i-1]+1

#for i in maps:
#  print(i)
for i in range(1,h):
  for j in range(1,w):
    if maze[i][j] == ".":
      maps[i][j] = min(maps[i-1][j],maps[i][j-1])
    else:
      a = maps[i-1][j]
      b = maps[i][j-1]
      if a <= b:
        if maze[i-1][j] == "#":
          t = a
        else:
          t = a+1
      else:
        if maze[i][j-1] == "#":
          t = b
        else:
          t = b+1
      maps[i][j] = t
print(maps[-1][-1])

Submission status: Wrong Answer

Solution code:
line1 = input()
h, w = line1.split(" ")
h = int(h)
w = int(w)
masu_list = [["*" for j in range(w)] for i in range(h)]
for i in range(h):
    line = input()
    masu_list[i] = list(line)
henkan = 0

for i in masu_list:
    if "." in i:
        break
    henkan = w + h - 1
print(henkan)

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][j-1]==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]:
                        pat_mat[i].append(min(pat_mat[i][j-1],pat_mat[i-1][j]))
                    else:
                        pat_mat[i].append(min(pat_mat[i][j-1]+1,pat_mat[i-1][j]+1))
                        
                    
print(int(pat_mat[H-1][W-1]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def dfs(row, col, count):
    global n, m
    global grid
    print(row,col)
    if row == n-1 and col == m-1:
        if grid[row][col] == '.':
            count += 1
        counts.append(count)
        return
    else:
        if count + (n-1-row) + (m-1-col) <= max(counts):
            return
        if grid[row][col] == '.':
            count += 1
        if row+1 <= n-1:
            dfs(row+1,col,count)
        if col+1 <= m-1:
            dfs(row,col+1,count)


n, m = map(int, input().split())
counts = [0]
grid = [[0 for i in range(m)]for j in range(n)]
for row in range(n):
    temp = input()
    for col in range(m):
        grid[row][col] = temp[col]

dfs(0,0,0)
print(m+n-1 - max(counts))

Submission status: Wrong Answer

Solution code:
INF = float('inf') 

h, w = map(int, input().split())
field = []

for i in range(h):
    field.append(list(input()))

dp = [[INF for i in range(w)] for j in range(h)]
if field[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0
for i in range(1, w):
    if field[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 field[i][0] == "#":
        dp[i][0] = dp[i - 1][0] + 1
    else:
        dp[i][0] = dp[i - 1][0]

for y in range(1, h):
    for x in range(1, w):
        #print(dp[y - 1][x], dp[y][x - 1], dp[y][x], min(dp[y - 1][x], dp[y][x - 1], dp[y][x]))
        dp[y][x] = min(dp[y - 1][x], dp[y][x - 1], dp[y][x]) + (1 if field[y][x] == "#" else 0)
print(dp[h - 1][w - 1])

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = map(int, input().split())
fields = np.zeros([H, W], dtype=np.int8)


def black_white(x):
    if x == '.':  # 白ならば1
        return 1
    elif x == '#':
        return -1  # 黒ならば-1


for i in range(H):
    fields[i] = list(map(black_white, list(input())))

if fields[0, 0] == -1:
    cnt = 1
else:
    cnt = 0
positions = [(0, 0, cnt)]
cnts = []
while len(positions) > 0:
    r, c, cnt = positions.pop(0)
    if r == H-1 and c == W-1:
        cnts.append(cnt)
        continue
    if r+1 <= H-1 and fields[r+1, c] == 1:
        positions.append((r+1, c, cnt))
    elif r + 1 <= H-1:
        tmp_r = r + 1
        while tmp_r + 1 <= H-1 and fields[tmp_r + 1, c] == -1:
            tmp_r += 1
        positions.append((tmp_r, c, cnt+1))
    if c+1 <= W-1 and fields[r, c+1] == 1:
        positions.append((r, c+1, cnt))
    elif c+1 <= W-1:
        tmp_c = c+1
        while tmp_c + 1 <= W-1 and fields[r, tmp_c + 1] == -1:
            tmp_c += 1
        positions.append((r, tmp_c, cnt+1))

print(min(cnts))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[list(input()) for _ in range(h)]
q=[(0,0)]
g=[[1000]*w for  _ in range(h)]
g[0][0]=0
if s[0][0]=="#":
    g[0][0]=1
from collections import deque
q=deque([(0,0)])

while q:
    x,y=q.popleft()
    if x==w and y==h:
        break
    for dx,dy in ((1,0),(0,1)):
        if not(0 <= x+dx < w) or not(0 <= y+dy < h):
            continue
        elif s[y+dy][x+dx] == '#':
            g[y+dy][x+dx]=g[y][x]+1
        else:
            g[y+dy][x+dx]=g[y][x]
        q.append((x+dx,y+dy))
    

print(g[-1][-1])

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):
    if grid[i-1][j-1] == '.':
      dp[i][j] = min(dp[i-1][j], dp[i][j-1])
    else:
      dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1
      if i >= 3 and grid[i-2][j-1] == '#':
        dp[i][j] =  min(dp[i][j], dp[i-2][j])
      if j >= 3 and grid[i-1][j-2] == '#':
        dp[i][j] =  min(dp[i][j], dp[i][j-2])
        
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
import sys
import heapq
sys.setrecursionlimit(10000000)

h, w = map(int, input().split())
c = [input() for _ in range(h)]
 
d=[[100000]*w for _ in range(h)]
 
def bfs():
    if c[0][0] == '.':
        que = [(0, (0, 0))]
        heapq.heapify(que)
        d[0][0] = 0
    else:
        que = [(1, (0, 0))]
        heapq.heapify(que)
        d[0][0] = 1
    while len(que):
        _, (qx, qy) = heapq.heappop(que)
        if _ > d[h-1][w-1]:
          return
        for dx,dy in [[1,0], [0,1]]:
            px, py = qx+dx, qy+dy
            if 0<=px<h and 0<=py<w and d[px][py]>d[qx][qy]:
                d[px][py]=min(d[px][py], d[qx][qy] + (c[px][py]=='#'))
                heapq.heappush(que, (d[px][py], (px , py)))
bfs()
print(d[h-1][w-1])