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 sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    H, W = map(int, input().split())
    S = stdin.read().splitlines()

    grid = [[0] * W for _ in [0] * H]
    if S[0][0] == '#':
        grid[0][0] = 1

    for i in range(1, W):
        if S[0][i] == '#':
            grid[0][i] = grid[0][i-1] + 1
        else:
            grid[0][i] = grid[0][i-1]

    for i in range(1, H):
        if S[i][0] == '#':
            grid[i][0] = grid[i-1][0] + 1
        else:
            grid[i][0] = grid[i-1][0]

    for r in range(1, H):
        for c in range(1, W):
            if S[r][c] == '#':
                grid[r][c] = min(grid[r-1][c], grid[r][c-1]) + 1
            else:
                grid[r][c] = min(grid[r-1][c], grid[r][c-1])
    print(grid[-1][-1])


main()

Submission status: Wrong Answer

Solution code:
H,W = map(int, input().split())			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7

M = list( list(input()) for _ in range(H) )
#=> [['.', '.', '.', '.'], ['.', 'S', 'S', '.'], ['.', '.', 'S', '.'], ['.', '.', '.', '.']]

M[0][0]=(0 if M[0][0]=='.' else 1)

h=0
w=0

for h in range(1,H):
	M[h][0]=M[h-1][0] + (0 if M[h][0]=='.' else 1)

for w in range(1,W):
	M[0][w]=M[0][w-1] + (0 if M[0][w]=='.' else 1)

#print(M)

for h in range(1,H):
	for w in range(1,W):
		M[h][w]= min(M[h][w-1],M[h-1][w]) + (0 if M[h][w]=='.' else 1)


#print(M)
print(M[H-1][W-1])

Submission status: Wrong Answer

Solution code:
import heapq
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 qx == h-1 and qy == w-1:
            return d[h-1][w-1]
        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]=d[qx][qy] + (c[px][py]=='#')
                heapq.heappush(que, (d[px][py], (px , py)))

print(bfs())

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def BellmanFord(g, s):
    V = list(g.keys())
    for v in g.values():
        V.extend(list(v.keys()))
    V = set(V)

    dist = defaultdict(lambda: sys.maxsize)
    pred = defaultdict(lambda: -1)

    dist[s] = 0

    N = len(V)
    for i in range(N+1):
        for u in g:
            for v in g[u]:
                if dist[v] > dist[u] + g[u][v]:
                    if i == N:
                        return 'inf'
                    dist[v] = dist[u] + g[u][v]
                    pred[v] = u
    return dist



@mt
def slv(H, W, S):
    g = defaultdict(dict)
    for i in range(H):
        for j in range(W):
            for x, y in ((0, 1), (1, 0), (0, -1), (-1, 0)):
                if 0 <= i + x < H and 0 <= j+y < W:
                    if S[i][j] == '.' and S[i+x][j+y] == '#':
                        c = 1
                    else:
                        c = 0
                    g[(i, j)][(i+x, j+y)] = c

    def bfs(s=(0, 0)):
        q = deque()
        q.append(s)
        done = {s}
        d = defaultdict(lambda: sys.maxsize)
        d[s] = 0
        while q:
            u = q.popleft()
            for v, c in g[u].items():
                if d[u] + c < d[v]:
                    d[v] = d[u] + c
                    if c == 0:
                        q.appendleft(v)
                    else:
                        q.append(v)
        print(d)
        return d

    return bfs()[(H-1, W-1)] + (1 if S[0][0] == '#' else 0)


def main():
    H, W = read_int_n()
    S = [read_str() for _ in range(H)]
    print(slv(H, W, S))

    # H = 100
    # W = 100
    # S = [''.join(random.choices('.#', k=W)) for _ in range(H)]
    # print(slv(H, W, S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

S = [input() for _ in range(H)]
p = [[0, 0, 0, 0]]
if S[0][0] == '#':
  p[0][3] = 1
for m in range(H+W-2):
  n = len(p)
  for i in range(n):
    x = p[i]
    if x[0] < H - 1:
      if x[3] == 1 and S[x[0]+1][x[1]] == '.':
        p.append([x[0]+1, x[1], x[2], 0])
      elif x[3] == 0 and S[x[0]+1][x[1]] == '#':
        p.append([x[0]+1, x[1], x[2]+1, 1])
      else:
        p.append([x[0]+1, x[1], x[2], x[3]])
    if x[1] < W - 1:
      if x[3] == 1 and S[x[0]][x[1]+1] == '.':
        p.append([x[0], x[1]+1, x[2], 0])
      elif x[3] == 0 and S[x[0]][x[1]+1] == '#':
        p.append([x[0], x[1]+1, x[2]+1, 1])
      else:
        p.append([x[0], x[1]+1, x[2], x[3]])
  p = p[n:]
