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:
def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    ans = [[float("inf")] * (W+1) for _ in range(H+1)]
    ans[1][0] = ans[0][1] = 0
    for i in range(1, H+1):
        for j in range(1, W+1):
            if S[i-1][j-1] == "#":
                ans[i][j] = min(ans[i-1][j], ans[i][j-1]) + 1
            else:
                ans[i][j] = min(ans[i-1][j], ans[i][j-1])
    print(ans[H][W])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import product
from heapq import heappush, heappop

INF = float('inf')

def dijkstra(V, E, s):
    '''
    Input:
        V = list(range(N))
        E = defaultdict(dict)
        s (in V) : start vertex
    Output:
        dist: the list of the minimum cost from s to arbitrary vertex v in the given graph
    '''
    N = len(V)
    dist = [INF] * N
    heap = []
    dist[s] = 0; rem = N
    heappush(heap, (0, s))
    while heap and rem:
        d, v = heappop(heap)
        # don't need to check when the stored value is greater than provisional distance
        if d > dist[v]: continue
        rem -= 1;
        for u, c in E[v]:
            temp = d + c
            if dist[u] > temp:
                dist[u] = temp
                heappush(heap, (temp, u))
    return dist

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

def vtx(i, j): return i*W + j
def coord(n): return divmod(n, W)

grid = [input() for _ in range(H)] # |string| = W
V = range(H * W)
E = [[] for _ in V]
ans = 0 if grid[0][0] == '.' else 1
for i, j in product(range(H), range(W)):
    v = vtx(i, j)
    check = [vtx(i+dx, j+dy) for dx, dy in [(1, 0), (0, 1)] if i+dx <= H-1 and j+dy <= W-1]
    for u in check:
        x, y = coord(u)
        if grid[i][j] == '.' and grid[x][y] == '#':
            E[v].append((u, 1))
            E[u].append((v, 0))
        elif grid[i][j] == '#' and grid[x][y] == '.':
            E[v].append((u, 0))
            E[u].append((v, 1))
        else:
            E[v].append((u, 0))
            E[u].append((v, 0))
dist = dijkstra(V, E, 0)
ans += dist[vtx(H-1, W-1)]
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product
from heapq import heappush, heappop

INF = float('inf')

def dijkstra(V, E, s):
    '''
    Input:
        V = list(range(N))
        E = defaultdict(dict)
        s (in V) : start vertex
    Output:
        dist: the list of the minimum cost from s to arbitrary vertex v in the given graph
    '''
    N = len(V)
    dist = [INF] * N
    heap = []
    dist[s] = 0; rem = N
    heappush(heap, (0, s))
    while heap and rem:
        d, v = heappop(heap)
        # don't need to check when the stored value is greater than provisional distance
        if d > dist[v]: continue
        rem -= 1;
        for u, c in E[v]:
            temp = d + c
            if dist[u] > temp:
                dist[u] = temp
                heappush(heap, (temp, u))
    return dist

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

def vtx(i, j): return i*W + j
def coord(n): return divmod(n, W)

grid = [input() for _ in range(H)] # |string| = W
V = range(H * W)
E = [[] for _ in V]
ans = 0 if grid[0][0] == '.' else 1
for i, j in product(range(H), range(W)):
    v = vtx(i, j)
    check = [vtx(i+dx, j+dy) for dx, dy in [(1, 0), (0, 1)] if i+dx <= H-1 and j+dy <= W-1]
    for u in check:
        x, y = coord(u)
        if grid[i][j] == '.' and grid[x][y] == '#':
            E[v].append((u, 1))
            E[u].append((v, 1))
        else:
            E[v].append((u, 0))
            E[u].append((v, 0))
dist = dijkstra(V, E, 0)
ans += dist[vtx(H-1, W-1)]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

stdinr = sys.stdin.readline

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

h,w = nm()
g = [list(map(lambda s:s=='#', list(input()))) for _ in range(h)]
dp = [[h*w]*w for _ in range(h)]
dp[0][0] = g[0][0]
for i in range(h):
    for j in range(w):
        if i < h-1:
            d = dp[i][j] + (g[i][j] > g[i+1][j])
            if dp[i+1][j] > d: dp[i+1][j] = d
        if j < w-1:
            d = dp[i][j] + (g[i][j] > g[i][j+1])
            if dp[i][j+1] > d: dp[i][j+1] = d
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
from itertools import product
from collections import deque

