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 sys
from collections import deque
sys.setrecursionlimit(10**7)
import numpy as np


def BFS(x, y):
    inf=float("inf")
    color=[["white" for _ in range(w+2)] for _ in range(h+2)]
    D=[[inf for _ in range(w+2)] for _ in range(h+2)]

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

    while len(queue)!=0:
        a,b=queue.popleft()
        for c,d in [[0,1],[1,0]]:
            X,Y=a+c,b+d
            if color[X][Y]=="white" and (1<=X<=h and 1<=Y<=w):
                color[X][Y]="gray"
                if S[X][Y]=="#":D[X][Y]=min(D[X-1][Y]+1,D[X][Y-1]+1)
                else:D[X][Y]=min(D[X-1][Y],D[X][Y-1])
                queue.append([X,Y])
        color[a][b]="black"
    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))


print(BFS(1,1))

Submission status: Wrong Answer

Solution code:
import numpy as np

h, w = map(int, input().split())
grid = np.array([input() for _ in range(h)])

cnt = np.zeros((h+1, w+1), dtype=np.int)
for i in range(h):
    for k in range(w):
        cnt[i+1][k+1] = max(cnt[i][k+1], cnt[i+1][k])
        if grid[i][k] == '.':
            cnt[i+1][k+1] += 1

print(h + w - 1 - cnt[h][w])

Submission status: Wrong Answer

Solution code:
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    ans = 0
    if S[0][0] == '#':
        ans += 1

    q = collections.deque()
    q.append([[0, 0], 0])

    d = [[10 ** 9] * W for j in range(H)]

    while q:
        c = q.pop()
        dist = c[1]

        j = c[0][0]
        i = c[0][1]

        if d[j][i] <= dist:
            continue
        else:
            d[j][i] = dist

        if i != W - 1:
            if S[j][i + 1] == '#':
                q.append([[j, i + 1], dist + 1])
            else:
                q.append([[j, i + 1], dist])

        if j != H - 1:
            if S[j + 1][i] == '#':
                q.append([[j + 1, i], dist + 1])
            else:
                q.append([[j + 1, i], dist])

    ans += d[H - 1][W - 1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

import collections
import heapq


class Solution:
    def solve(self, H, W, grid):
        queue = [(0, 0, 1 if grid[0][0] == '#' else 0)]
        distances = {(0, 0): 1 if grid[0][0] == '#' else 0}
        INF = float("inf")

        while queue:
            currX, currY, cost = heapq.heappop(queue)

            if distances.get((currX, currY), INF) < cost:
                continue

            for newX, newY in [(currX + 1, currY), (currX, currY + 1)]:
                if 0 <= newX < H and 0 <= newY < W:
                    extraCost = 1 if grid[newX][newY] == '#' else 0
                    altCost = cost + extraCost

                    if distances.get((newX, newY), INF) > altCost:
                        distances[(newX, newY)] = altCost
                        heapq.heappush(queue, (newX, newY, altCost))

        return distances[(H-1, W-1)]


sol = Solution()

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

grid = []

for _ in range(H):
    string = input().strip()
    row = [ch for ch in string]
    grid.append(row)

print(sol.solve(H, W, grid))

Submission status: Wrong Answer

Solution code:
h,w= map(int, input().split())
import numpy as np
x=np.zeros((h,w))
for i in range(h):
    s=input()
    for j in range(w):
        
        if i==0:
            if j==0:
                if s[j]=='.':
                    x[i][j]=0
                else:
                    x[i][j]=1
            else:
                if s[j]=='.':
                    x[i][j]=x[i][j-1]
                else:
                    x[i][j]=x[i][j-1]+1
        else:
            if j==0:
                if s[j]=='.':
                    x[i][j]=x[i-1][j]
                else:
                    x[i][j]=x[i-1][j]+1
            else:
                if s[j]=='.':
                    x[i][j]=min(x[i-1][j],x[i][j-1])
                else:
                    x[i][j]=min(x[i-1][j],x[i][j-1])+1
print(int(x[h-1][w-1]))

Submission status: Wrong Answer

Solution code:
import sys
INF = 10 ** 9 + 7

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):
        dp[y][x] = min(dp[y - 1][x], dp[y][x - 1])
        if field[y][x] == "#":
            dp[y][x] += 1


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

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l=[list(input()) for i in range(h)]
ans=[[0 for i in range(w)] for j in range(h)]
if l[0][0]=='#':
  ans[0][0]=1
