Robot Navigation

Time limit1sMemory limit128 MB

Summary
Find the length of the shortest command program that walks a robot from start to destination with turns and moves, and count distinct shortest programs modulo m.
Level

Medium4 of 10

Topics
BFS, Graph, Dynamic programming
Solved
No attempts yet

Problem

A robot has been sent to explore a distant planet. The path the robot should follow is sent each day as a single program, where a program is a sequence of the following three commands.

  • FORWARD: move one unit forward in the direction the robot is facing.
  • TURN LEFT: turn 9090 degrees to the left in place. The robot's location does not change.
  • TURN RIGHT: turn 9090 degrees to the right in place. The robot's location does not change.

The robot carries a sensor that provides a map of the surrounding terrain. The map is an MM-row by NN-column grid, and each cell is identified by a coordinate (r,c)(r, c), where r=0r = 0 is the north edge, r=M−1r = M-1 is the south edge, c=0c = 0 is the west edge, and c=N−1c = N-1 is the east edge. Some cells contain hazards such as craters, and the program must avoid them or the robot may be lost.

Given the robot's starting position and facing direction together with its destination, we want to send the shortest program (the one with the fewest commands) that moves the robot to its destination; the direction it faces at the destination does not matter. Because interplanetary communication is not always reliable, we may need to send several different programs, so we want to know how many distinct shortest programs move the robot to its destination. This count can be very large, so the answer is reported as its remainder modulo a given number mm.

Input

The input consists of several test cases. The first line of each case contains three integers MM, NN, and the modulus mm (0<M,N≤10000 < M, N \le 1000, 0<m≤10000000000 < m \le 1000000000). The next MM lines each contain NN characters describing the map, where . is a cell the robot may enter and * is a hazard. The following line contains four integers r1r_1, c1c_1, r2r_2, c2c_2 and a character dd. (r1,c1)(r_1, c_1) is the robot's starting position and (r2,c2)(r_2, c_2) is the destination; dd is one of N, S, W, E, giving the robot's initial facing direction (north, south, west, east respectively). The starting position and the destination are never hazards. The input terminates on the line where m=0m = 0.

Output

For each case, print one line in the format Case i: m r, where ii is the case number (starting from 11), mm is that case's modulus, and rr is the number of distinct shortest programs that move the robot to its destination, taken modulo mm. If no program can move the robot to its destination, print -1 in place of the count.

Examples1

  1. Example 1

    Input
    3 3 100
    ***
    .*.
    ***
    1 0 1 2 E
    4 4 100
    ****
    *.*.
    *.*.
    *...
    1 1 1 3 N
    4 8 100
    ********
    ...**...
    *......*
    ********
    1 0 1 7 E
    0 0 0
    
    Expected output
    Case 1: 100 -1
    Case 2: 100 2
    Case 3: 100 4