def zero_one_bfs(E, s):
    N = len(E)
    order = [-1] * N # a bfs ordering of each vertex
    parent = [-1] * N # parent of each vertex in the bfs search tree
    dist = [-1] * N # the distance of each vertex from s
    dq = deque([(s, -1, 0)]) # (vertex, parent, dist)
    num = 0 # current ordering
    while dq:
        v, p, d = dq.popleft()
        if order[v] < 0: # visited v for the first time
            order[v] = num
            parent[v] = p
            dist[v] = d
            num += 1
            for u, w in E[v]:
                if order[u] >= 0: continue
                if w == 0: dq.appendleft((u, v, d))
                else: dq.append((u, v, d+1))
    return order, parent, dist

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

def vtx(i, j): return i*W + j
def coord(n): return divmod(n, W)

grid = [input() for _ in range(H)] # |string| = W
V = range(H * W)
E = [[] for _ in V]
ans = 0 if grid[0][0] == '.' else 1
for i, j in product(range(H), range(W)):
    v = vtx(i, j)
    check = [vtx(i+dx, j+dy) for dx, dy in [(1, 0), (0, 1)] if i+dx <= H-1 and j+dy <= W-1]
    for u in check:
        x, y = coord(u)
        if grid[i][j] == '.' and grid[x][y] == '#':
            E[v].append((u, 1))
            E[u].append((v, 1))
        else:
            E[v].append((u, 0))
            E[u].append((v, 0))
_, _, dist = zero_one_bfs(E, 0)
ans += dist[vtx(H-1, W-1)]
print(ans)

Submission status: Wrong Answer

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

que = deque()
alr = [[float('inf') for i in range(W)] for j in range(H)]
if s[0][0] == '.':
    alr[0][0] = 0
else:
    alr[0][0] = 1
que.append((0,0))
while que:
    h,w = que.popleft()
    if h < H-1:
        if s[h+1][w] == '.':
            if alr[h+1][w] > alr[h][w]:
                alr[h+1][w] = alr[h][w]
                que.append((h+1,w))
        else:
            if alr[h+1][w] > alr[h][w]+1:
                alr[h+1][w] =alr[h][w]+1
                que.append((h+1,w))            
    if w < W-1:
        if s[h][w+1] == '.':
            if alr[h][w+1] > alr[h][w]:
                alr[h][w+1] =alr[h][w]
                que.append((h,w+1))
        else:
            if alr[h][w+1] > alr[h][w]+1:
                que.append((h,w+1))
                alr[h][w+1] = alr[h][w] + 1
print(alr[-1][-1])

Submission status: Wrong Answer

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


def next(cx, cy):
    c, wx, wy = 0, 0, 0
    if mp[cy - 1][cx - 1] == "#":
        c = 1

    if cx != w:
        if memo[cy][cx + 1] == -1:
            wx = memo[cy][cx + 1] = next(cx + 1, cy) + c
        else:
            wx = memo[cy][cx + 1]
    if cy != h:
        if memo[cy + 1][cx] == -1:
            wy = memo[cy + 1][cx] = next(cx, cy + 1) + c
        else:
            wy = memo[cy + 1][cx]

    return c + min(wx, wy)


print(next(1, 1))

Submission status: Wrong Answer

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


def next(cx, cy):
    c, wx, wy = 0, 0, 0
    if mp[cy - 1][cx - 1] == "#":
        c = 1

    if memo[cy][cx] != -1:
        return memo[cy][cx]

    if cx != w:
        if memo[cy][cx + 1] == -1:
            wx = memo[cy][cx + 1] = next(cx + 1, cy) + c
        else:
            wx = memo[cy][cx + 1]
    if cy != h:
        if memo[cy + 1][cx] == -1:
            wy = memo[cy + 1][cx] = next(cx, cy + 1) + c
        else:
            wy = memo[cy + 1][cx]

    return c + min(wx, wy)