for i in range(h):
  for j in range(w):
    if i!=0 or j!=0:
      if i!=0 and j!=0:
        if l[i][j]==l[i-1][j]:
          x=ans[i-1][j]
        else:
          x=ans[i-1][j]+1
        if l[i][j]==l[i][j-1]:
          y=ans[i][j-1]
        else:
          y=ans[i][j-1]+1
        ans[i][j]=min(x,y)
      elif i==0 and j!=0:
        if l[i][j]==l[i][j-1]:
          y=ans[i][j-1]
        else:
          y=ans[i][j-1]+1
        ans[i][j]=y
      else:
        if l[i][j]==l[i-1][j]:
          x=ans[i-1][j]
        else:
          x=ans[i-1][j]+1
        ans[i][j]=ans[i-1][j]
print(ans[h-1][w-1])

Submission status: Wrong Answer

Solution code:
import bisect
import collections
import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline
ACMOD = 1000000007
INF = 1 << 32


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


def llmi(n):
    return [lmi() for _ in range(n)]


H, W = lmi()
S = [input().strip() for _ in range(W)]

import functools


@functools.lru_cache(maxsize=None)
def solve(h, w, last_b=False):
    if h < 0 or w < 0:
        return INF
    if h == 0 and w == 0:
        return int(S[0][0] == '#')
    b = bool(S[h][w] == '#')
    return min(
        solve(h - 1, w, last_b=True) + (not last_b) + (not b),
        solve(h, w - 1, last_b=True) + (not last_b) + (not b),
        solve(h - 1, w, last_b=False) + b,
        solve(h, w - 1, last_b=False) + b,
    )


print(solve(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            
        if 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:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    h,w = LI()
    s = SR(h)
    b = [[inf] * w for i in range(h)]
    q = [(s[0][0] == "#", (0, 0))]
    b[0][0] = s[0][0] == "#"
    if s[0][0] == "#":
        x = 0
        y = 0
        p = 0
        f = 1
        for mx, my in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
            if mx == 1:
                mx += x
                my += y
                for mx in range(mx, w):
                    if s[my][mx] == ".":
                        if f:
                            break
                        if b[my][mx] > p:
                            b[my][mx] = p
                            heappush(q, (p, (my, mx)))
                    else:
                        if b[my][mx] > p + 1:
                            b[my][mx] = p + 1
                            heappush(q, (p + 1, (my, mx)))
                        f = 1
            elif my == 1:
                mx += x
                my += y
                for my in range(my, h):
                    if s[my][mx] == ".":
                        if f:
                            break
                        if b[my][mx] > p:
                            b[my][mx] = p
                            heappush(q, (p, (my, mx)))
                    else:
                        if b[my][mx] > p + 1:
                            b[my][mx] = p + 1
                            heappush(q, (p + 1, (my, mx)))
                        f = 1
            elif mx == -1:
                mx += x
                my += y
                for mx in range(mx, -1, -1):
                    if s[my][mx] == ".":
                        if f:
                            break
                        if b[my][mx] > p:
                            b[my][mx] = p
                            heappush(q, (p, (my, mx)))
                    else:
                        if b[my][mx] > p + 1:
                            b[my][mx] = p + 1
                            heappush(q, (p + 1, (my, mx)))
                        f = 1
            else:
                mx += x
                my += y
                for my in range(my, -1, -1):
                    if s[my][mx] == ".":
                        if f:
                            break
                        if b[my][mx] > p:
                            b[my][mx] = p
                            heappush(q, (p, (my, mx)))
                        f = 1

                    else:
                        if b[my][mx] > p + 1:
                            b[my][mx] = p + 1
                            heappush(q, (p + 1, (my, mx)))
                        f = 1
    while q:
        p,xy = heappop(q)
        y,x = xy
        for mx, my in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
            f = 0
            if mx == 1:
                mx += x
                my += y
                for mx in range(mx, w):
                    if s[my][mx] == ".":
                        if f:
                            break
                        if b[my][mx] > p:
                            b[my][mx] = p
                            heappush(q, (p, (my, mx)))
                    else:
                        if b[my][mx] > p + 1:
                            b[my][mx] = p + 1
                            heappush(q, (p + 1, (my, mx)))
                        f = 1
            elif my == 1:
                mx += x
                my += y
                for my in range(my, h):
                    if s[my][mx] == ".":
                        if f:
                            break
                        if b[my][mx] > p:
                            b[my][mx] = p
                            heappush(q, (p, (my, mx)))
                    else:
                        if b[my][mx] > p + 1:
                            b[my][mx] = p + 1
                            heappush(q, (p + 1, (my, mx)))
                        f = 1
            elif mx == -1:
                mx += x
                my += y
                for mx in range(mx, -1, -1):
                    if s[my][mx] == ".":
                        if f:
                            break
                        if b[my][mx] > p:
                            b[my][mx] = p
                            heappush(q, (p, (my, mx)))
                    else:
                        if b[my][mx] > p + 1:
                            b[my][mx] = p + 1
                            heappush(q, (p + 1, (my, mx)))
                        f = 1
            else:
                mx += x
                my += y
                for my in range(my, -1, -1):
                    if s[my][mx] == ".":
                        if f:
                            break
                        if b[my][mx] > p:
                            b[my][mx] = p
                            heappush(q, (p, (my, mx)))
                        f = 1
                            
                    else:
                        if b[my][mx] > p + 1:
                            b[my][mx] = p + 1
                            heappush(q, (p + 1, (my, mx)))
                        f = 1
    print(b[-1][-1] if b[-1][-1] else 0)

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

op = [[0 for i in range(W)] for j in range(H)]
op[0][0] = int(map[0][0] == '#')
for i in range(1, H):
    op[0][i] = op[0][i-1] + int(map[0][i] == '#' and map[0][i-1] == '.')
    op[i][0] = op[i-1][0] + int(map[i][0] == '#' and map[i-1][0] == '.')
for i in range(1, H):
    for j in range(1, W):
        op[i][j] = min(op[i-1][j] + int(map[i][j] == '#' and map[i-1][j] == '.'),
                       op[i][j-1] + int(map[i][j] == '#' and map[i][j-1] == '.'))
print(op[H-1][W-1])

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 if S[0][0]=='.' else 1))])
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:
h,w = list(map(int,input().split()))
s = []
hc = 0
wc = 0
res = 0

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

