Under construction

Still being built. Things may change

Laser Puzzle

Time limit2sMemory limit512 MB

Summary
Decide whether John can leave through the door by pushing at most two mirrors or crystals so the laser hits every statue.
Level

Medium6 of 10

Topics
Brute force, Simulation, BFS
Solved
No attempts yet

Problem

An explorer, John, is in trouble. He is trapped in a W×HW \times H rectangular room. A laser emitter on the wall shoots a deadly beam, and John cannot cross or touch it. The wall also has statues and a locked door. The lock opens when the beam hits every statue.

The room has pillars, mirrors, and crystals. A mirror reflects a laser beam. Its angle is vertical, horizontal, or diagonal. A crystal splits an incoming beam into two beams at angles θ±π4\theta \pm \frac{\pi}{4}, where θ\theta is the angle of the incoming beam. John can push one mirror or crystal at a time. He can push the same object several times, but he can push at most two mirrors or crystals in total. If he makes a third push, the door locks forever.

The room is a grid of W×HW \times H unit squares (Figure 1). John moves to a horizontally or vertically adjacent square and never diagonally. Pillars, mirrors, and crystals sit at square centers. When John pushes a mirror or crystal, it moves to the center of the next square at the next moment (Figure 2). You do not need to model the pushing state.

The following conditions hold:

  • The laser beams travel horizontally, vertically, or diagonally.
  • The emitter shoots a beam toward the center of an adjacent square.
  • The emitter is on the wall.
  • John cannot enter a square with a pillar.
  • A mirror reflects a beam at the center of its square, on both sides.
  • A mirror parallel to a beam blocks it.
  • A crystal splits a beam at the center of its square.
  • A diagonal beam does not hit a statue.
  • If a horizontal, vertical, or diagonal beam hits the door, John cannot leave the room.

John must push and move the mirrors and crystals into the right positions, then leave through the door. Write a program that checks whether John can leave the room. Figure 3 shows the initial state, and Figure 4 shows one possible solution.

Input

The first line contains two positive integers WW and HH (2<W,H≤82 < W, H \le 8). The next HH lines give the room map with these characters:

  • #: wall of the room
  • *: pillar
  • S: statue on the wall
  • /: diagonal mirror at angle π4\frac{\pi}{4} from the bottom side of the room
  • \: diagonal mirror at angle 3π4\frac{3\pi}{4} from the bottom side of the room
  • -: horizontal mirror
  • |: vertical mirror
  • O: crystal
  • @: John's initial position
  • .: empty floor
  • L: laser emitter
  • D: door

Output

Print Yes if John can leave the room. Otherwise, print No.

Examples3

  1. Example 1

    Input
    7 8
    ##S####
    #.....#
    S.O...#
    #.*|..#
    L..*..#
    #.O*..D
    #.@...#
    #######
    
    Expected output
    Yes
    
  2. Example 2

    Input
    8 3
    ###L####
    S.../@.#
    ######D#
    
    Expected output
    Yes
    
  3. Example 3

    Input
    8 3
    ##L#####
    S..\.@.#
    ######D#
    
    Expected output
    No