Problem: 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.

Submission attempts (in order):
import numpy as np

H,W = input().split()
H,W=int(H),int(W)
P=[0]*H
for i in range(H):
    P[i] = input()

dp=np.zeros((H,W),dtype='u8')
if P[0][0]=='#':
    dp[0][0]=1
else:
    dp[0][0]=0

def DP(x,y):
    global dp
    if 0<=x<=W-1 and 0<=y<=H-1:
        return dp[y][x]
    else:
        return 9999999

for l in range(1,H+W):
    for i in range(l+1):
        if i<H and l-i<W:
            if P[i][l-i]=='#':
                a=DP(i-1,l-i)
                b=DP(i,l-i-1)
                if a < 9999999:
                    if P[i-1][l-i]=='.':
                        a+=1
                if b <  9999999:
                    if P[i][l-i-1]=='.':
                        b+=1
                dp[i][l-i]=min(a,b)
            else:
                #print(i,l-i,DP(i-1,l-i),DP(i,l-i-1))
                dp[i][l-i]=min(DP(i-1,l-i),DP(i,l-i-1))
print(dp[H-1][W-1])

H,W = input().split()
H,W=int(H),int(W)
sw=[[0]*W]*H

for i in range(W):
    sw[i] = input()

count=0
if sw[0][0]=='#':
    sw[0]='.'+sw[0][1:]
    count+=1
if sw[H-1][W-1]=='#':
    sw[H-1]=sw[H-1][:W-1]+'.'
    count+=1

def search(h,v):
    global H,W
    global sw
    if h>=H-1 and v>=W-1:
        return -2,-2
    elif v>=H-1:
        if sw[h+1][W-1]=='.':
            return h+1,v
        else:
            return -1,-1
    elif h>=W-1:
        if sw[H-1][v+1]=='.':
            return h,v+1
        else:
            return -1,-1
    else:
        if sw[h+1][v]=='.':
            return h+1,v
        elif sw[h][v+1]=='.':
            return h,v+1
        else:
            return -1,-1

h=0
v=0
i=0
while(1):
    flag=True
    for j in range(i+1):
        result = search(h+i-j,v+j)
        if result == (-2,-2):
            print(count)
            exit()
        if result!=(-1,-1):
            flag = False
            h,v=result
            break
    if flag:
        count+=1
        i+=1
    else:
        i=1

import numpy as np

H,W = input().split()
H,W=int(H),int(W)
P=[0]*H
for i in range(H):
    P[i] = input()

dp=np.zeros((H,W),dtype='u8')
if P[0][0]=='#':
    dp[0][0]=1
else:
    dp[0][0]=0

def DP(x,y):
    global dp
    if 0<=x<=W-1 and 0<=y<=H-1:
        return dp[y][x]
    else:
        return 9999999

for l in range(1,H+W):
    for i in range(l):
        if i<H and l-i<W:
            if P[i][l-i]=='#':
                #print(i,l-i, DP(i-1,l-i),DP(i,l-i-1))
                dp[i][l-i]=min(DP(i-1,l-i),DP(i,l-i-1))+1
            else:
                #print(i,l-i,DP(i-1,l-i),DP(i,l-i-1))
                dp[i][l-i]=min(DP(i-1,l-i),DP(i,l-i-1))
print(dp[H-1][W-1])

H,W = input().split()
H,W=int(H),int(W)
sw=[[0]*W]*H

for i in range(W):
    sw[i] = input()

count=0
if sw[0][0]=='#':
    sw[0]='.'+sw[0][1:]
    count+=1
if sw[H-1][W-1]=='#':
    sw[H-1]=sw[H-1][:W-1]+'.'
    count+=1

def search(h,v):
    global H,W
    global sw
    if h>=H-1 and v>=W-1:
        return -2,-2
    elif v>=H-1:
        if sw[h+1][W-1]=='.':
            return h+1,v
        else:
            return -1,-1
    elif h>=W-1:
        if sw[H-1][v+1]=='.':
            return h,v+1
        else:
            return -1,-1
    else:
        if sw[h+1][v]=='.':
            return h+1,v
        elif sw[h][v+1]=='.':
            return h,v+1
        else:
            return -1,-1

h=0
v=0
i=0
while(1):
    flag=True
    for j in range(i+1):
        result = search(h+i-j,v+j)
        if result == (-2,-2):
            exit()
        if result!=(-1,-1):
            i=0
            flag = False
            h,v=result
            break
    if flag:
        count+=1
        i+=1

H,W = input().split()
H,W=int(H),int(W)
sw=[[0]*W]*H

for i in range(W):
    sw[i] = input()

count=0
if sw[0][0]=='#':
    sw[0]='.'+sw[0][1:]
    count+=1
if sw[H-1][W-1]=='#':
    sw[H-1]=sw[H-1][:W-1]+'.'
    count+=1

def search(h,v):
    global H,W
    global sw
    if h==H-1 and v==W-1:
        return -2,-2
    elif v>=H-1 or h>=W-1:
        return -1,-1
    else:
        if sw[h+1][v]=='.':
            return h+1,v
        elif sw[h][v+1]=='.':
            return h,v+1
        else:
            return -1,-1

P=[[0,0]]
next=[]
i=0
plus=True
while(1):
    flag=False
    for p in P:
        for j in range(i+1):
            if p[0]+i-j>=W or p[1]+j>=H:
                continue
            result=search(p[0]+i-j,p[1]+j)
            #print('search',p[0]+i-j,p[1]+j,count)
            if result==(-2,-2):
                print(count)
                exit()
            elif result!=(-1,-1):
                next.append(result)
                flag=True
                i=0
                
    if not flag:
        #print('yaa')
        i+=1
        if plus:
            count+=1
        plus=False
    else:
        plus=True
        #print('next',next,count)
        P=[]
        P=next
        next=[]


import numpy as np

H,W = input().split()
H,W=int(H),int(W)
P=[0]*H
for i in range(H):
    P[i] = input()

dp=np.zeros((H,W),dtype='u8')
if P[0][0]=='#':
    dp[0][0]=1
else:
    dp[0][0]=0

def DP(y,x):
    global dp
    if 0<=x<=W-1 and 0<=y<=H-1:
        return dp[y][x]
    else:
        return 9999999

for l in range(1,H+W):
    for i in range(l+1):
        if i<H and l-i<W:
            if P[i][l-i]=='#':
                a=DP(i-1,l-i)
                b=DP(i,l-i-1)
                if a < 9999999:
                    if P[i-1][l-i]=='.':
                        a+=1
                if b <  9999999:
                    if P[i][l-i-1]=='.':
                        b+=1
                dp[i][l-i]=min(a,b)
            else:
                #print(i,l-i,DP(i-1,l-i),DP(i,l-i-1))
                dp[i][l-i]=min(DP(i-1,l-i),DP(i,l-i-1))
print(dp[H-1][W-1])

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Accepted