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:
##!/usr/bin/env python
## -*- coding: utf-8 -*-
#import sys
#import os
#f = open('input.txt', 'r')
#sys.stdin = f
#input = sys.stdin.readline
# A　range flip find route

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

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

maze = []
for i in range(h):
    sub = list(input())[:-1]
    maze.append(sub)

if maze[0][0] == "#":
    dp[0][0] = 1
for i in range(1, w):
    if maze[0][i - 1] == "." and maze[0][i] == "#":
        dp[0][i] = dp[0][i - 1] + 1

    else:
        dp[0][i] = dp[0][i - 1]
for j in range(1, h):
    if maze[j - 1][0] == "." and maze[j][0] == "#":
        dp[j][0] = dp[j - 1][0] + 1
    else:
        dp[j][0] = dp[j - 1][0]

for i in range(1, h):
    for j in range(1, w):
        d = dp[i - 1][j]
        e = dp[i][j - 1]
        if maze[i - 1][j] == "." and maze[i][j] == "#":
            d += 1
        if maze[i][j - 1] == "." and maze[i][j] == "#":
            e += 1
        dp[i][j] = min(d, e)
print(dp[h - 1][w - 1])

Submission status: Runtime Error

Solution code:
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0,0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i range(2):
      p1 = p[0]+i, p[1]+(1-i)
      if not (p1[0]<H and p1[1]<W) or p1 in fixed:
        continue
      x, y = p
      x1, y1 = p1
      heappush(candicates, (dist+(grid[p1] & ~grid[p]), p1))
  return None

H, W = map(int, input().split())
grid = {(i,j): int(c=="#") for i in range(H) for j, c in enumerate(input())}

print(solve(H, W, grid))

Submission status: Runtime Error

Solution code:
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0][0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i,j in product([1,-1], [0,1]):
      p1 = p[0]+i*j, p[1]+i*(1-j)
      if not (0<=p1[0]<H and 0<=p1[1]<W) or p1 in fixed:
        continue
      x, y = p
      x1, y1 = p1
      heappush(candicates, (dist+(grid[y1][x1] & ~grid[y][x]), p1))
  return None

H, W = map(int, input().split())
grid = [list(map([".", "#"].index, input())) for i in range(H)]

print(solve(H, W, grid))

Submission status: Runtime Error

Solution code:
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0][0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i,j in product([1,-1], [0,1]):
      y1, x1 = p[0]+i*j, p[1]+i*(1-j)
      if not (0<=y<H and 0<=x<W) or (y, x) in fixed:
        continue
      x0, y0 = p
      heappush(candicates, (dist+(grid[y][x] & ~grid[y0][x0]), (y, x)))
  return None

H, W = map(int, input().split())
grid = [list(map([".", "#"].index, input())) for i in range(H)]
print(solve(H, W, grid))

Submission status: Runtime Error

Solution code:
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i,j in product([1,-1], [0,1]):
      y, x = p[0]+i*j, p[1]+i*(1-j)
      if not (0<=y<H and 0<=x<W) or (y, x) in fixed:
        continue
      heappush(candicates, (dist+(1-grid[p])*grid[W*y+x], (y, x)))
  return None

H, W = map(int, input().split())
grid = list(chain.from_iterable(map([".", "#"].index, input()) for i in range(H)))
print(solve(H, W, grid))

Submission status: Runtime Error

Solution code:
from itertools import *
from heapq import *

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i,j in product([1,-1], [0,1]):
      y, x = p[0]+i*j, p[1]+i*(1-j)
      if not (0<=y<H and 0<=x<W) or (y, x) in fixed:
        continue
      heappush(candicates, dist+(1-grid[p])*grid[W*y+x], (y, x)))
  return None

H, W = map(int, input().split())
grid = list(chain.from_iterable(map([".", "#"].index, input()) for i in range(H)))
print(solve(H, W, grid))

Submission status: Runtime Error

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

def solve(H, W, grid):
  goal = (H-1, W-1)
  fixed = set()
  candicates = [(grid[0], (0, 0))]
  while 1:
    dist, p = heappop(candicates)
    if p == goal:
      return dist
    if p in fixed:
      continue
    fixed.add(p)
    for i,j in product([1,-1], [0,1]):
      y, x = p[0]+i*j, p[1]+i*(1-j)
      if not (0<=y<H and 0<=x<W) or (y,x) in fixed:
        continue
      heappush(candicates, (dist+grid[W*y+x], (y, x)))
  return None

