Tomb Raider

시간 제한3초메모리 제한512 MB

요약
회전 가능한 두 면 gargoyle이 있는 n×m 거울 미로에서, 모든 gargoyle 면이 빛으로 다른 gargoyle 면과 연결되도록 회전 횟수의 최솟값을 구한다.
난이도

어려움10점 중 9점

유형
그래프, DFS, 이분 탐색, 구현
정답자
아직 제출이 없습니다

문제

Hu the Tomb Raider has entered a new tomb! It is full of gargoyles, mirrors, and obstacles. There is a door, with treasure beyond. Hu must unlock the door guarding the treasure. On that door is written, in an ancient tongue, the secret to opening the door:

Every face of every gargoyle shall see a face of a gargoyle.

This means that the gargoyles must be rotated in such a way that there is a path for a beam of light to connect each gargoyle’s face to another gargoyle’s face (possibly of its own). The beam of light may reflect in mirrors.

The floorplan of the tomb can be described as a rectangular n×m grid of cells:

  • A Dot (‘.’) represents an empty cell.
  • A Hash (‘#’) represents an obstacle.
  • A Slash (‘/’) represents a double-sided mirror, as does a Backslash (‘\’) .
  • A character ‘V’ represents a gargoyle with two faces facing top and bottom.
  • A character ‘H’ represents a gargoyle with two faces facing left and right.

In addition to the ‘\’ and ‘/’ mirrors, the tomb is surrounded by walls of mirrors. The following common sense about light is assumed:

  1. Light travels in a straight line through empty cells.
  2. Two beams of light can intersect without interfering with each other.
  3. A ‘\’ mirror reflects light coming from the top/bottom/left/right to the right/left/bottom/top. A ‘/’ mirror reflects light coming from the top/bottom/left/right to the left/right/top/bottom.
  4. Light is reflected by 180 degrees when it hits a wall (walls are all mirrors).
  5. Light is blocked by obstacles and gargoyles.

Hu may rotate any gargoyle by 90 degrees. As time is running short, he wants to know the minimum number of gargoyles that have to be rotated in order to unlock the treasure door.

입력

The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 500), which are the dimensions of the tomb.

Each of the next n lines contains a string s (|s| = m) with the characters described above. This is the floorplan of the tomb.

출력

Output a single integer, which is the minimum number of gargoyles that have to be rotated in order to unlock the treasure door. If the puzzle has no solution, output −1.

힌트

The above are illustrations of Sample Input/Output 1 with the initial configuration on the left, and the solution of the puzzle on the right. Three gargoyles are rotated to solve the puzzle.

예제3

  1. 예제 1

    입력
    5 5
    /.V.\
    ./.V.
    ..#..
    .V.#.
    \.V./
    
    예상 출력
    3
    
  2. 예제 2

    입력
    2 5
    V...\
    H...V
    
    예상 출력
    -1
    
  3. 예제 3

    입력
    2 2
    VV
    VV
    
    예상 출력
    0