Out of Sight

Time limit1sMemory limit128 MB

Summary
Given a walled grid, your start, and the step-by-step routes of several robots, find the maximum number of turns you can survive without any robot seeing you along a row or column.
Level

Hard8 of 10

Topics
BFS, Simulation, Greedy, Implementation
Solved
No attempts yet

Problem

You fell asleep at your desk again and woke to the sound of newly installed security-camera robots rolling out into the office corridors. If any robot photographs you, you are in serious trouble. Fortunately, you can pull up on your computer the exact route each robot is programmed to follow, so you can plan your own moves to stay out of every robot's line of sight.

The robots move in discrete unit steps, each step going north, east, south, or west. On each step you may also move one unit north, east, south, or west (where a wall permits), or you may stay in place. You and every robot move at the same time, one step per turn.

Neither you nor any robot may move through a wall or leave the building.

Immediately after moving, every robot takes a photograph looking north, south, east, and west. You are captured if you stand on the same row or the same column as a robot with no wall between you and that robot. You are also captured if, at the end of a step, you are standing on the same cell as a robot.

Input

The input contains one or more mazes. Each maze starts with a line holding two integers ww and hh: the width (west-to-east) and the height (north-to-south) of the maze. Neither you nor any robot may leave this rectangular area; it is treated as fully enclosed, although it may also contain interior walls. Input ends when w<3w < 3 or h<3h < 3.

The header is followed by hh lines describing the maze, each containing at least ww characters; only the first ww characters of each line are significant and any extra characters are ignored. Each character means:

  • a space denotes an open cell;
  • X denotes a wall;
  • Y denotes your starting cell and appears exactly once;
  • a single digit kk in the range 00 to 99 denotes the starting cell of robot kk. No digit repeats, and a set of NN robots is labelled with the digits 0,1,…,N−10, 1, \ldots, N-1.

The maze is followed by NN lines, one per robot; the ii-th of these lines gives the moves of robot ii. Each line contains from 00 to 8080 characters, and all NN lines have the same length. Each character is one of N, S, E, W, meaning north, south, east, or west, where north points toward the first maze line and west points toward the first column. The characters list the robot's moves, one per turn.

Output

For each maze, print a single line:

You can hide for M turns.

where MM is the largest number of turns the robots can take while you remain unphotographed. If you can avoid detection for the entire programmed movement of the robots, MM equals the number of moves listed for each robot.

Examples3

  1. Example 1

    Input
    12 7
    XXXXXXXXXXXX
    X      1   X
    X        X X
    X        X X
    X      XXX X
    X    0 XXXYX
    XXXXXXXXXXXX
    NNEWSWWW
    EEEWWWWW
    0 0
    
    Expected output
    You can hide for 2 turns.
    
  2. Example 2

    Input
    5 5
    XXXXX
    XY  X
    X   X
    X0  X
    XXXXX
    EEW
    0 0
    
    Expected output
    You can hide for 3 turns.
    
  3. Example 3

    Input
    5 5
    XXXXX
    XY  X
    X   X
    X0  X
    XXXXX
    EEW
    3 5
    XXX
    XYX
    X X
    X0X
    XXX
    N
    0 0
    
    Expected output
    You can hide for 3 turns.
    You can hide for 0 turns.