h = h-1
w = w-1

while True:
    if s[hc][wc] == "#":
        res += 1
    
    if hc == h and wc == w:
        break
    
    if hc < h and wc == w:        
        hc += 1
    elif hc == h and wc < w: 
        wc += 1
    else:
        if s[hc+1][wc] == ".":
            hc += 1
        elif s[hc][wc+1] == ".":
            wc += 1
        else:
            hc += 1

print(res)

Submission status: Wrong Answer

Solution code:
def main():
    import collections
    H, W = map(int, input().split())
    s = [list(input()) for i in range(H)]
    t = [[H*W]*W for i in range(H)]
    q = collections.deque()

    q.append((0, 0, 0))

    ans = H*W

    while(q):

        x, y, c = q.popleft()

        if s[y][x] == "#":
            c += 1
            t[y][x] += 1
        if x == W-1 and y == H-1:
            ans = min(ans, c)
            continue
        #print(x, y, c)
        if y < H-1 and (x == 0 or t[y+1][x] < H*W):
            t[y+1][x] = min(c, t[y+1][x])
            q.append((x, y+1, t[y+1][x]))
        else:
            if y < H-1:
                t[y+1][x] = c

        if x < W-1 and (y == 0 or t[y][x+1] < H*W):
            t[y][x+1] = min(c, t[y][x+1])
            q.append((x+1, y, t[y][x+1]))
        else:
            if x < W-1:
                t[y][x+1] = c
                #print(x, y, W-1, H-1, t[y][x+1], c)
        # print(t)
    print(ans)


main()

Submission status: Wrong Answer

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    H, W = iim()
    S = [input() for i in range(H)]
    c1 = "#"
    H1, W1 = H-1, W-1

    inf = H*W+1
    dq = [0]*W
    dq0 = [inf]*W
    dq0[0] = 0

    for y in range(H):
        dq[0] = dq0[0]
        if S[y][0] == c1:
            dq[0] += 1
        for x in range(1, W):
            dq[x] = min(dq[x-1], dq0[x])
            if S[y][x] == c1:
                dq[x] += 1
        dq, dq0 = dq0, dq
    print(dq0[-1])