print(min(p[i][2] for i in range(len(p))))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(10 ** 9)
# import numpy as np
# 答えの提出の時にi+1とj+1は忘れないように
h, w = map(int, input().split())
maze = [[-1 for i in range(w)] for j in range(h)]

INF = h*w
countBlack = [[INF for i in range(w)] for j in range(h)]

for i in range(h):
    maze[i] = input()

sx=0
sy=0
queue = deque()
queue.append((sy,sx))
countBlack[sy][sx] = int(maze[0][0]== '#')

while queue:
    y, x = queue.popleft()
    for i,j in [(1,0),(0,1)]:
        ny, nx = y+i, x+j
        if 0 <= nx < w and 0 <= ny < h:
            if countBlack[ny][nx] != INF and countBlack[y][x]+1 > countBlack[ny][nx]:
                continue
            if maze[ny][nx] == '#':
                tmp = countBlack[y][x] + 1
                countBlack[ny][nx] = min(countBlack[ny][nx], tmp)
            else:
                countBlack[ny][nx] = min(countBlack[ny][nx], countBlack[y][x])
            if ny != h-1 or nx != w-1:
                queue.append((ny, nx))

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[]
for i in range(n):
    s=list(input())
    l.append(s)
flag=0
#print(l)
if(l[0][0]=="#"):
    l[0][0]=1 
else:
    l[0][0]=0;
for i in range(1,n):
    if(l[i][0]=="#"):
        l[i][0]=l[i-1][0]+1 
    else:
        l[i][0]=l[i-1][0]
for i in range(1,m):
    if( l[0][i]=="#"):
        l[0][i]=l[0][i-1]+1 
    else:
        l[0][i]=l[0][i-1]

for i in range(1,n):
    for j in range(1,m):
        if(l[i][j]=="#"):
            #print("test")
            l[i][j]=min(l[i-1][j],l[i][j-1])+1
        else:
            l[i][j]=min(l[i-1][j],l[i][j-1])
#print(l)
print(l[n-1][m-1])

Submission status: Wrong Answer

Solution code:

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

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

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

Submission status: Wrong Answer

Solution code:
H,W = map(int, input().split())			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7

M = list( list(input()) for _ in range(H) )
#=> [['.', '.', '.', '.'], ['.', 'S', 'S', '.'], ['.', '.', 'S', '.'], ['.', '.', '.', '.']]

M[0][0]=(0 if M[0][0]=='.' else 1)

h=0
w=0

for h in range(1,H):
	M[h][0]=M[h-1][0] + (0 if M[h][0]=='.' else 1)

for w in range(1,W):
	M[0][w]=M[0][w-1] + (0 if M[0][w]=='.' else 1)

#print(M)

for h in range(1,H):
	for w in range(1,W):
		M[h][w]= min(M[h][w-1],M[h-1][w]) + (0 if M[h][w]=='.' else 1)


#print(M)
print(M[h][w])

Submission status: Wrong Answer

Solution code:
from fractions import gcd
# from datetime import date, timedelta
# from heapq import *
import heapq
import math
from collections import defaultdict, Counter, deque
from bisect import *
import itertools
import fractions
import sys
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
# input = sys.stdin.readline


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


