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):
H,W = map(int,input().split())

a = [tuple(input()) for i in range(H)]

i = 0
j = 0
res = True

while (i!=H-1 or j !=W-1) and res:
    while a[i][j] == '
        j += 1
    if j < W-1:
        for k in range(j,W):
            if a[i][k] == '
                res = False
                break
    if i < H-1:
        i+=1
    
    for k in range(j-1):
        if a[i][k] == '
            res = False
            break
if res:
    print('Possible')
else:
    print('Impossible')

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

a = [tuple(input()) for i in range(H)]

i = 0
j = 0
res = True

if a[i][j] == '.': 
    res = False
    exit()

while (i!=H-1 or j !=W-1) and res:
    if 1 <= i and a[i][j] == '.': 
        res = False
        break
    while a[i][j] == '
        j += 1
    if j < W-1:
        for k in range(j,W):
            if a[i][k] == '
                res = False
                break
    if i < H-1:
        i+=1
    
    for k in range(j-1):
        if a[i][k] == '
            res = False
            break
if res and a[i][j] == '#':
    print('Possible')
else:
    print('Impossible')

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

a = [tuple(input()) for i in range(H)]

i = 0
j = 0
res = True

while (i!=H-1 or j !=W-1) and res:
    while a[i][j] == '
        j += 1
    if j < W-1:
        for k in range(j,W):
            if a[i][k] == '
                res = False
                break
    if i < H-1:
        i+=1
    
    for k in range(j-1):
        if a[i][k] == '
            res = False
            break
if res and a[i][j] == '#':
    print('Possible')
else:
    print('Impossible')

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

a = [tuple(input()) for i in range(H)]

i = 0
j = 0
res = True

if a[i][j] == '.': 
    res = False
    exit()

while (i!=H-1 or j !=W-1) and res:
    if 1 <= i and a[i][j-1] == '.': 
        res = False
        break
    while a[i][j] == '
        j += 1
    if j < W-1:
        for k in range(j,W):
            if a[i][k] == '
                res = False
                break
    if i < H-1:
        i+=1
    
    for k in range(j-1):
        if a[i][k] == '
            res = False
            break
if res and a[i][j] == '#':
    print('Possible')
else:
    print('Impossible')

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

a = [tuple(input()) for i in range(H)]

i = 0
j = 0
res = True

while (i!=H-1 or j+1 !=W-1) and res:
    if a[i][j] == '
        if j+1 < W-1:
            while a[i][j+1] == '
                j += 1
                if j == W-1:
                    break
    else:
        res = False
        break
    for k in range(j+1,W):
        if a[i][k] == '
            res = False
            break
    
    if i < H-1:
        i+=1
        for k in range(j):
            if a[i][k] == '
                res = False
                break

if res and a[i][j+1] == '#':
    print('Possible')
else:
    print('Impossible')

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

a = [tuple(input()) for i in range(H)]

i = 0
j = 0
res = True

while (i!=H-1 or j !=W-1) and res:
    if a[i][j] == '.': 
        res = False
        break
    while a[i][j] == '
        j += 1
    if j < W-1:
        for k in range(j,W):
            if a[i][k] == '
                res = False
                break
    if i < H-1:
        i+=1
    
    for k in range(j-1):
        if a[i][k] == '
            res = False
            break
if res and a[i][j] == '#':
    print('Possible')
else:
    print('Impossible')

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

res = 0

for i in range(H):
    res += input().count('#')

if res == H+W-1:
    print('Possible')
else:
    print('Impossible')

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted