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:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    H, W = LI()
    g = []

    def update(dp, r, c):
        if r == 0 and c == 0:
            if g[r][c] == '#':
                return 1
            else:
                return 0

        cand1, cand2 = inf, inf
        if 0 <= r-1 < H:
            if g[r-1][c] == '.' and g[r][c] == '#':
                cand1 = dp[r-1][c] + 1
            else:
                cand1 = dp[r-1][c]
        if 0 <= c-1 < H:
            if g[r][c-1] == '.' and g[r][c] == '#':
                cand2 = dp[r][c-1] + 1
            else:
                cand2 = dp[r][c-1]
        return min(cand1, cand2)


    for _ in range(H):
        g.append(S())
    dp = [[0 for _ in range(W)] for __ in range(H)]
    for r in range(H):
        for c in range(W):
            dp[r][c] = update(dp, r, c)
    print(dp[H-1][W-1])
main()

Submission status: Wrong Answer

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

S=[list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        for x,y in ((1,0),(0,1)):
            X,Y=i+x,j+y
            if 0<=X<=h-1 and 0<=Y<w-1:
                if S[i][j]=="." and S[X][Y]=="#":D[X][Y]=D[i][j]+1
                else:D[X][Y]=D[i][j]

ans=D[h-1][w-1]
print(ans if S[0][0]!="#" else ans+1)

Submission status: Wrong Answer

Solution code:
def dp(maze, visited, gy, gx):
    cnt = 0
    for w in range(W):
        if maze[0][w] == "#":
            cnt += 1
        visited[0][w] = cnt
    cnt = 0        
    for h in range(H):
        if maze[h][0] == "#":
            cnt += 1
        visited[h][0] = cnt
    for h in range(1,H):
        for w in range(1,W):
            if maze[h][w] == "#":
                visited[h][w] = min(visited[h-1][w], visited[h][w-1]) + 1
            else:
                visited[h][w] = min(visited[h-1][w], visited[h][w-1])
    print(visited[-1][-1])            
                

H,W = map(int,input().split())
maze = [input() for _ in range(H)]
visited = [[0]*W for _ in range(H)]

dp(maze, visited, H-1, W-1)

Submission status: Wrong Answer

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

S=[list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        for x,y in ((1,0),(0,1)):
            X,Y=i+x,j+y
            if 0<=X<h and 0<=Y<w:
                if S[i][j]=="." and S[X][Y]=="#":
                    D[X][Y]=D[i][j]+1

ans=D[h-1][w-1]
print(ans if S[0][0]!="#" else ans+1)

Submission status: Wrong Answer

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


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]=="#":
                    if a==1 and b==1:D[X][Y]=1
                    else:D[X][Y]=min(D[X-1][Y]+1,D[X][Y-1]+1,D[X][Y])
                    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],D[X][Y])
                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))

color=[["white" for _ in range(w+2)] for _ in range(h+2)]
D=[[inf for _ in range(w+2)] for _ in range(h+2)]

print(BFS(1,1))

Submission status: Wrong Answer

Solution code:
def fn(x):
	return 1 if x == '#' else 0

h,w = map(int,input().split())
grid = []
for i in range(h):
	s = list(map(fn,list(input())))
	grid.append(s)

dp = [[(float('inf'),0)]*w]*h

dp[0][0] = (0,grid[0][0])

for i in range(h):
	for j in range(w):
		if (i == 0) and (j == 0):
			continue
		
		if i==0:
			if (dp[i][j-1][1] == 1) and (grid[i][j] == 0):
				dp[i][j] = (dp[i][j-1][0]+1,0)
			else:
				dp[i][j] =	(dp[i][j-1][0],grid[i][j])
			continue
		
		if j==0:
			if (dp[i-1][j][1] == 1) and (grid[i][j] == 0):
				dp[i][j] = (dp[i-1][j][0]+1,0)
			else:
				dp[i][j] = (dp[i][j-1][0],grid[i][j])
			continue

		if grid[i][j] == 0:
			if dp[i-1][j][1] == 1:
				if dp[i][j-1][1] == 1:
					dp[i][j] = (min(dp[i-1][j][0],dp[i][j-1][0])+1,grid[i][j])
				else:
					dp[i][j] = (min(dp[i-1][j][0]+1,dp[i][j-1][0]),grid[i][j])
			else:
				if dp[i][j-1][1] == 1:
					dp[i][j] = (min(dp[i-1][j][0],dp[i][j-1][0]+1),grid[i][j])
				else:
					dp[i][j] = (min(dp[i-1][j][0],dp[i][j-1][0]),grid[i][j])
		else:
			dp[i][j] = (min(dp[i-1][j][0],dp[i][j-1][0]),grid[i][j])