def main():

    # print(100, 100)
    # for i in range(100):
    #     s=""
    #     for j in range(100):
    #         if j % 2 == 0:
    #             if i % 2 == 0:
    #                 s += "#"
    #             else:
    #                 s += "."
    #         else:
    #             if i % 2 == 0:
    #                 s += "."
    #             else:
    #                 s += "#"
    #     print(s)



    h, w = map(int, input().split())
    maze = [input() for i in range(h)]
    ans = 0
    if maze[0][0] == "#":
        ans+=1
    

    def Dijkstra_search(sx, sy):
        H = h
        W = w
        visited = [[False for i in range(W)] for j in range(H)]
        d = [[float("inf") for i in range(W)] for j in range(H)]  # now cost
        d[sx][sy] = 0

        queue = []
        heapq.heappush(queue, (0, (sx, sy)))
        while len(queue):
            cost, (x, y) = heapq.heappop(queue)

            if visited[x][y]:
                continue
            visited[x][y] = True

            for dx, dy in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
                nx = x + dx
                ny = y + dy
                if nx >= 0 and ny >= 0 and nx < H and ny < W:
                    if visited[nx][ny]:
                        continue

                    vd=cost
                    if maze[nx][ny] =="." and maze[x][y]=="#":
                        vd+=1
                    if d[nx][ny] > vd:
                        d[nx][ny] = vd
                        heapq.heappush(queue, (vd, (nx, ny)))

        return d[h-1][w-1]

    ans += Dijkstra_search(0,0)
    print(ans)
 


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
map = []
ansList = [[0] * w for i in range(h)]
#print(list(input()))
for x in range(h):
    a = list(input())
    map.append(a)

prev = ""
for y in range(h):
    for x in range(w):
        if y > 0 and x > 0: val = min(ansList[y - 1][x], ansList[y][x - 1])
        elif y == 0 and x == 0: val = 0
        elif y == 0: val = ansList[y][x - 1]
        elif x == 0: val = ansList[y - 1][x]

        if map[y][x] == "#" and prev != "#": ansList[y][x] = val + 1
        elif map[y][x] == "#" and prev == "#": ansList[y][x] = val
        else: ansList[y][x] = val
        prev = map[y][x]
print(ansList[h - 1][w - 1])

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().strip().split())
arr = []
for i in range(h):
    temp = list(input().strip())
    arr.append(temp)

dp = [[0 for _ in range(w)] for _ in range(h)] 
for i in range(h):
    for j in range(w):
        if i==0 and j==0:
            if arr[i][j]=='#':
                dp[i][j] = 1            
        elif arr[i][j]=='#':
            if i==0 :
                dp[i][j] = dp[i][j-1]+1
            elif j==0:
                dp[i][j] = dp[i-1][j]+1
            else:
                dp[i][j] = min(dp[i][j-1],dp[i-1][j])+1
        else:
            if i==0:
                dp[i][j] = dp[i][j-1]
            elif j==0:
                dp[i][j] = dp[i-1][j]
            else:
                dp[i][j] = min(dp[i][j-1],dp[i-1][j])
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
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
  
while x<H-1 or y<W-1:
  _,nextx,nexty=findroot(x,y,S,0,now)
  if nextx==x and nexty==y:
    if now=='#':
      now='.'
    else:
      ans+=1
      now='#'
  x,y=nextx,nexty
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h,w=map(int,input().split())
infi=10**10
D=[list(input()) for i in range(h)]
kotae=[[infi]*(w+1) for _ in range(h+1)]
# D[i][j]はD[h-1][w-1]まで
#dfs
stack=[]
stack.append((0,0,0))
da=[0,1]
db=[1,0]
for i in range(h):
    kotae[i][0]=0
for j in range(2):
    kotae[0][j]=0
for i in range(h):
    for j in range(w):
        kotae[i+1][j+1]=min(kotae[i][j+1], kotae[i+1][j])
        if D[i][j]=='#':
            kotae[i+1][j+1]+=1
print(kotae[h][w])

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w = map(int, input().split())
c = [list(input()) for _ in range(h)]
moves = ((1, 0), (0, 1))
sy = 0
sx = 0
ty = h-1
tx = w-1
st = 0
if c[0][0] == '#':
    st = 1
        
def bfs(sy, sx):
    dist = [[float('inf')]*w for _ in range(h)]
    dist[sy][sx] = 0 
    q = deque([[sy, sx]])
    while q:
        y, x = q.popleft()
        #print(q)
        for dy, dx in moves:
            ny = y + dy 
            nx = x + dx 
            if 0 <= ny < h and 0 <= nx < w and dist[ny][nx] == float('inf'):
                if c[ny][nx] == '#' and c[y][x] != '#':
                    dist[ny][nx] = dist[y][x] + 1
                    q.append([ny, nx])
                else:
                    dist[ny][nx] = dist[y][x]
                    q.appendleft([ny, nx])
 
    return dist 
 
dist = (bfs(0, 0))
print(dist[ty][tx] + st)

Submission status: Wrong Answer

Solution code:
import collections
import math
import operator as op
from functools import reduce
import numpy as np
import math
import bisect
import heapq


