Problem: We have a grid of H rows and W columns. Initially, there is a stone in the top left cell. Shik is trying to move the stone to the bottom right cell. In each step, he can move the stone one cell to its left, up, right, or down (if such cell exists). It is possible that the stone visits a cell multiple times (including the bottom right and the top left cell).  You are given a matrix of characters a_{ij} (1 \leq i \leq H, 1 \leq j \leq W). After Shik completes all moving actions, a_{ij} is `#` if the stone had ever located at the i-th row and the j-th column during the process of moving. Otherwise, a_{ij} is `.`. Please determine whether it is possible that Shik only uses right and down moves in all steps.

Submission attempts (in order):
import sys
input = sys.stdin.readline
def dfs(x,y,cnt):
    cnt += 1
    print(cnt)
    if x == W-1 and y == H-1:
        # return print("Possible")
        return cnt
    if x+1 != W and A[y][x+1] == '#':
        cnt =dfs(x+1, y, cnt)
    if y+1 != H and A[y+1][x] == '#':
        cnt = dfs(x, y+1, cnt)
    return cnt

if __name__ == '__main__':
    H, W = map(int, input().split())
    A = [list(input().rstrip()) for i in range(H) ]
    print(A)
    cnt = 0
    cnt = dfs(0, 0, cnt)
    if cnt == H+W:
        print('Possible')
    else:
        print('Impossible')

import sys
input = sys.stdin.readline
def dfs(x,y):
    if x == W-1 and y == H-1:
        return print('Possible')
    if x+1 != W-1 and A[y][x+1] == '#':
        if y+1 != H-1 and A[y+1][x] == '#':
            print('Impossible')
            sys.exit()
        dfs(x+1, y)
    if y+1 != H-1 and A[y+1][x] == '#':
        dfs(x, y+1)
    else:
        print('Impossible')
        sys.exit()
    return

if __name__ == '__main__':
    H, W = map(int, input().split())
    A = [list(input().rstrip()) for i in range(H) ]
    dfs(0, 0)

import sys
input = sys.stdin.readline
def dfs(x,y,cnt):
    cnt += 1
    if x == W-1 and y == H-1:
        # return print("Possible")
        return cnt
    if x+1 != W and A[y][x+1] == '#':
        cnt =dfs(x+1, y, cnt)
    if y+1 != H and A[y+1][x] == '#':
        cnt = dfs(x, y+1, cnt)
    return cnt

if __name__ == '__main__':
    H, W = map(int, input().split())
    A = [list(input().rstrip()) for i in range(H) ]
    cnt = 0
    cnt = dfs(0, 0, cnt)
    if cnt == H+W:
        print('Possible')
    else:
        print('Impossible')

import sys
input = sys.stdin.readline
def dfs(x,y,cnt):
    cnt += 1
    if x == W-1 and y == H-1:
        # return print("Possible")
        return cnt
    if x+1 != W and A[y][x+1] == '#':
        cnt =dfs(x+1, y, cnt)
    if y+1 != H and A[y+1][x] == '#':
        cnt = dfs(x, y+1, cnt)
    return cnt

if __name__ == '__main__':
    H, W = map(int, input().split())
    A = [list(input().rstrip()) for i in range(H) ]
    print(A)
    cnt = 0
    cnt = dfs(0, 0, cnt)
    if cnt == H+W:
        print('Possible')
    else:
        print('Impossible')

import sys
input = sys.stdin.readline
def dfs(x,y):
    if x == W-1 and y == H-1:
        return print('Possible')
    if x+1 != W-1 and A[y][x+1] == '#':
        if y+1 != H-1 and A[y+1][x] == '#':
            print('Impossible')
            sys.exit()
        dfs(x+1, y)
    elif y+1 != H-1 and A[y+1][x] == '#':
        dfs(x, y+1)
    else:
        print('Impossible')
        sys.exit()
    return

if __name__ == '__main__':
    H, W = map(int, input().split())
    A = [list(input().rstrip()) for i in range(H) ]
    dfs(0, 0)

import sys
input = sys.stdin.readline
def dfs(x,y):
    if x == W-1 and y == H-1:
        return print('Possible')
    if x+1 != W and A[y][x+1] == '#':
        if y+1 != H and A[y+1][x] == '#':
            print('Impossible')
            sys.exit()
        dfs(x+1, y)
    if y+1 != H and A[y+1][x] == '#':
        dfs(x, y+1)
    else:
        print('Impossible')
        sys.exit()
    return

if __name__ == '__main__':
    H, W = map(int, input().split())
    A = [list(input().rstrip()) for i in range(H) ]
    dfs(0, 0)

import sys
input = sys.stdin.readline
def dfs(x,y):
    if x == W-1 and y == H-1:
        return print('Possible')
    if x+1 != W and A[y][x+1] == '#':
        if y+1 != H and A[y+1][x] == '#':
            return print('Impossible')
        dfs(x+1, y)
    if y+1 != H and A[y+1][x] == '#':
        dfs(x, y+1)
    else:
        print('Impossible')
        sys.exit()
    return

if __name__ == '__main__':
    H, W = map(int, input().split())
    A = [list(input().rstrip()) for i in range(H) ]
    dfs(0, 0)

import sys
input = sys.stdin.readline
def main():
    H, W = map(int, input().split())
    A = [list(input().rstrip()) for i in range(H)]
    CNT = 0
    for i in range(H):
        for j in range(W):
            if A[i][j] == '#':
                CNT += 1
    if CNT == H+W-1:
        print('Possible')
    else:
        print('Impossible')

if __name__ == '__main__':
    main()

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted