HDU 4166 Robot Navigation

Robot Navigation

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1161    Accepted Submission(s): 274


Problem Description
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.
 

Input
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.
 

Output
For each case, print its case number, the modulus, as well as the remainder of the number of different programs when divided by the modulus m. The output of each case should be on a single line, in the format demonstrated below. If there is no program that can move the robot to its destination, output -1 for the number of different programs.
 

Sample 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
 

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

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   4161  4165  4164  4163  4162 

 



就三种指令,向左向右向前。每种指令一秒。求从出发地到目标的方案数(时间最小)。

开两个三维数组。一个记录步数,一个记录方法数。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#define MAX 1111
#define INF 0x7FFFFFFF
using namespace std;
char mat[1010][1010];
int dx[]= {1,0,-1,0};
int dy[]= {0,1,0,-1}; //四个方向  E   S W  N
int sum[1010][1010][4];   //  fang fa shu
int step[1010][1010][4];   //zuixiaobushu
int n,m;
struct node
{
    int x,y,dir;   //zuobiao  fangxiang
};
int judge(char x)
{
    if(x=='S')
        return 0;
    else if(x=='E')
        return 1;
    else if(x=='N')
        return 2;
    else if(x=='W')
        return 3;
}
bool check(int x,int y)
{
    if(x>=0 &&x<n && y>=0 &&y<m &&mat[x][y]=='.')
        return 1;
    return 0;
}
int bfs(int x,int y,int dir,int mo,int edx,int edy)
{
    int i;
    queue<node>q;
    while(!q.empty())q.pop();
    node st,ed;
    st.x=x;
    st.y=y;
    st.dir=dir;
    memset(sum,0,sizeof(sum));
    memset(step,-1,sizeof(step));
    step[x][y][dir]=0;
    sum[x][y][dir]=1;
    q.push(st);
    while(!q.empty())
    {
        node tt=q.front();
        q.pop();
        node qq;
        for(int i=0; i<4; i++)
        {
            if(abs(tt.dir-i)==1 ||abs(tt.dir-i) ==3)
            {
                qq=tt;
                if(step[tt.x][tt.y][i]==-1)
                {
                    step[tt.x][tt.y][i]=step[tt.x][tt.y][tt.dir]+1;
                    sum[tt.x][tt.y][i]=sum[tt.x][tt.y][tt.dir];
                    qq.dir=i;
                    q.push(qq);
                }
                else
                {
                    if(step[tt.x][tt.y][i]==step[tt.x][tt.y][tt.dir]+1)
                        sum[tt.x][tt.y][i]=(sum[tt.x][tt.y][i]+sum[tt.x][tt.y][tt.dir])%mo;
                }
            }
        }
        qq=tt;
        qq.x+=dx[tt.dir];
        qq.y+=dy[tt.dir];
        if(check(qq.x,qq.y))
        {
            if(step[qq.x][qq.y][qq.dir]==-1)
            {
                step[qq.x][qq.y][qq.dir]=step[tt.x][tt.y][tt.dir]+1;
                sum[qq.x][qq.y][qq.dir]=sum[tt.x][tt.y][tt.dir];
                //=sum[tt.x][tt.y][dir]+1;
                //  tt.dir=i;
                q.push(qq);
            }
            else
            {
                if(step[qq.x][qq.y][qq.dir]==step[tt.x][tt.y][tt.dir]+1)
                    sum[qq.x][qq.y][qq.dir]=(sum[qq.x][qq.y][qq.dir]+sum[tt.x][tt.y][tt.dir])%mo;
            }
        }
    }
    int ans=INF;
    for(i=0; i<4; i++)
    {
        if(step[edx][edy][i]!=-1)
            ans=min(ans,step[edx][edy][i]);
    }
    if(ans==INF)
        return -1;
    int res=0;
    for(i=0; i<4; i++)
    {
        if(ans==step[edx][edy][i])
        {
            res=(res+sum[edx][edy][i])%mo;

        }
        //   cout<<sum[edx][edy][i]<<endl;
    }
    // printf("%d\n",res);
    return res;

}
int main()
{
    int mo;
    int Case=0;
    while(cin>>n>>m>>mo)
    {
        if(mo==0)
            break;
        for(int i=0; i<n; i++)
            cin>>mat[i];
        int stx,sty,edx,edy;
        char str[10];
        cin>>stx>>sty>>edx>>edy>>str;
        int ans=judge(str[0]);
        int x=bfs(stx,sty,ans,mo,edx,edy);
        printf("Case %d: %d %d\n",++Case,mo,x);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值