2015
05-23

A robot has been sent to explore a remote planet. To specify the path the robot should take, a program is sent each day. The program consists of a sequence of the following commands:

FORWARD: move forward by one unit.
TURN LEFT: turn left by 90 degrees. The robot remains at the same location.
TURN RIGHT: turn right by 90 degrees. The robot remains at the same location.
The robot also has sensor units which allows it to obtain a map of its surrounding area. The map is represented as a grid of M rows and N columns. Each grid point is represented by a coordinate (r,c) where r = 0 is the north edge of the map, r = M-1 is the south edge, c = 0 is the west edge, and c = N-1 is the east edge. Some grid points contain hazards (e.g. craters) and the program must avoid these points or risk losing the robot.

Naturally, if the initial location and direction of the robot and its destination position are known, we wish to send the shortest program (one consisting of the fewest commands) to move the robot to its destination (we do not care which direction it faces at the destination). You are more interested in knowing the number of different shortest programs that can move the robot to its destination, because we may need to send different sequences as interplanetary communication is not necessarily reliable. However, the number of shortest programs can be very large, so you are satisfied to compute the number as a remainder under some modulus, knowing that something you learned in classes called the Chinese remainder theorem can be used to compute the final answer.

The input consists of a number of cases. The first line of each case gives three integers M, N, and the modulus m (0 < M, N <= 1000, 0 < m <= 1000000000). The next M lines contain N characters each and specify the map. A ‘.’ indicates that the robot can move into that grid point, and a ‘*’ indicates a hazard. The final line gives four integers r1, c1, r2, c2 followed by a character d. The coordinates (r1, c1) specify the initial position of the robot, and (r2, c2) specify the destination. The character d is one of ‘N’, ‘S’, ‘W’, ‘E’ indicating the initial direction of the robot. It is assumed that the initial position and the destination are not hazards. The input is terminated when m = 0.

The input consists of a number of cases. The first line of each case gives three integers M, N, and the modulus m (0 < M, N <= 1000, 0 < m <= 1000000000). The next M lines contain N characters each and specify the map. A ‘.’ indicates that the robot can move into that grid point, and a ‘*’ indicates a hazard. The final line gives four integers r1, c1, r2, c2 followed by a character d. The coordinates (r1, c1) specify the initial position of the robot, and (r2, c2) specify the destination. The character d is one of ‘N’, ‘S’, ‘W’, ‘E’ indicating the initial direction of the robot. It is assumed that the initial position and the destination are not hazards. The input is terminated when m = 0.

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

Case 1: 100 -1
Case 2: 100 2
Case 3: 100 4

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>

using namespace std;
#define N 1003
const int INF = 100000000;
int m,n,Mod,move[][2] = {0,1,-1,0,0,-1,1,0};
struct node
{
int step,dir,x,y;
}p,pt;

int dx,dy,sx,sy,dic,d[N][N][4],dp[N][N][4],vis[N][N][4];
char maz[N][N];
int cas = 0;

bool operator<(node aa,node bb)
{
return aa.step > bb.step;
}
bool inner(int x,int y)
{
return x >= 0 && x < m && y >= 0 && y < n && maz[x][y] == '.';
}
int dfs(int x,int y,int di)
{
if(dp[x][y][di])
return dp[x][y][di];
int k,xx,yy,res = 0;
k = (di + 1) % 4;
if(d[x][y][di] == d[x][y][k] + 1)
res = (res + dfs(x,y,k)) % Mod;

k = (di + 3) % 4;
if(d[x][y][di] == d[x][y][k] + 1)
res = ( res + dfs(x,y,k)) % Mod;

k = (di + 2) % 4;
xx = x + move[k][0];
yy = y + move[k][1];
if(inner(xx,yy) && d[x][y][di] == d[xx][yy][di] + 1)
res = ( res + dfs(xx,yy,di)) % Mod;

return dp[x][y][di] = res;
}
void bfs()
{

if(sx == dx && sy == dy) ///杩欎釜澶彈浼や簡锛岃捣鐐圭粓鐐圭浉鍚屾椂缁撴灉搴旇鏄�1锛屼笉璇ョ壒鍒ょ殑锛岀敾铔囨坊瓒充簡銆�
{
printf("1\n");
return ;
}

memset(vis,0,sizeof(vis));
int i,j,k;
for(i = 0; i < m; ++i)
for(j = 0; j < n; ++j)
for(k = 0; k < 4; ++k)
d[i][j][k] = INF;
priority_queue<node> que;
p.x = dx;
p.y = dy;
p.step = 0;
for(k = 0; k < 4; ++k)
{
p.dir = k;
que.push(p);
d[dx][dy][k] = 0;
}

while(!que.empty())
{
p = que.top();
que.pop();

if(vis[p.x][p.y][p.dir])
continue;
vis[p.x][p.y][p.dir] = 1;

pt = p;
pt.step ++;
k = (p.dir + 1) % 4;

if(pt.step < d[p.x][p.y][k])
{
pt.dir = k;
d[pt.x][pt.y][pt.dir] = pt.step;
que.push(pt);
}

k = (p.dir + 3) % 4;
if(pt.step < d[p.x][p.y][k])
{
pt.dir = k;
d[pt.x][pt.y][pt.dir] = pt.step;
que.push(pt);
}

pt.dir = p.dir;
pt.x += move[p.dir][0];
pt.y += move[p.dir][1];

if(inner(pt.x,pt.y) && !vis[pt.x][pt.y][pt.dir] && pt.step < d[pt.x][pt.y][pt.dir] )
{
d[pt.x][pt.y][pt.dir] = pt.step;
que.push(pt);
}
}
if(d[sx][sy][dic] == INF)
{
printf("-1\n");
return;
}
printf("%d\n",dfs(sx,sy,dic));
}

int main()
{
//freopen("robot.in","r",stdin);
// freopen("12.out","w",stdout);
int i,j,k;
char c[5];
///琚緭鍏ュ潙浜嗭紝鏄庢槑璇磎 == 0缁撴潫鐨勶紝Mod棰濄�傘�傘�傘��
while(scanf("%d%d%d",&m,&n,&Mod) != EOF && Mod)
{
for(i = 0; i < m; ++i)
scanf("%s",maz[i]);
scanf("%d%d%d%d %s",&sx,&sy,&dx,&dy,c);
if(c[0] == 'E')
dic = 2;
else if(c[0] == 'N')
dic = 3;
else if(c[0] == 'W')
dic = 0;
else
dic = 1;
memset(dp,0,sizeof(dp));
for(i = 0; i < 4; ++i)  dp[dx][dy][i] = 1;
printf("Case %d: %d ",++cas,Mod);
bfs();
}
return 0;
}