H, W = map(int, input().split())
grid = list(chain.from_iterable(map([".", "#"].index, input()) for i in range(H)))
print(solve(H, W, grid))

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = []  # h, w
for i in range(h):
    s += [','.join(input()).split(',')]

cost = [[1 if s[0][0] == '#' else 0 for i in range(w)] for j in range(h)]

for i in range(1, h + w-1):
    for j in range(w if h < w else h):
        if i - j < w and j >= 0 and i - j >= 0:
            if j == 0:
                cost[j][i - j] = cost[j][i - j - 1]
            elif i - j == 0:
                cost[j][i - j] = cost[j - 1][i - j]
            else:
                cost[j][i - j] = cost[j - 1][i - j] if cost[j - 1][i -
                                                                   j] < cost[j][i - j - 1] else cost[j][i - j - 1]

            if s[j][i - j] == '#':
                cost[j][i - j] += 1

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

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = [] # h, w
for i in range(h):
    s += [','.join(input()).split(',')]

cost = [[1 if s[0][0] == '#' else 0 for i in range(h + 1)] for j in range(w + 1)]

for i in range(1, h + w - 1):
    for j in range(w if h < w else h):
        if j < h and i - j < w and j >= 0 and i - j >= 0:
            print(j, i - j)
            if j == 0:
                cost[j][i - j] = cost[j][i - j - 1]
            elif i - j == 0:
                cost[j][i - j] = cost[j - 1][i - j]
            else:
                cost[j][i - j] = cost[j - 1][i - j] if cost[j - 1][i - j] < cost[j][i - j - 1] else cost[j][i - j - 1]

            if s[j][i - j] == '#':
                cost[j][i - j] += 1

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

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
s = [] # h, w
for i in range(h):
    s += [','.join(input()).split(',')]

cost = [[1 if s[0][0] == '#' else 0 for i in range(h)] for j in range(w)]

for i in range(1, h + w - 1):
    for j in range(w if h < w else h):
        if j < h and i - j < w:
            if j >= 0 and i - j >= 0:
                if j == 0:
                    cost[j][i - j] = cost[j][i - j - 1]
                elif i - j == 0:
                    cost[j][i - j] = cost[j - 1][i - j]
                else:
                    cost[j][i - j] = cost[j - 1][i - j] if cost[j - 1][i - j] < cost[j][i - j - 1] else cost[j][i - j - 1]

                if s[j][i - j] == '#':
                    cost[j][i - j] += 1
                    
print(cost[h - 1][w - 1])

Submission status: Runtime Error

Solution code:
class maze:
    "mz=maze();mz.find(0,0,0,0)"
    moves=[[0,1],[1,0]]
    counter=[]
    
    def __init__(self):
        p=self
        p.H,p.W=map(int,input().split())
        p.board=['' for h in range(p.H)]
        for h in range(p.H):
            s=input()
            p.board[h]=[s[w] for w in range(p.W)]
        # print(p.board)
        
    def __del__(self):
      print(min(self.counter))
        
    def on_white(self,x,y):
        p=self
        return 0<=x<p.W and 0<=y<p.H and p.board[x][y]=='.'
    def set_white(self,x,y,n,c):
        p=self
        p.board[x][y]=str(n)
        return c
    def reset_white(self,x,y,c):
        p=self
        p.board[x][y]='.'
        return c            

    def on_black(self,x,y):
        p=self
        return 0<=x<p.W and 0<=y<p.H and p.board[x][y]=='#'
    def set_black(self,x,y,n,c):
        p=self
        p.board[x][y]=str(n)
        return c+1
    def reset_black(self,x,y,c):
        p=self
        p.board[x][y]='#'
        return c-1
    
    def is_goal(self,x,y):
        return x==self.W-1 and y==self.H-1
    
    def find(self,x,y,n,c):
        p=self
        if (p.on_white(x,y)):
            c=p.set_white(x,y,n,c)
            if p.is_goal(x,y):
                p.result(c)
            else:
                for k in range(len(p.moves)):
                    dx,dy = p.moves[k]
                    p.find(x+dx,y+dy,n+1,c)
            c=p.reset_white(x,y,c)
        elif (p.on_black(x,y)):
            c=p.set_black(x,y,n,c)
            if p.is_goal(x,y):
                p.result(c)
            else:
                for k in range(len(p.moves)):
                    dx,dy = p.moves[k]
                    p.find(x+dx,y+dy,n+1,c)
            c=p.reset_black(x,y,c)            

    def result(self,c):
        #for h in range(self.H):
        #    print(self.board[h]) 
        self.counter+=[c]
        #print(min(self.counter))

