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: Runtime Error

Solution code:
from collections import deque
h,w = map(int,input().split())
s = []
for i in range(h):
    s0 = list(input())
    s.append(s0)
d = deque()
rl = [[h*w+1 for i in range(w)] for i in range(h)]
if s[0][0] == ".":
    d.append([0,0])
    rl[0][0] = 0
else:
    d.append([0,0])
    rl[0][0] = 1
while len(d):
    a,b = d.popleft()
    print(a,b,rl)
    if a == h-1:
        if b < w-1:
            if rl[a][b+1] == h*w+1:
                if s[a][b+1] == "#":
                    rl[a][b+1] = rl[a][b] + 1
                else:
                    rl[a][b+1] = rl[a][b]
                d.append([a,b+1])
            else:
                if s[a][b+1] == "#":
                    rl[a][b+1]= min(rl[a][b+1],rl[a][b] + 1)
                else:
                    rl[a][b+1] = min(rl[a][b+1],rl[a][b])
    else:
        if b == w-1:
            if rl[a+1][b] == h*w+1:
                if s[a+1][b] == "#":
                    rl[a+1][b] = rl[a][b] + 1
                else:
                    rl[a+1][b] = rl[a][b]
                d.append([a+1,b])
            else:
                if s[a+1][b] == "#":
                    rl[a+1][b]= min(rl[a+1][b],rl[a][b] + 1)
                else:
                    rl[a+1][b] = min(rl[a+1][b],rl[a][b])
        else:
            if rl[a][b+1] == h*w+1:
                if s[a][b+1] == "#":
                    rl[a][b+1] = rl[a][b] + 1
                else:
                    rl[a][b+1] = rl[a][b]
                d.append([a,b+1])
            else:
                if s[a][b+1] == "#":
                    rl[a][b+1]= min(rl[a][b+1],rl[a][b] + 1)
                else:
                    rl[a][b+1] = min(rl[a][b+1],rl[a][b])
            if rl[a+1][b] == h*w+1:
                if s[a+1][b] == "#":
                    rl[a+1][b] = rl[a][b] + 1
                else:
                    rl[a+1][b] = rl[a][b]
                d.append([a+1,b])
            else:
                if s[a+1][b] == "#":
                    rl[a+1][b]= min(rl[a+1][b],rl[a][b] + 1)
                else:
                    rl[a+1][b] = min(rl[a+1][b],rl[a][b])
print(rl[h-1][w-1])

Submission status: Runtime Error

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



    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[ny][nx] == "#":
                        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: Runtime Error

Solution code:
from math import *
n,m=map(int,input().split())
a=[]
dp=[]
for i in range(n):
	a.append(input())
	dp.append([inf]*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: Runtime Error

Solution code:
from math import *
# from sys import stdin,stdout

def binarySearch(arr,x,i):
	l=i
	r=len(arr)-1 
	while l <= r: 
		mid = (l + r)//2; 
		if arr[mid] == x: 
			return mid 
		elif arr[mid] < x: 
			l = mid + 1
		else: 
			r = mid - 1
	return -1

def js(arr,x):
	l=0
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]<=x):
			ans=m
			l=m+1
		else:
			r=m-1
	return ans

def jg(arr,x,i):
	l=i
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]>=x):
			ans=m
			r=m-1
		else:
			l=m+1
	return ans

# def ceil(a,b):
# 	if a%b == 0:
# 		return int(a/b)
# 	else:
# 		return (a//b + 1)

from math import *
n,m=map(int,input().split())
a=[]
dp=[]
for i in range(n):
	a.append(input())
	dp.append([inf]*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: Runtime Error

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair < int, int >;

/**
 * Contest : AtCoder Grand Contest 043
 * Contest URL : https://atcoder.jp/contests/agc043
 * Problem : 
 * Problem URL : 
 */

void solve() {
	int H, W;
	cin >> H >> W;
	vector < string > matrix (H);
	for (int i = 0; i < H; i++) {
		cin >> matrix[i];
	}
	vector < vector < int > > dp (H, vector < int > (W));
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (i == 0 && j == 0) {
				dp[i][j] = (matrix[i][j] == '#');
			} else if (i == 0) {
				dp[i][j] = dp[i][j - 1] + (matrix[i][j] == '#');
			} else if (j == 0) {
				dp[i][j] = dp[i - 1][j] + (matrix[i][j] == '#');
			} else {
				dp[i][j] = min (dp[i - 1][j], dp[i][j - 1]) + (matrix[i][j] == '#');
			}
		}
	}
	cout << dp[H - 1][W - 1] << endl;
}

int main() {
	int T = 1;
	//scanf ("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

Submission status: Runtime Error

Solution code:

h, w = map(int, input().split())
mp = [list(input()) for i in range(h)]
dp = [[0]*105 for _ in range(105)]
if mp[0][0]=='#':
    dp[0][0]+=1
for i in range(w-1):
    plus=0
    if mp[0][i+1]=='#':
        plus=1
    dp[0][i+1] = dp[0][i]+plus
for i in range(h-1):
    plus=0
    if mp[i+1][0]=='#':
        plus=1
    dp[i+1][0] = dp[i][0]+plus

for i in range(1,w):
    for j in range(1,h):
        plus=0
        if mp[i][j]=='#':
            plus=1
        dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + plus

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)

H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]

goal_cnt = [10**20]
def dfs(x, y, cnt):
    if cnt >= goal_cnt[0]:
        return
    if field[x][y] == "#":
        cnt += 1
    if x == W-1 and y == H-1:
        goal_cnt[0] = min(goal_cnt[0], cnt)
        return
    for nx, ny in ((1,0),(0,1)):
        nx += x
        ny += y
        if nx >= W or ny >= H:
            continue
        dfs(nx, ny, cnt)

dfs(0,0,0)
print(goal_cnt[0])

Submission status: Runtime Error

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

goal_set = set()

def dfs(x, y, cnt):
    if field[x][y] == "#":
        cnt += 1
    if x == W-1 and y == H-1:
        goal_set.add(cnt)
    for nx, ny in ((1,0),(0,1)):
        nx += x
        ny += y
        if nx >= W or ny >= H:
            continue
        dfs(nx, ny, cnt)

dfs(0,0,0)
print(min(goal_set))

Submission status: Runtime Error

Solution code:
#list(map(int,input().split()))
n,m=map(int,input().split())
it=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
    j=input()
    for k in range(m):
        if j[k]=="#":
            it[i][k]=0
        else:
            it[i][k]=1
dp=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(m):
        if it[i][j]==0:
            dp[i][j]=1
        if(i>0 or j>0):
            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-1][j],dp[i][j-1])
print(dp[n-1][m-1])