if __name__ == "__main__":
    resolve()

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([10000]*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:
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:
                d[px][py]=min(d[px][py], d[qx][qy] + (c[px][py]=='#'))
                heapq.heappush(que, (d[px][py], (px , py)))

print(bfs())
print(d)

Submission status: Wrong Answer

Solution code:
import math
import fractions

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 ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans
'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)
'''


#a = list(map(int, input().split()))

h,w = list(map(int, input().split()))

s = []
for i in range(h):
    ss=[]
    a=input()
    for j in range(w):
        if(a[j]=='.'):
            ss.append(0)
        else:
            ss.append(1)
    s.append(ss)
        
reach = [[0 for i in range(w)] for j in range(h)]
total = 0

ans = 0
#print(s)
#print(reach)

for i in range(200):
    que=[[0,0]]
    done=0
    for j in range(h):
        for k in range(w):
            reach[j][k]=0
    
    while(len(que)>done):
        now = que[done]
        cand = []
        cand.append([now[0],min(w-1,now[1]+1)])
        cand.append([now[0],max(0,now[1]-1)])
        cand.append([min(h-1,now[0]+1),now[1]])
        cand.append([max(0,now[0]-1),now[1]])
        #print(cand)
        for j in range(4):
            here = [cand[j][0],cand[j][1]]
            #print(here)
            if(reach[here[0]][here[1]]==0 and s[here[0]][here[1]]==0):
                
                
                que.append([cand[j][0],cand[j][1]])
                reach[here[0]][here[1]]=1
            elif(reach[here[0]][here[1]]==0 and s[here[0]][here[1]]==1):
                reach[here[0]][here[1]]=1
                s[here[0]][here[1]]=0
        done+=1
        
    if(reach[h-1][w-1]==1):
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
h,w = list(map(int,input().split()))
s = []
hc = 0
wc = 0
res = 0

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

h = h-1
w = w-1

while True:
    if s[hc][wc] == "#":
        res += 1
    
    if hc == h and wc == w:
        break
    
    if hc < h and wc == w:        
        hc += 1
    elif hc == h and wc < w: 
        wc += 1
    else:
        if s[hc+1][wc] == ".":
            hc += 1
        elif s[hc][wc+1] == ".":
            wc += 1
        else:
            wc += 1

print(res)

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([100000000000000000000]*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())
s=[input() for i in range(H)]
m=[[None]*W for i in range(H)]
if s[0][0]==".":
    m[0][0]=0
    t=0
else:
    m[0][0]=1
    t=1
for i in range(W):
    if t%2==0:
        if s[0][i]=="#":
            t+=1
    else:
        if s[0][i]==".":
            t+=1
    m[0][i]=t
t=m[0][0]
for i in range(H):
    if t%2==0:
        if s[i][0]=="#":
            t+=1
    else:
        if s[i][0]==".":
            t+=1
    m[i][0]=t
for i in range(1,H):
    for j in range(1,W):
        if s[i][j]==s[i-1][j] and s[i][j]==s[i][j-1]:
            m[i][j]=min(m[i-1][j],m[i][j-1])
        elif s[i][j]==s[i-1][j]:
            m[i][j]=m[i-1][j]
        elif s[i][j]==s[i][j-1]:
            m[i][j]=m[i][j-1]
        else:
            m[i][j]=min(m[i-1][j],m[i][j-1])+1
print((m[-1][-1]+1)//2)

Submission status: Wrong Answer

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

# print(S)

INF = 10 ** 6

dp = [[INF] * W for i in range(H)]
if S[0][0] == ".":
    dp[0][0] = 0
else:
    dp[0][0] = 1

dx = [-1, 0, 0, 1]
dy = [0, 1, -1, 0]

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            continue
        if S[i][j] == "#":
            flip = 1
        else:
            flip = 0
        if i == 0:
            dp[i][j] = dp[i][j - 1] + flip
        elif j == 0:
            dp[i][j] = dp[i - 1][j] + flip
        else:
            dp[i][j] = min(dp[i - 1][j] + flip, dp[i][j - 1] + flip)

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

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split(' '))
move_list = [ list(input()) for _ in range(H) ]
possible_list = [[10**9]*W  for _ in range(H)]

def dfs(x,y,score):
    if x < 0 or H <= x or y < 0 or W <= y: #対象idxがareaの外にいる場合は対象外
        return
    if move_list[x][y] == '#':
        score +=1
    if possible_list[x][y] <= score:
        return
    possible_list[x][y] = score
    dfs(x+1,y,score)
    dfs(x,y+1,score)
dfs(0,0,0)
print(possible_list[H-1][W-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, -1, 0]
    dy = [0, 1, 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(4):
            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)
if l[0][0] == "#":
    print(1+dist[-1][-1]//2)
else:
    print(dist[-1][-1]//2)

Submission status: Wrong Answer

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

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

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

dp[0,:] = np.cumsum(S[0,:]=="#")
dp[:,0] = np.cumsum(S[:,0]=="#")

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

print(int(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

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] == "#":
                        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:
def main():
    h,w = map(int, input().split())
    s = []
    for i in range(h):
        ss = list(input())
        s.append(ss)

    now = [0,0]
    cnt = 0
    if s[0][0] == '#':
        cnt += 1
        s[0][0] = '.'
    if s[h-1][w-1] == '#':
        cnt += 1
        s[h-1][w-1] = '.'

    dp = [[-1] * w for i in range(h)]
    dp[0][0] = cnt

    for i in range(h):
        for j in range(w):
            tmp_up = 100000
            tmp_left = 100000
            change  = 0
            if s[i][j] == '#':
                change = 1
            if i == 0 and j == 0:
                continue
            if i != 0:
                tmp_up = dp[i-1][j] + change
            if j != 0:
                tmp_left = dp[i][j-1] + change

            dp[i][j] = min(tmp_up,tmp_left)


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

if __name__ == '__main__':
    main()