mz=maze();
mz.find(0,0,0,0)

Submission status: Runtime Error

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

class Dijkstra:
    def __init__(self, N):
        self.N = N # #vertices
        self.E = [[] for _ in range(N)]

    def add_edge(self, init, end, weight, undirected=False):
        self.E[init].append((end, weight))
        if undirected: self.E[end].append((init, weight))
    
    def distance(self, s):
        INF = float('inf')
        self.dist = [INF] * self.N # the distance of each vertex from s
        self.prev = [-1] * self.N # the previous vertex of each vertex on a shortest path from s
        visited = [False] * self.N
        n_visited = 0 # #(visited vertices)
        heap = []
        heappush(heap, (0, -1, s))
        while heap:
            d, p, v = heappop(heap)
            if visited[v]: continue # (s,v)-shortest path is already calculated
            self.dist[v] = d; self.prev[v] = p; self.visited[v] = True
            n_visited += 1
            if n_visited == self.N: break
            for u, c in self.E[v]:
                if visited[u]: continue
                temp = d + c
                if self.dist[u] > temp: heappush(heap, (temp, v, u))
        return self.dist
    
    def shortest_path(self, t):
        P = []
        prev = self.prev
        while True:
            P.append(t)
            t = prev[t]
            if t == -1: break
        return P[::-1]

H, W = map(int, input().split())
dijkstra = Dijkstra(H * W)

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

grid = [input() for _ in range(H)] # |string| = W
E = [[] for _ in range(H * W)]
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] == '#':
            dijkstra.add_edge(v, u, 1)
        else:
            dijkstra.add_edge(v, u, 0)
dist = dijkstra.distance(0)
ans += dist[vtx(H-1, W-1)]
print(ans)

Submission status: Runtime Error

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