print(next(1, 1))

Submission status: Wrong Answer

Solution code:
from collections import deque
q = deque()
h,w=map(int,input().split())
global finished 
finished = set()
c=[]
for i in range(h):
    c.append(list(map(str,input())))
cans=[[300 for _ in range(w)] for _ in range(h)]
#print(cans)
q.append([0,0,0])
if c[h-1][w-1]=='#':
    asd=1
else:
    asd=0
    
c[h-1][w-1]='s'

def check(x,y):
    if 0<=x and x<h:
        if 0<=y and y<w:
            return True
    return False
global ans
ans=1000

def search():
    while (q):
        x,y,count=q.pop()
        #print(q)
        global ans
        
        if check(x,y)==False :
            continue

        else:
            if c[x][y]=='#':
                
                if c[max(0,x-1)][y]!='#' or c[x][max(y-1,0)]!='#':
                    if cans[x][y]==300 or count+1<cans[x][y]:
                        cans[x][y]=count+1
                        q.append([x,y+1,count+1])
                        q.append([x+1,y,count+1])
                    else:
                        continue
                        
                else:
                    cans[x][y]=count
                    q.append([x,y+1,count])
                    q.append([x+1,y,count])
                    
            else:
                if cans[x][y]==300 or count<cans[x][y]:
                    cans[x][y]=count
                    q.append([x,y+1,count])
                    q.append([x+1,y,count])
                else:
                    continue
                #print(ans)
     
search()
#for i in range(h):
 #   print(cans[i])9
           
print(cans[h-1][w-1]+asd)

Submission status: Wrong Answer

Solution code:
from collections import deque
q = deque()
h,w=map(int,input().split())
global finished 
finished = set()
c=[]
for i in range(h):
    c.append(list(map(str,input())))
cans=[[300 for _ in range(w)] for _ in range(h)]
#print(cans)
q.append([0,0,0])
if c[h-1][w-1]=='#':
    asd=1
else:
    asd=0
if c[0][0]=='#':
    asd+=1
c[h-1][w-1]='s'

def check(x,y):
    if 0<=x and x<h:
        if 0<=y and y<w:
            return True
    return False
global ans
ans=1000

def search():
    while (q):
        x,y,count=q.pop()
        #print(q)
        global ans
        
        if check(x,y)==False :
            continue

        else:
            if c[x][y]=='#':
                
                if c[max(0,x-1)][y]!='#' or c[x][max(y-1,0)]!='#':
                    if cans[x][y]==300 or count+1<cans[x][y]:
                        cans[x][y]=count+1
                        q.append([x,y+1,count+1])
                        q.append([x+1,y,count+1])
                    else:
                        continue
                        
                else:
                    cans[x][y]=count
                    q.append([x,y+1,count])
                    q.append([x+1,y,count])
                    
            else:
                if cans[x][y]==300 or count<cans[x][y]:
                    cans[x][y]=count
                    q.append([x,y+1,count])
                    q.append([x+1,y,count])
                else:
                    continue
                #print(ans)
     
search()
#for i in range(h):
 #   print(cans[i])9
           
print(cans[h-1][w-1]+asd)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=[[10101 for i in range(w)] for _ in range(h)]
s=[[_ for _ in input()] for _ in range(h)]
if s[0][0]=="#":
    ans[0][0]=1
else:
    ans[0][0]=0

for i in range(1,w):
    if s[0][i]=="#":
        if s[0][i-1]==".":
            ans[0][i]=ans[0][i-1]+1
    else:
        ans[0][i]=ans[0][i-1]

for i in range(1,h):
    if s[i][0]=="#":
        if s[i-1][0]==".":
            ans[i][0]=ans[i-1][0]+1
    else:
        ans[i][0]=ans[i-1][0]