ans = dp[h-1][w-1][0] if (dp[h-1][w-1][1] == 0) else (dp[h-1][w-1][0]+1)

print(ans)

Submission status: Wrong Answer

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


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]=="#":
                    if a==1 and b==1:D[X][Y]=1
                    else:D[X][Y]=min(D[X-1][Y]+1,D[X][Y-1]+1,D[X][Y])
                    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))

color=[["white" for _ in range(w+2)] for _ in range(h+2)]
D=[[inf for _ in range(w+2)] for _ in range(h+2)]

print(BFS(1,1))

Submission status: Wrong Answer

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

dist[0][0] = 0
q = deque([(0, 0)])
while q:
	h, w = q.pop()

	if h < H-1 and dist[h+1][w] < 0:
		if s[h][w] == "." and s[h+1][w] == "#":
			dist[h+1][w] = dist[h][w] + 1
			q.appendleft((h+1, w))
		else:
			dist[h+1][w] = dist[h][w]
			q.append((h+1, w))

	if w < W-1 and dist[h][w+1] < 0:
		if s[h][w] == "." and s[h][w+1] == "#":
			dist[h][w+1] = dist[h][w] + 1
			q.appendleft((h, w+1))
		else:
			dist[h][w+1] = dist[h][w]
			q.append((h, w+1))

print(dist[H-1][W-1] + (s[0][0] == "#"))

Submission status: Wrong Answer

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

dp = [[0 for _ in range(W)] for _ in range(H)]
if masu[0][0] == '.':
	dp[0][0] = 0
else:
	dp[0][0] = 1
    