dp = [[float("inf")] * (w + 10) for _ in range(h + 10)]
if s[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0

for c in range(w):
    for r in range(h):
        if c + 1 < w:
            if s[c + 1][r] == "#":
                dp[c + 1][r] = min(dp[c + 1][r], dp[c][r]) + 1
            else:
                dp[c + 1][r] = min(dp[c + 1][r], dp[c][r])
        if r + 1 < h:
            if s[c][r + 1] == "#":
                dp[c][r + 1] = min(dp[c][r + 1], dp[c][r]) + 1
            else:
                dp[c][r + 1] = min(dp[c][r + 1], dp[c][r])

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

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<queue>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#include<string>// 加法 S.size() s.substr(l,r=s.size()-l) -> l文字目からr文字分
#include<cmath>//三角関数,rad
#include<algorithm>//__gcd()　lower_bound(a+l,a+r,x)-a でindex
// vector -> min/max({vector}) 
// 配列   -> *min_element(a+l,a+r)  *忘れず lとrで[l,r) 
//reverse(a+l,a+r)   配列aの[l,r)を逆順に strはreverse(S.begin(),S.end())
//sort(a+l,a+r,greater <int>()) 配列aの[l,r)昇順sort,大きい順はgreater<type>()
//vectorならv.begin(),v.end()
#include<ctime>//"clock()/CLOCKS_PER_SEC で秒数
#include<vector>//v.push_back(x),v.pop_back()
#include<queue>//q.push(x),front()で先頭を返す,pop()で削除, size(),empty()
#include<functional>//vector,queueと併用でpriority_queue
//宣言は priority_queue< Type, vector<Type>, greater<Type>> Q1; 
// pq.push(x),top()で参照 pop()で削除   greaterで最小 lessで最大がtopに
using namespace std;
typedef pair<int,int> P;
const int inf = 10e7;
#define Maxw 1000
#define Maxh 1000
int h,w;


bool flag=0;
int dx[2]={0,1};
int dy[2]={1,0};

int main(){
  int step[Maxh][Maxw];char maze[Maxh][Maxw];
  cin>>h>>w;
  string s;
for (int i=0;i<h+2;i++){
    for (int j=0;j<w+2;j++){
    step[i][j]=inf;}
  }step[1][1]=0;
  for (int i=0;i<w+2;i++){maze[0][i]='!';maze[h+1][i]='!';}
  for (int i=0;i<h+2;i++){maze[i][0]='!';maze[i][w+1]='!';}
  for (int H=1;H<h+1;H++){cin>>s;rep(j,w){maze[H][j+1]=s[j];}maze[H][0]='!';maze[H][w+1]='!';}
rep(i,h){
  rep(j,w){
    rep(k,2){if (maze[i+1+dx[k]][j+1+dy[k]]!='!'){
      step[i+1+dx[k]][j+1+dy[k]]=min(step[i+1+dx[k]][j+1+dy[k]],step[i+1][j+1]+(maze[i+1+dx[k]][j+1+dy[k]]=='#'&&maze[i+1][j+1]=='.'? 1:0));}}}}
int ans=0;if (maze[1][1]=='#')ans++;

  cout<<step[h][w]+ans<<endl;
}

Submission status: Runtime Error

Solution code:
5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

Submission status: Runtime Error

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,h):
    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,w):
    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,w):
    for j in range(1,h):
        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: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=[[10101 for i in range(w+1)] for j in range(h+1)]
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,h):
    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,w):
    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,w):
    for j in range(1,h):
        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[w-1][h-1])

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=[[10**5 for i in range(w+1)] for j in range(h+1)]
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,h):
    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,w):
    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,w):
    for j in range(1,h):
        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[w-1][h-1])

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=[[10**5 for i in range(w)] for j 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,h):
    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,w):
    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,w):
    for j in range(1,h):
        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[w-1][h-1])

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=[[10**5 for i in range(w)] for j 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,h):
    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,w):
    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,w):
    for j in range(1,h):
        if s[i][j]=="#":
            if s[i-1][j]=="#" or s[i][j-1]=="#":
                ans[i][j]=min(ans[i-1][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[w-1][h-1])

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=[[10**5 for i in range(w)] for j 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,h):
    if s[0][i]=="#":
        ans[0][i]=ans[0][i-1]+1
    else:
        ans[0][i]=ans[0][i-1]

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

for i in range(1,w):
    for j in range(1,h):
        if s[i][j]=="#":
            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[w-1][h-1])

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=[[10**5 for i in range(w)] for j 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,h):
    if s[0][i]=="#":
        ans[0][i]=ans[0][i-1]+1
    else:
        ans[0][i]=ans[0][i-1]

for i in range(1,w):
    if s[i][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]=="#":
            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[w-1][h-1])

Submission status: Runtime Error

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

dp = [[0 for _ in range(W)] for _ in range(H)]
if masu[0][0] == '.':
	dp[0][0] = 0
else:
	dp[0][0] = 1
    
for i in range(H-1):
	if masu[i+1][0] == '.':
		dp[i+1][0] = dp[i][0]
    else:
      	dp[i+1][0] = dp[i][0] + 1
        
for j in range(W-1):
  	if masu[0][j+1] == '.':
      	dp[0][j+1] = dp[0][j]
    else:
      	dp[0][j+1] = dp[0][j] + 1
        
for i in range(H-1):
  	for j in range(W-1):
      	if masu[i+1][j+1]=='.':
      		dp[i+1][j+1] = min(dp[i][j+1], dp[i+1][j])
        else:
          	dp[i+1][j+1] = min(dp[i][j+1], dp[i+1][j]) + 1
            
print(dp[H-1][W-1])

Submission status: Runtime Error

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

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


n = I()
ans = 1
for _ in range(3):
    G = [[] for _ in range(n)]
    m = I()
    for _ in range(m):
        p, q = LI()
        G[p - 1] += [q - 1]
        G[q - 1] += [p - 1]

    visited = [0] * n
    cnt = 0
    for i in range(n - 1, -1, -1):
        if visited[i] == 0:
            visited[i] = 1
            cnt += i + 1
            dq = deque([i])
            while dq:
                u = dq.pop()
                for v in G[u]:
                    visited[v] = 1