for i in range(1,h):
    for j in range(1,w):
        if s[i][j]=="#" and s[i-1][j]==".":
            ans[i][j]=min(ans[i][j],ans[i-1][j]+1)
        elif s[i][j]=="#" and s[i-1][j]=="#":
            ans[i][j]=min(ans[i][j],ans[i-1][j])
        elif s[i][j]=="#" and s[i][j-1]==".":
            ans[i][j]=min(ans[i][j],ans[i][j-1]+1)
        elif s[i][j]=="#" and s[i][j-1]=="#":
            ans[i][j]=min(ans[i][j],ans[i][j-1])
        elif s[i][j]==".":
            ans[i][j]=min(ans[i-1][j],ans[i][j-1])

print(ans[-1][-1])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=[[10101 for i in range(w)] for _ in range(h)]
s=[[_ for _ in input()] for _ in range(h)]
if s[0][0]=="#":
    ans[0][0]=1
else:
    ans[0][0]=0

for i in range(1,w):
    if s[0][i]=="#":
        if s[0][i-1]==".":
            ans[0][i]=ans[0][i-1]+1
    else:
        ans[0][i]=ans[0][i-1]

for i in range(1,h):
    if s[i][0]=="#":
        if s[i-1][0]==".":
            ans[i][0]=ans[i-1][0]+1
    else:
        ans[i][0]=ans[i-1][0]

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

print(ans[-1][-1])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=[[10101 for i in range(w)] for _ in range(h)]
s=[[_ for _ in input()] for _ in range(h)]
if s[0][0]=="#":
    ans[0][0]=1
else:
    ans[0][0]=0

for i in range(1,w):
    if s[0][i]=="#":
        if s[0][i-1]=="#":
            ans[0][i]=ans[0][i-1]
        else:
            ans[0][i]=ans[0][i-1]+1
    else:
        ans[0][i]=ans[0][i-1]

for i in range(1,h):
    if s[i][0]=="#":
        if s[i-1][0]=="#":
            ans[i][0]=ans[i-1][0]
        else:
            ans[i][0]=ans[i-1][0]+1
    else:
        ans[i][0]=ans[i-1][0]

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


print(ans[-1][-1])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=[[10101 for i in range(w)] for _ in range(h)]
s=[[_ for _ in input()] for _ in range(h)]
if s[0][0]=="#":
    ans[0][0]=1
else:
    ans[0][0]=0

for i in range(1,w):
    if s[0][i]=="#":
        if s[0][i-1]=="#":
            ans[0][i]=ans[0][i-1]
        else:
            ans[0][i]=ans[0][i-1]+1
    else:
        ans[0][i]=ans[0][i-1]

for i in range(1,h):
    if s[i][0]=="#":
        if s[i-1][0]=="#":
            ans[i][0]=ans[i-1][0]
        else:
            ans[i][0]=ans[i-1][0]+1
    else:
        ans[i][0]=ans[i-1][0]

for i in range(1,h):
    for j in range(1,w):
        if s[i][j]=="#":
            if s[i-1][j]=="." :
                ans[i][j]=min(ans[i-1][j]+1,ans[i][j])
            elif s[i][j-1]==".":
                ans[i][j]=min(ans[i][j-1]+1,ans[i][j])
            else:
                ans[i][j]=min(ans[i-1][j],ans[i][j-1],ans[i][j])
        else:
            ans[i][j]=min(ans[i-1][j],ans[i][j-1],ans[i][j])

print(ans[-1][-1])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=[[10101 for i in range(w)] for _ in range(h)]
s=[[_ for _ in input()] for _ in range(h)]
if s[0][0]=="#":
    ans[0][0]=1
else:
    ans[0][0]=0

for i in range(1,w):
    if s[0][i]=="#":
        if s[0][i-1]=="#":
            ans[0][i]=ans[0][i-1]
        else:
            ans[0][i]=ans[0][i-1]+1
    else:
        ans[0][i]=ans[0][i-1]

for i in range(1,h):
    if s[i][0]=="#":
        if s[i-1][0]=="#":
            ans[i][0]=ans[i-1][0]
        else:
            ans[i][0]=ans[i-1][0]+1
    else:
        ans[i][0]=ans[i-1][0]

