Jailbreak

시간 제한2초메모리 제한1024 MB

요약
천장에 구멍이 있고 각 층에 사다리가 놓인 감옥 격자가 주어질 때, 죄수가 위층으로 올라가 탈출할 수 있는지 판정한다.
난이도

어려움10점 중 8점

유형
그래프, BFS, 시뮬레이션, 구현
정답자
아직 제출이 없습니다

문제

You are one of the greatest computer scientists of the 21st century, and just discovered a polynomial-time algorithm for integer factorization. Unfortunately, the secret service noticed your work. They confiscated the algorithm and put you in an underground jail, the Federal Prison for Criminals. You are determined to escape, publish your findings,1 and seek justice.

The only exits in the jail through which you can escape are in the top storey of the jail, while you currently are in the leftmost cell of the bottom storey in the jail, hh storeys below the ground. To climb up through holes in the ceiling, you need a ladder. However, since you have practiced breaking a fall, and there are no two holes directly below each other, you can jump down through a hole without a ladder. Given the layout of the jail, determine whether it is possible to escape the jail.

The ladders can be found in the jail. You cannot carry another ladder up or down through a hole, or retrieve a ladder from a different storey, but you can carry them to different cells on the same (wall-enclosed) part of the same storey. You can jump over (arbitrarily many) holes in the floor/ceiling.

As an example, consider the first sample input. You can use the ladder next to you to go up one storey. Then you cannot go up again: although there is a hole, you have no ladder. But you can go down and then up twice, and finally escape.


1No worries: while the algorithm runs in polynomial time, it is not fast in practice.

입력

The input consists of:

  • One line with two integers ww and hh (3≤w≤1053 \leq w \leq 10^5, 1≤h≤1051 \leq h \leq 10^5, w⋅h≤3⋅105w\cdot h \leq 3 \cdot 10^5), the width and height of the jail.
  • 2h+12h+1 lines with ww characters:
    • On odd-numbered lines, the characters are either '-' or '.', representing the ceiling or a hole in the ceiling, respectively. The last line will only contain '-', to represent the floor.
  • On even-numbered lines, the characters are either '|', '.', or 'L', representing a wall, an empty space, or a ladder, respectively. The first and last character will always be a wall.

It is guaranteed that there is not a wall in the cell in a storey (even-numbered line) directly above and below a hole in a ceiling (odd-numbered line). It is guaranteed that there are no two holes in the ceilings directly below each other. It is guaranteed that the starting cell (bottom left cell) is not a wall.

출력

If it is possible to escape the jail, output "possible". Otherwise, output "impossible".

예제2

  1. 예제 1

    입력
    10 3
    -----.----
    |.|L....||
    ----.--.--
    |.....|L.|
    ---.-.--.-
    |.L.|..L.|
    ----------
    
    예상 출력
    possible
    
  2. 예제 2

    입력
    7 3
    ---.---
    |..L|.|
    --.----
    |.L..||
    -.-.---
    |.|L..|
    -------
    
    예상 출력
    impossible