for i in range(H-1):
	if masu[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 masu[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 masu[i+1][j+1]=='.':
			dp[i+1][j+1] = min(dp[i][j+1], dp[i+1][j])
		else:
			dp[i+1][j+1] = min(dp[i][j+1], dp[i+1][j]) + 1
            
print(dp[H-1][W-1])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
from re import split
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy
import re

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


h, w = LI()
L = SR(h)
dp = [[10] * w for _ in range(h)]
dp[0][0] = 1 if L[0][0] == '#' else 0
for y in range(h):
    for x in range(w):
        if y:
            if L[y][x] == '.' or L[y - 1][x] == '#':
                dp[y][x] = dp[y - 1][x]
            else:
                dp[y][x] = dp[y - 1][x] + 1
        if x:
            if L[y][x] == '.' or L[y][x - 1] == '#':
                dp[y][x] = min(dp[y][x], dp[y][x - 1])
            else:
                dp[y][x] = min(dp[y][x], dp[y][x - 1] + 1)


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

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
grid = []
for i in range(h):
  s = input()
  grid.append(s)
dp = [[0] * w for i in range(h)]
for i in range(h):
  for j in range(w):
    if i == 0:
      if j == 0:
        if grid[i][j] == '#':
          dp[i][j] = 1
      else:
        dp[i][j] = dp[i][j-1]
        if grid[i][j] == '#':
          dp[i][j] += 1
    else:
      if j == 0:
        dp[i][j] = dp[i-1][j]
        if grid[i][j] == '#':
          dp[i][j] += 1
      else:
        m = min(dp[i][j-1], dp[i-1][j])
        dp[i][j] = m
        if grid[i][j] == '#':
          dp[i][j] += 1
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
import numpy as np
import math
import itertools
 
H, W = map(int, input().split())
mat = np.zeros((H, W))
 
for i in range(H):
  lines = list(input())
  for j in range(W):
    if lines[j]=='#':
      mat[i,j] = 1
      
min_score = 1000
 
H_count = ''.join(str(0) for i in range(H-1))
W_count = ''.join(str(1) for i in range(W-1))
roots = H_count + W_count
for root in itertools.permutations(roots):
  state = np.zeros((H, W))
  h = 0
  w = 0
  state[h,w] = 1
  for a in root:
    if a == '0':
      h +=1
    else:
      w +=1
    state[h,w] = 1
    score = int(np.sum(mat * state))
  if score >= min_score:
    break
  min_score = score
 
print(min_score)

Submission status: Wrong Answer

Solution code:
inp = list(map(lambda x: int(x), input().split(" ")))
H = inp[0]
W = inp[1]

field = [""]*H

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

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

if (field[0][0] == "#"):
    dp[0][0] = 1

for i in range(H):
    for j in range(W):
        check = 0
        if (i>0 and j>0):
            if (field[i][j]=="#"):
                check = 1
            dp[i][j] = min(dp[i-1][j]+check, dp[i][j-1]+check)
        elif (i>0 and j==0):
            if (field[i][j]=="#"):
                check = 1
            dp[i][j] = dp[i-1][j]+check
        elif (i==0 and j>0):
            if (field[i][j]=="#"):
                check = 1
            dp[i][j] = dp[i][j-1]+check

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

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
route = []
memo= [0 for z in range(w + h - 1)]

def serch(x,y):
    memo[(x + y)] = s[x][y]
    if x==h-1 and y==w-1:
        route.append(memo)
        return
    elif x==h-1:
        serch(x, y+1)
        return
    elif y==w-1:
        serch(x+1, y)
        return
    else:
        serch(x+1, y)
        serch(x, y+1)
        return
serch(0, 0)
cnt = 0
result = 0
for i in range(len(route)):
    for k in range(len(route[i])-1):
        if route[i][k] != route[i][k+1]:
            cnt += 1
    if result <cnt + int(s[0][0]=="#"):
        result = cnt + int(s[0][0]=="#")
    cnt = 0
print((result+1)//2)

Submission status: Wrong Answer

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

save=[[100000]*(W) for i in range(H)]

if L[0][0]=="#":
    save[0][0]=1
else:
    save[0][0]=0

H_LAST=H-1
W_LAST=W-1
    
for b in range(H-1):
    for r in range(W-1):
        nr=r+1
        nb=b+1

        if L[b][nr]=="#":
            save[b][nr]=min(save[b][r]+1,save[b][nr])
        else:
            save[b][nr]=min(save[b][r],save[b][nr])
            
        if L[nb][r]=="#":
            save[nb][r]=min(save[b][r]+1,save[nb][r])
        else:
            save[nb][r]=min(save[b][r],save[nb][r])

    for i in range(H_LAST):
        nb=i+1

        if L[nb][W_LAST]=="#":
            save[nb][W_LAST]=min(save[i][W_LAST]+1,save[nb][W_LAST])
        else:
            save[nb][W_LAST]=min(save[i][W_LAST],save[nb][W_LAST])

for i in range(W_LAST):
    nr=i+1
    if L[H_LAST][nr]=="#":
        save[H_LAST][nr]=min(save[H_LAST][i]+1,save[H_LAST][nr])
    else:
        save[H_LAST][nr]=min(save[H_LAST][i],save[H_LAST][nr])

print(save[H_LAST][W_LAST])

Submission status: Wrong Answer

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

save=[[100000]*(W) for i in range(H)]

if L[0][0]=="#":
    save[0][0]=1
else:
    save[0][0]=0

H_LAST=H-1
W_LAST=W-1
    
for b in range(H-1):
    for r in range(W-1):
        nr=r+1
        nb=b+1

        if L[b][nr]=="#":
            save[b][nr]=min(save[b][r]+1,save[b][nr])
        else:
            save[b][nr]=min(save[b][r],save[b][nr])
            
        if L[nb][r]=="#":
            save[nb][r]=min(save[b][r]+1,save[nb][r])
        else:
            save[nb][r]=min(save[b][r],save[nb][r])

    for i in range(H_LAST):
        nb=i+1
        print(nb)
        if L[nb][W_LAST]=="#":
            save[nb][W_LAST]=min(save[i][W_LAST]+1,save[nb][W_LAST])
        else:
            save[nb][W_LAST]=min(save[i][W_LAST],save[nb][W_LAST])

for i in range(W_LAST):
    nr=i+1
    if L[H_LAST][nr]=="#":
        save[H_LAST][nr]=min(save[H_LAST][i]+1,save[H_LAST][nr])
    else:
        save[H_LAST][nr]=min(save[H_LAST][i],save[H_LAST][nr])

print(save[H_LAST][W_LAST])

Submission status: Wrong Answer

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

save=[[100000]*(W) for i in range(H)]

if L[0][0]=="#":
    save[0][0]=1
else:
    save[0][0]=0

H_LAST=H-1
W_LAST=W-1
    
for b in range(H-1):
    for r in range(W-1):
        nr=r+1
        nb=b+1

        if L[b][nr]=="#":
            save[b][nr]=min(save[b][r]+1,save[b][nr])
        else:
            save[b][nr]=min(save[b][r],save[b][nr])
            
        if L[nb][r]=="#":
            save[nb][r]=min(save[b][r]+1,save[nb][r])
        else:
            save[nb][r]=min(save[b][r],save[nb][r])

    for i in range(b,H_LAST):
        nb=i+1

        if L[nb][W_LAST]=="#":
            save[nb][W_LAST]=min(save[i][W_LAST]+1,save[nb][W_LAST])
        else:
            save[nb][W_LAST]=min(save[i][W_LAST],save[nb][W_LAST])

for i in range(W_LAST):
    nr=i+1
    if L[H_LAST][nr]=="#":
        save[H_LAST][nr]=min(save[H_LAST][i]+1,save[H_LAST][nr])
    else:
        save[H_LAST][nr]=min(save[H_LAST][i],save[H_LAST][nr])

print(save[H_LAST][W_LAST])

Submission status: Wrong Answer

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

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

for h in range(H):
    for w in range(W):
        dp[h+1][w+1] = min(dp[h][w+1] + (board[h][w]=='#' and (h==0 or board[h-1][w]=='')), dp[h+1][w] + (board[h][w]=='#' and (w==0 or board[h][w-1]=='')))
 
            

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

Submission status: Wrong Answer

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

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

for h in range(H):
    for w in range(W):
        dp[h+1][w+1] = min(dp[h][w+1], dp[h+1][w])
        if board[h][w]=='#':
            dp[h+1][w+1] += 1
            
print (dp[-1][-1])

Submission status: Wrong Answer

Solution code:

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

A = []

point = 0
for i in range(H):
    P = input()
    P = list(P)
    if point == 0:
        A.append(P)
    else:
        A.vstack(P)
        
if A[0][0] == '.':
    num = 0
else:
    num = 1
if A[H-1][W-1] == '.':
    o = 1
else:
    num += 1
h = 0
w = 0
if H > W:
    while point <= W-2:
        if A[h+1][w] == '.':
            h += 1
        elif A[h][w+1] == '.':
            w += 1
            point += 1
        else:
            num += 1
            h += 1
            w += 1
            point += 1
    for i in range(H-W):
        if A[i+W][W-1] == '#':
            num +=1
            
elif H < W:
    while point <= H-2:
        if A[h+1][w] == '.':
            h += 1
            point += 1
        elif A[h][w+1] == '.':
            w += 1
            
        else:
            num += 1
            h += 1
            w += 1
            point += 1

    for i in range(H-W):
        if A[H-1][i+H] == '#':
            num +=1
        
else:
    while point <= W-2:
        if A[h+1][w] == '.':
            h += 1
            point += 1
        elif A[h][w+1] == '.':
            w += 1
            point += 1
        else:
            num += 1
            h += 1
            w += 1
            point += 1
print(num)

Submission status: Wrong Answer

Solution code:

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

A = []

point = 0
for i in range(H):
    P = input()
    P = list(P)
    if point == 0:
        A.append(P)
    else:
        A.vstack(P)
        
if A[0][0] == '.':
    num = 0
else:
    num = 1
if A[H-1][W-1] == '.':
    o = 1
else:
    num += 1
h = 0
w = 0
if H > W:
    while point <= W-2:
        if A[h+1][w] == '.':
            h += 1
        elif A[h][w+1] == '.':
            w += 1
            point += 1
        else:
            num += 1
            #h += 1
            w += 1
            point += 1
    for i in range(H-W):
        if A[i+W][W-1] == '#':
            num +=1
            
elif H < W:
    while point <= H-2:
        if A[h+1][w] == '.':
            h += 1
            point += 1
        elif A[h][w+1] == '.':
            w += 1
            
        else:
            num += 1
            h += 1
            #w += 1
            point += 1

    for i in range(H-W):
        if A[H-1][i+H] == '#':
            num +=1
        
else:
    while point <= W-2:
        if A[h+1][w] == '.':
            h += 1
            point += 1
        elif A[h][w+1] == '.':
            w += 1
            point += 1
        else:
            num += 1
            h += 1
            w += 1
            point += 1
print(num)

Submission status: Wrong Answer

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

dp = [[float("inf")] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        is_black = int(s[i][j] == "#")
        if j:
            dp[i][j] = min(dp[i][j], dp[i][j - 1] + is_black)
        if i:
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + is_black)
        if (i == 0) and (j == 0):
            dp[i][j] = is_black
ans = dp[-1][-1]

print(ans)

Submission status: Wrong Answer

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

dp = [[H + W - 1] * W for _ in range(H)]

for i in range(H):
    for j in range(W):
        is_black = int(s[i][j] == "#")
        if j:
            dp[i][j] = min(dp[i][j], dp[i][j - 1] + is_black)
        if i:
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + is_black)
        if (i == 0) and (j == 0):
            dp[i][j] = is_black
ans = dp[-1][-1]

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
map1 = [list(input()) for _ in range(h)]
check_board = [[3000]*w for _ in range(h)]
for x in range(h):
	for y in range(w):
		map1[x][y] = 1 if map1[x][y] =="#" else 0
check_board[0][0] = map1[0][0]
def cal_ans(a,b,x,y):
	return ((map1[a][b]^map1[x][y])&map1[x][y])+check_board[x][y]
for x in range(h):
	for y in range(w):
		if x < h-1:
			check_board[x+1][y] = min(check_board[x+1][y],cal_ans(x+1,y,x,y))
		if y < w-1:
			check_board[x][y+1] = min(check_board[x][y+1],cal_ans(x,y+1,x,y))
print(check_board[-1][-1])

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
map1 = [list(input()) for _ in range(h)]
check_board = [[300]*w for _ in range(h)]
for x in range(h):
	for y in range(w):
		map1[x][y] = 1 if map1[x][y] =="#" else 0
check_board[0][0] = map1[0][0]
def cal_ans(a,b,x,y):
	return ((map1[a][b]^map1[x][y])&map1[x][y])+check_board[x][y]
for x in range(h):
	for y in range(w):
		if x < h-1:
			check_board[x+1][y] = min(check_board[x+1][y],cal_ans(x+1,y,x,y))
		if y < w-1:
			check_board[x][y+1] = min(check_board[x][y+1],cal_ans(x,y+1,x,y))
print(check_board[-1][-1])

Submission status: Wrong Answer

Solution code:
# Problem A - Range Flip Find Route

# input
h, w = map(int, input().split())
s_board = [['']*w for i in range(h)]
for i in range(h):
    s = list(input())
    for j in range(w):
        s_board[i][j] = s[j]

# initialization
INF = 10**5
dp = [[INF]*w for i in range(h)]
if s_board[i][j]=='#':
    dp[0][0] = 1
else:
    dp[0][0] = 0

# count
for i in range(h):
    for j in range(w):
        # skip
        if i==0 and j==0:
            continue

        # left
        if j-1>=0:
            if s_board[i][j]=='#' and s_board[i][j-1]=='.':
                dp[i][j] = min(dp[i][j], dp[i][j-1] + 1)
            else:
                dp[i][j] = min(dp[i][j], dp[i][j-1])

        # up
        if i-1>=0:
            if s_board[i][j]=='#' and s_board[i-1][j]=='.':
                dp[i][j] = min(dp[i][j], dp[i-1][j] + 1)
            else:
                dp[i][j] = min(dp[i][j], dp[i-1][j])

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

Submission status: Wrong Answer

Solution code:
def main():
  import sys
  input=sys.stdin.buffer.readline
  h,w=map(int,input().split())
  s=input()
  a=x=s[0]>>3
  b=y=5-a
  d=[(b,a)]
  for i in range(1,w):
    f=s[i]>>3
    b+=a^f
    a=f
    d+=(b,a),
  for _ in range(1,h):
    s=input()
    f=s[0]>>3
    y+=x^f
    b=y
    a=x=f
    p=[(b,a)]
    for i in range(1,w):
      f=s[i]>>3
      b+=a^f
      c,g=d[i]
      c+=f^g
      if b>c:b,f=c,g
      a=f
      p+=(b,a),
    d=p
  print(b+1>>1)
main()

Submission status: Wrong Answer

Solution code:
def main():
  import sys
  input=sys.stdin.buffer.readline
  h,w=map(int,input().split())
  s=input()
  a=b=x=y=s[0]==35and 1or 0
  d=[(b,a)]
  for i in range(1,w):
    f=s[i]==35
    b+=a^f
    a=f
    d+=(b,a),
  for _ in range(1,h):
    s=input()
    f=s[0]==35
    y+=x^f
    b=y
    a=x=f
    p=[(b,a)]
    for i in range(1,w):
      f=s[i]==35
      b+=a^f
      c,g=d[i]
      c+=f^g
      if b>c:b,f=c,g
      a=f
      p+=(b,a),
    d=p
  print(b+1>>1)
main()

Submission status: Wrong Answer

Solution code:
def main():
  import sys
  input=sys.stdin.buffer.readline
  h,w=map(int,input().split())
  s=input()
  a=b=x=y=s[0]==35
  d=[(b,a)]
  for i in range(1,w):
    f=s[i]==35
    b+=a^f
    a=f
    d+=(b,a),
  for _ in range(1,h):
    s=input()
    f=s[0]==35
    y+=x^f
    b=y
    a=x=f
    p=[(b,a)]
    for i in range(1,w):
      f=s[i]==35
      b+=a^f
      c,g=d[i]
      c+=f^g
      if b>c:b,f=c,g
      a=f
      p+=(b,a),
    d=p
  print(b+1>>1)
main()

Submission status: Wrong Answer

Solution code:
import sys
h, w = list(map(int, sys.stdin.readline().split()))
graph = [list(sys.stdin.readline().rstrip()) for _ in range(h)]

dp = [[10**5 for _ in range(w+1)] for _ in range(h+1)]
dp[0][1] = 0
dp[1][0] = 0

for j in range(1, h+1):
    for i in range(1, w+1):
        if graph[j-1][i-1] == '.':
            dp[j][i] = min(dp[j-1][i], dp[j][i-1])
        else:
            dp[j][i] = min(dp[j-1][i], dp[j][i-1])+1

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

Submission status: Wrong Answer

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


def change(val):
    # 黒なら1
    if val == '#':
        return 1

    # 白なら0
    return 0


# def calc_root(board):


def main():
    h, w = map(int, input().split())
    board = [[s for s in input()] for i in range(h)]

    cnt = 200
    dp = [[200] * w for i in range(h)]

    for i_row, row in enumerate(dp):
        for i_col, col in enumerate(row):
            if i_row == 0 and i_col == 0:
                dp[i_row][i_col] = change(board[i_row][i_col])
                continue

            if i_row >= 1:
                if board[i_row][i_col] == '#' and board[i_row - 1][i_col] == '.':
                    dp[i_row][i_col] = min(dp[i_row - 1][i_col] + 1, dp[i_row][i_col])
                else:
                    dp[i_row][i_col] = dp[i_row - 1][i_col]

            if i_col >= 1:
                if board[i_row][i_col] == '#' and board[i_row][i_col - 1] == '.':
                    dp[i_row][i_col] = min(dp[i_row][i_col - 1] + 1, dp[i_row][i_col])
                else:
                    dp[i_row][i_col] = dp[i_row][i_col - 1]

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


if __name__ == '__main__':
    main()

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)]
dp[0][0] = 1 if S[0][0] == "#" else 0