MOD = 1000000007


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


def main():
    H, W = get_inputs(int)
    # white: 1 / black: 0
    s = np.zeros((H, W), dtype=np.int)
    req_n_ops = np.empty((H, W), dtype=np.int)
    for i in range(H):
        row = get_input(str)
        for j, r in enumerate(row):
            if r == ".":
                s[i, j] = 1

    if s[0, 0] == 0:
        req_n_ops[0, 0] = 1
    else:
        req_n_ops[0, 0] = 0

    for l in range(1, W+H-1):
        for i in range(H):
            j = l - i
            if not(i >= 0 and i < W and j >= 0 and j < H):
                continue
            # print(i, j)
            if i >= 1:
                if s[i, j] == 0 and s[i-1, j] == 1:
                    x1 = req_n_ops[i-1, j] + 1
                else:
                    x1 = req_n_ops[i-1, j]
            else:
                x1 = 100000

            if j >= 1:
                if s[i, j] == 0 and s[i, j-1] == 1:
                    x2 = req_n_ops[i, j-1] + 1
                else:
                    x2 = req_n_ops[i, j-1]
            else:
                x2 = 100000

            req_n_ops[i, j] = min(x1, x2)


    print(int(req_n_ops[-1, -1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


dy = [1, 0]
dx = [0, 1]
def solve(y, x):
    for i in range(2):
        ny = y + dy[i]
        nx = x + dx[i]
        if ny < H and nx < W and memo[ny][nx] == -1:
            if canvas[ny][nx] == "#":
                canvas[ny][nx] = "."
                memo[ny][nx] = memo[y][x] + 1
                que.append((ny, nx))
            else:
                memo[ny][nx] = memo[y][x]
                que.appendleft((ny, nx))


H, W = map(int, input().split())
canvas = [list(input()) for _ in range(H)]
memo = [[-1]*W for _ in range(H)]

que = deque([(0, 0)])
if canvas[0][0] == "#":
    memo[0][0] = 1
    canvas[0][0] = "."
else:
    memo[0][0] = 0

while que:
    y, x = que.popleft()
    solve(y, x)

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

Submission status: Wrong Answer

Solution code:
X = 10**4
H, W = map(int, input().split())

# 入力マップ
map_data = [['.' for j in range(W)] for i in range(H)]
for i in range(H):
    dat = list(input())
    for j in range(W):
        map_data[i][j] = dat[j]

# 計算マップ
r_map_data = [[X for j in range(W+1)] for i in range(H+1)]
# 開始位置のみ0になるよう初期化
r_map_data[0][1] = 0
r_map_data[1][0] = 0

# (1,1)~(H,W)まで求める
for i in range(H):
    for j in range(W):
        val = 0
        # 真上もしくは、真左のうちの小さいほうを取得
        val = min([r_map_data[i][j+1], r_map_data[i+1][j]])

        # 対象のマスが白
        if(map_data[i][j] == '#'):
            val += 1
        r_map_data[i+1][j+1] = val

print(r_map_data[H][W])

Submission status: Wrong Answer

Solution code:

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

grid = tuple(tuple(map(lambda x: x == '.',input())) for _ in range(H))


dp = [float('inf')]*W
dp[0] = 0
prow = [True]*W
for row in grid:
    ndp = [None]*W
    ndp[0] = dp[0] + (row[0]!=prow[0])
    for i in range(1,W):
        ndp[i] = min(ndp[i-1]+int(row[i-1] and not row[i]),dp[i]+int(prow[i] and not row[i]))
    prow = row
    dp = ndp

print(dp[-1])

Submission status: Wrong Answer

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


from collections import deque

def solve(x,y):
    dx = [1, 0]
    dy = [0, 1]

    start = [x, y]
    dist = [[10**3]*w for i in range(h)]
    dist[y][x] = 0
    Q = deque()
    Q.append([start,l[0][0]])
    mx = 0

    while Q:
        p,color = Q.popleft()
        for i in range(2):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]

            if 0 <= nx < w and 0 <= ny < h:
                if l[ny][nx]!=color:
                    cost=1
                else:
                    cost=0
                if dist[ny][nx] > dist[p[1]][p[0]] + cost:
                    Q.append([[nx, ny],l[ny][nx]])
                    dist[ny][nx] = dist[p[1]][p[0]] + cost
    return dist

dist=solve(0,0)
print(dist)
if l[0][0] == "#" or l[-1][-1]=="#":
    print(1+dist[-1][-1]//2)
else:
    print(dist[-1][-1]//2)

Submission status: Wrong Answer

Solution code:
h,w= map(int,input().split(' '))

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

for i in range(h):
    row=input()
    for j in range(len(row)):
        if row[j]=='#':
            grid[i][j]=1

btdt= [[-1 for i in range(w)] for j in range(h)]
def go(i,j,count):
    global h,w,grid
    if i>=h or j>=w:
        return
    if btdt[i][j] != -1 and count >= btdt[i][j]:
        return
    if i==h-1 and j==w-1:
        if grid[i][j]==1:
            count+=1
        btdt[i][j]=count
        return
    if grid[i][j]==1:
        count+=1
    btdt[i][j] = count
    go(i + 1, j, count)
    go(i, j + 1, count)

go(0,0,0)
print(btdt[i][j])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)

h,w=map(int,input().split())
INF=10**10
s_org=[list(input()) for _ in range(h)]
s=[[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    if s_org[i][j]=="#":
      s[i][j]=1
dp=[[-1]*w for _ in range(h)]
dp[0][0]=s[0][0]
def func(i,j):
  if i<0 or j<0: return INF
  if dp[i][j]!=-1:return dp[i][j]
  a=func(i,j-1)+s[i][j-1]
  b=func(i-1,j)+s[i-1][j]
  if s[i][j]==1 and s[i][j-1]==1:
    a-=1
  if s[i][j]==1 and s[i-1][j]==1:
    b-=1
  val=min(a,b)
  dp[i][j]=val
  return val
print(func(h-1,w-1))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
m = []
for i in range(h):
    inp = input()
    tmpa = inp.replace('.', '0')
    tmpb = tmpa.replace('#', '1')
    m.append(tmpb)
# print(m)

ans = [[-1] * w for i in range(h)]
for i in range(h):
    for j in range(w):
        if i == 0:
            if j == 0:
                ans[i][j] = int(m[i][j])
            else:
                ans[i][j] = ans[i][j - 1] + int(m[i][j])
        elif j == 0:
            ans[i][0] = ans[i - 1][0] + int(m[i][j])

        else:
            ans[i][j] = min(ans[i - 1][j], ans[i][j - 1]) + int(m[i][j])
# print(ans)
print(ans[h - 1][w - 1])

Submission status: Wrong Answer

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

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


H,W = m()

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

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


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

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

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

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[input() for i in range(H)]
from collections import deque
q=deque([(0,0,0)])
v=[[-1]*W for i in range(H)]
v[0][0]=0
x=[(0,1),(0,-1),(1,0),(-1,0)]
while q:
    i,j,d=q.popleft()
    for a,b in x:
        if 0<=i+a<H and 0<=j+b<W and v[i+a][j+b]<0:
            if S[i+a][j+b]==S[i][j]:
                q.appendleft((i+a,j+b,d))
                v[i+a][j+b]=d
            else:
                q.append((i+a,j+b,d+1))
                v[i+a][j+b]=d+1
print(v[-1][-1]//2+(S[0][0]=='#' or S[-1][-1]=='#'))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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


if sw[0][0]=="#":
    print(1+result[h-1][w-1]//2)
else:
    print(result[h-1][w-1]//2)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w=map(int,input().split())
ans=0
field=[["*" for i in range(w+2)]]+[["*"]+list(input())+["*"] for i in range(h)]+[["*" for i in range(w+2)]]
queue=deque([(1,1)])
move=[(0,1),(1,0)]
ans=0
cnt=0
if field[1][1]==field[h][w]=="#":
    cnt=2
elif field[1][1]=="#" or field[h][w]=="#":
    cnt=1
while queue:
    flg=0
    pos=queue.popleft()
    color=field[pos[0]][pos[1]]
    for i in move:
      if field[pos[0]+i[0]][pos[1]+i[1]]=="*":continue
      if field[pos[0]+i[0]][pos[1]+i[1]]==color:
        if flg==-1:
            queue.pop()
            ans-=1
        queue.append((pos[0]+i[0],pos[1]+i[1]))
        flg=1
      else:
          if flg:continue
          else:
            ans+=1
            queue.append((pos[0]+i[0],pos[1]+i[1]))
            flg=-1
      if (pos[0]+i[0],pos[1]+i[1])==(h,w):print((ans+cnt)//2);exit()