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())
table = []
for _ in range(h):
    table.append(list(input()))
if table[0][0] == "." or table[h - 1][w - 1] == ".":
    print("Impossible")
    exit()
stack = [[0, 0]]
while len(stack) > 0:
    cur = stack.pop()
    if cur[0] == h - 1 and cur[1] == w - 1:
        break
    for step in [[0, 1], [1, 0]]:
        x, y = cur[1] + step[1], cur[0] + step[0]
        if x < w and y < h and table[y][x] == "#":
            table[y][x] = "."
            stack.append([y, x])

for y in range(h):
    for x in range(w):
        if table[y][x] == "#":
            print("Impossible")
            exit()

print("Possible")


h, w = map(int, input().split())
table = []
for _ in range(h):
    table.append(list(input()))
if table[0][0] == "." or table[h - 1][w - 1] == ".":
    print("Impossible")
    exit()
stack = [[0, 0]]
while len(stack) > 0:
    cur = stack.pop()
    if cur[0] == h - 1 and cur[1] == w - 1:
        table[cur[0]][cur[1]] = "."
        break
    for step in [[0, 1], [1, 0]]:
        x, y = cur[1] + step[1], cur[0] + step[0]
        if x < w and y < h and table[y][x] == "#":
            table[y][x] = "."
            stack.append([y, x])
for y in range(h):
    for x in range(w):
        if table[y][x] == "#":
            print("Impossible")
            exit()

print("Possible")





h, w = map(int, input().split())
table = []
sharp = 0
for _ in range(h):
    get = input()
    table.append(get)
    sharp += get.count("#")

flag = False
pos = [0, 0]
path = 0
while True:
    if pos == [h - 1, w - 1]:
        path += 1
        flag = True
        break
    if h <= pos[0] or w <= pos[1]:
        break
    if (
        pos[0] < h - 1
        and pos[1] < w - 1
        and table[pos[0] + 1][pos[1]] == "#"
        and table[pos[0]][pos[1] + 1] == "#"
    ):
        break
    if pos[0] < h - 1 and pos[1] < w - 1 and table[pos[0] + 1][pos[1]] == "#":
        pos[0] += 1
        path += 1
        continue
    elif pos[0] < h - 1 and pos[1] < w - 1 and table[pos[0]][pos[1] + 1] == "#":
        pos[1] += 1
        path += 1
        continue
    if pos[0] == h - 1 and pos[1] < w - 1 and table[pos[0]][pos[1] + 1] == "#":
        pos[1] += 1
        path += 1
        continue
    elif pos[0] < h - 1 and pos[1] == w - 1 and table[pos[0] + 1][pos[1]] == "#":
        pos[0] += 1
        path += 1
        continue
    else:
        break

print("Possible" if flag and path == sharp else "Impossible")


Verdicts: Wrong Answer

Wrong Answer

Accepted