for i in range(1,H):
    for j in range(1,W):
        up = dp[i-1][j] + (1 if S[i-1][j] != "#" and S[i][j] == "#" else 0) 
        left = dp[i][j-1] + (1 if S[i][j-1] != "#" and S[i][j] == "#" else 0)
        dp[i][j] = min(up, left)
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

h,w = nm()
g = [list(map(lambda s:s=='#', list(input()))) for _ in range(h)]
dp = [[h*w]*w for _ in range(h)]
q = deque()
q.append((0,g[0][0]))
while q:
    v,d = q.popleft()
    if v == h*w-1: print(int(d)); break
    y,x = divmod(v,w)
    if dp[y][x] <= d: continue
    dp[y][x] = d
    if y < h-1:
        if g[y+1][x] < g[y][x]:
            q.append((v+w, d+1))
        else:
            q.appendleft((v+w, d))
    if x < w-1:
        if g[y][x+1] < g[y][x]:
            q.append((v+1, d+1))
        else:
            q.appendleft((v+1, d))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
dp=[0]*W
if S[0][0]=='#':
  dp[0]=1
for j in range(1,W):
  if S[0][j]!=S[0][j-1]:
    dp[j]=dp[j-1]+1
  else:
    dp[j]=dp[j-1]

for i in range(1,H):
  if S[i][0]!=S[i-1][0]:
    dp[0]=dp[0]+1
  for j in range(1,W):
    hidari=1 if S[i][j]!=S[i][j-1] and S[i][j-1]=='.' else 0
    ue=1 if S[i][j]!=S[i-1][j] and S[i-1][j]=='.' else 0
    dp[j]=min(dp[j-1]+hidari,dp[j]+ue)