for i in range(1,h):
    for j in range(1,w):
        if s[i][j]=="#":
            if s[i-1][j]=="#" :
                ans[i][j]=ans[i-1][j]
            elif s[i][j-1]=="#":
                ans[i][j]=ans[i][j-1]
            else:
                ans[i][j]=min(ans[i-1][j]+1,ans[i][j-1]+1)
        else:
            ans[i][j]=min(ans[i-1][j],ans[i][j-1])

print(ans[-1][-1])

Submission status: Wrong Answer

Solution code:
def main():
  _,*s=open(0)
  b,q='.'*101,range(101)
  for i,s in enumerate(s):
    a=[]
    for x,y,z,c in zip(b,'.'+s,s,q):
      c+=x=='.'>z
      i+=y=='.'>z
      if i>c:i=c
      a+=i,
    b,q=s,a
  print(i)
main()

Submission status: Wrong Answer

Solution code:
import sys
import copy
import math
import fractions

import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
import numpy as np
from decimal import *

maiz = []
maxh = 0
maxw = 0


def CheckCost(h, w):
    if h + 1 < maxh:
        if costList[h + 1][w] == -1 or costList[h + 1][w] > costList[h][w] + CheckColum(h + 1, w ,maiz[h][w]):
            costList[h + 1][w] = costList[h][w] + CheckColum(h + 1, w ,maiz[h][w])
            CheckCost(h + 1, w)

    if w + 1 < maxw:
        if costList[h][w + 1] == -1 or costList[h][w + 1] > costList[h][w] + CheckColum(h, w + 1,maiz[h][w]):
            costList[h][w + 1] = costList[h][w] + CheckColum(h, w + 1,maiz[h][w])
            CheckCost(h, w + 1)


def CheckColum(h, w ,s):
    if maiz[h][w] == "#" and s == ".":
        return 1
    else:
        return 0


if __name__ == '__main__':
    h, w = map(int, input().split())
    maxh = h
    maxw = w
    costList = []
    for i in range(h):
        costList.append([-1] * w)

    for i in range(h):
        s = input()
        maiz.append(list(map(lambda x: x, s)))
    costList[0][0] = CheckColum(0, 0,maiz[0][0])

    CheckCost(0, 0)

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

Submission status: Wrong Answer

Solution code:
_,*s=open(0)
b,q='.'*100,range(100)
for i,s in enumerate(s):
  a=[i]
  for x,y,z,c in zip(b,'.'+s,s,q):a+=min(c+(x=='.'>z),a[-1]+(y=='.'>z)),
  b,q=s,a[1:]
print(a[-2])

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
inf = 100000000000
u = [[0 for j in range(w)] for i in range(h)]

def v(i,j):
    if i<0 or j<0:
        return inf
    else:
        return u[i][j]

for i in range(h):
    s = input()
    for j in range(w):
        if i==0 and j==0:
            x = 0
        else:
            x = min(v(i-1,j), v(i,j-1))
        if s[j]=='#':
            x+=1
        u[i][j] = x

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

Submission status: Wrong Answer

Solution code:
_,*s=open(0)
b,q='.'*100,range(100)
for i,s in enumerate(s):
  a=[i]
  for x,y,z,c in zip(b,'.'+s,s,q):a+=min(c+(x!=z),a[-1]+(y!=z)),
  b,q=s,a[1:]
print(a[-2]+1>>1)

Submission status: Wrong Answer

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

dp = [[10**10] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        s1 = s[i][j]
        if i == 0 and j == 0:
            if s1 == '.':
                dp[i][j] = 0
            else:
                dp[i][j] = 1
        elif i == 0:
            if s1 == '.' and s[i][j-1] == '#':
                dp[i][j] = dp[i][j-1] + 1
            else:
                dp[i][j] = dp[i][j-1]
        elif j == 0:
            if s1 == '.' and s[i-1][j] == '#':
                dp[i][j] = dp[i-1][j] + 1
            else:
                dp[i][j] = dp[i-1][j]
        else:
            if s1 == '.' and s[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])
            if s1 == '.' and s[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])

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

Submission status: Wrong Answer

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

dp = [[10**10] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        s1 = s[i][j]
        if i == 0 and j == 0:
            if s1 == '.':
                dp[i][j] = 0
            else:
                dp[i][j] = 1
        elif i == 0:
            if s1 == '.' and s[i][j-1] == '#':
                dp[i][j] = dp[i][j-1] + 1
            else:
                dp[i][j] = dp[i][j-1]
        elif j == 0:
            if s1 == '.' and s[i-1][j] == '#':
                dp[i][j] = dp[i-1][j] + 1
            else:
                dp[i][j] = dp[i-1][j]
        else:
            if s1 == '.' and s[i][j-1] == '#':
                dp[i][j] = dp[i][j-1] + 1
            else:
                dp[i][j] = dp[i][j-1]
            if s1 == '.' and s[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])

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

Submission status: Wrong Answer

Solution code:
def dfs(tbl, i, j, h, w, cost, area):

    if(tbl[i][j]!=area):
        cost = cost + 1
        area = tbl[i][j]
    if(((i+1)==h)and((j+1)==w)):
        return cost
    else:
        tmp = 1000
        if( (i+1)!=h ):
            tmp = dfs(tbl, i+1, j, h, w, cost, area)
        if( (j+1)!=w ):
            tmp = min(tmp, dfs(tbl, i, j+1, h, w, cost, area))
        cost = tmp
        return cost

def main():
    n = list(map(int,input().strip().split()))
    h = n[0]
    w = n[1]
    table = []

    for i in range(h):
        h_in = list(input())
        table.append(h_in)
    if((table[0][0]=='#')and(table[h-1][w-1]=='#')):
        ans = dfs(table, 0, 0, h, w, -2, '.')
    elif((table[0][0]=='#')or(table[h-1][w-1]=='#')):
        ans = dfs(table, 0, 0, h, w, -1, '.')
    else:
        ans = dfs(table, 0, 0, h, w, 0, '.')
    print(int(ans/2))

main()

Submission status: Wrong Answer

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

dp = [[10**10] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if i == 0 and j == 0:
            dp[i][j] = 0
        elif 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])
        if s[i][j] == '#':
            dp[i][j] += 1

print(dp[-1][-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 = []
    for _ in range(H):
        g.append(S())
    dp = [[inf for _ in range(W)] for __ in range(H)]

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

    for r in range(H):
        for c in range(W):
            if r == 0 and c == 0:
                if g[r][c] == '#':
                    dp[r][c] = 1
                else:
                    dp[r][c] = 0
 
            cand1, cand2 = inf, inf
            if 0 <= r-1 < W:
                if g[r-1][c] == '.' and g[r][c] == '#':
                    dp[r][c] = min(dp[r][c], dp[r-1][c] + 1)
                else:
                    dp[r][c] = min(dp[r][c], dp[r-1][c])
            if 0<= c-1 < H:
                if g[r][c-1] == '.' and g[r][c] == '#':
                    dp[r][c] = min(dp[r][c], dp[r][c-1] + 1)
                else:
                    dp[r][c] = min(dp[r][c], dp[r][c-1])
    print(dp[-1][-1])
main()

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 = []



    for _ in range(H):
        g.append(S())
    dp = [[inf for _ in range(W)] for __ in range(H)]
    for r in range(H):
        for c in range(W):
            if r == 0 and c == 0:
                if g[r][c] == '#':
                    dp[r][c] = 1
                else:
                    dp[r][c] = 0

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

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 = []



    for _ in range(H):
        g.append(S())
    dp = [[inf for _ in range(W)] for __ in range(H)]
    for r in range(H):
        for c in range(W):
            if r == 0 and c == 0:
                if g[r][c] == '#':
                    dp[r][c] = 1
                else:
                    dp[r][c] = 0

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

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 = [[inf 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:
def dp(maze, visited, gy, gx):
    cnt = 0
    for w in range(W):
        cnt += int(maze[0][w] == "#")
        visited[0][w] = cnt
    cnt = 0        
    for h in range(H):
        cnt += int(maze[h][0] == "#")
        visited[h][0] = cnt
    for h in range(1,H):
        for w in range(1,W):
            _min = min(visited[h-1][w], visited[h][w-1])
            visited[h][w] = _min + int(maze[h][w] == "#")
        
    print(visited)            

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)