Submission status: Runtime Error

Solution code:
from itertools import count, repeat

h = int(input().split()[0])
a0 = repeat('.')
r0 = list(range(w))
for i in range(h):
  a = input()
  r = [i]
  for x, y, z, u in zip(a0, '.' + a, a, r0):
    r.append(min(u + (x + z == '.#'), r[-1] + (y + z == '.#')))
  a0 = a
  r0 = r[1:]
print(r[-1])

Submission status: Runtime Error

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

def com(r, c):
	if r == 1 and c == 1:
		if masu[0][0] == '.':
			a = 0
		else:
			a = 1
	elif r == 1 and c >= 2:
		a = masu[0][:c].count('#')
	elif r >= 2 and c == 1:
		if masu[r-1][0] == '.':    
			a = com(r-2, 0) + 0
		else:
			a = com(r-2, 0) + 1
	else:
		if masu[r-1][c-1] == '.':
			a = min(com(r-2, c-1), com(r-1, c-2))
		else:
			a = min(com(r-2, c-1), com(r-1, c-2)) + 1
      
	return a
  
print(com(H, W))

Submission status: Runtime Error

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

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

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

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

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

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

Submission status: Runtime Error

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

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

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

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

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

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

Submission status: Runtime Error

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

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

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

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

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

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

Submission status: Runtime Error

Solution code:
import queue

class VInfo(object):
    def __init__(self,x,y,d,color):
        self.color=color
        self.d=d
        self.x=x
        self.y=y
    def turnColor(self):
        if self.color==".":return "#"
        else:return "."

h,w=map(int,input().split())
a=[[str(i) for i in input()] for j in range(h)]
sx,sy=0,0
gx,gy=w-1,h-1
cw=0
mapA=[]
m=100000000000000
for my in range(h):
    l=[]
    for mx in range(w):
        l.append(m)
    mapA.append(l)

q=queue.Queue()
if a[0][0]==".":q.put(VInfo(0,0,0,"."))
else:q.put(VInfo(0,0,1,"#"))
l=[]
while not q.empty():
    now=q.get()
    if now.x==gx and now.y==gy:
        if now.color=="#":l.append((mapA[gy][gx]+2)//2)
        else:l.append((mapA[gy][gx]+1)//2)

    if now.y+1<w and a[now.y+1][now.x]==now.color and mapA[now.y+1][now.x]>now.d:
            mapA[now.y+1][now.x]=now.d
            q.put(VInfo(now.x,now.y+1,now.d,now.color))
    if now.y+1<w and a[now.y+1][now.x]!=now.color and mapA[now.y+1][now.x]>now.d:
            mapA[now.y+1][now.x]=now.d
            q.put(VInfo(now.x,now.y+1,now.d+1,now.turnColor()))
    if now.x+1<h and a[now.y][now.x+1]==now.color and mapA[now.y][now.x+1]>now.d:
            mapA[now.y][now.x+1]=now.d
            q.put(VInfo(now.x+1,now.y,now.d,now.color))
    if now.x+1<h and a[now.y][now.x+1]!=now.color and mapA[now.y][now.x+1]>now.d:
            mapA[now.y][now.x+1]=now.d
            q.put(VInfo(now.x+1,now.y,now.d+1,now.turnColor()))
print(min(l))

Submission status: Runtime Error

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

if s[0][0] == '.':
  dp[0][0] = 0
else:
  dp[0][0] = 1

for i in range(1,W):
  if s[0][i] == '.':
    dp[0][i] = dp[0][i-1]
  else:
    if s[0][i-1] == '.':
      dp[0][i] = dp[0][i-1]+1
    else:
      dp[0][i] = dp[0][i-1]

for i in range(1,H):
  for j in range(W):
    if j == 0:
      if s[i][j] == '.':
        dp[i][j] = dp[i-1][j]
      else:
        dp[i][j] = dp[i-1][j]+1
    else:
      if s[i][j] == '.':
        dp[i][j] = min(dp[i-1][j],dp[i][j-1])
      else:
        left = 0
        up = 0
        if s[i-1][j] == '.':
          left = 1
        if s[i][j-1] == '.':
          up = 1
        dp[i][j] = min(dp[i-1][j]+left,dp[i][j-1]+up)
print(dp[-1][-1])