print(dp[-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
dp=[0]*W
if S[0][0]=='#':
  dp[0]=1
for j in range(1,W):
  if S[0][j]!=S[0][j-1]:
    dp[j]=dp[j-1]+1
  else:
    dp[j]=dp[j-1]

for i in range(1,H):
  if S[i][0]!=S[i-1][0]:
    dp[0]=dp[0]+1
  for j in range(1,W):
    hidari=1 if S[i][j]!=S[i][j-1] and S[i][j-1]=='#' else 0
    ue=1 if S[i][j]!=S[i-1][j] and S[i-1][j]=='#' else 0
    dp[j]=min(dp[j-1]+hidari,dp[j]+ue)
print(dp[-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
dp=[0]*W
if S[0][0]=='#':
  dp[0]=1
for j in range(1,W):
  if S[0][j]!=S[0][j-1]:
    dp[j]=dp[j-1]+1
  else:
    dp[j]=dp[j-1]

for i in range(1,H):
  print(dp)
  if S[i][0]!=S[i-1][0]:
    dp[0]=dp[0]+1
  for j in range(1,W):
    hidari=1 if S[i][j]!=S[i][j-1] and S[i][j-1]=='#' else 0
    ue=1 if S[i][j]!=S[i-1][j] and S[i-1][j]=='#' else 0
    dp[j]=min(dp[j-1]+hidari,dp[j]+ue)
print(dp)
print(dp[-1])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
h,w=map(int,input().split())
s=[list(input())for i in range(h)]
t=[[""for i in range(w)] for j in range(h)]
t[0][0]=0
def f(x,y):
    if t[x][y]!="":
        return t[x][y]
    else:
        t[x][y]=min((f(x-1,y)+(s[x][y]!=s[x-1][y]) if x>0 else float("INF")),(f(x,y-1)+(s[x][y]!=s[x][y-1]) if y>0 else float("INF")))
        return t[x][y]
print(f(h-1,w-1)//2)