zitian246的博客

你若安好便是晴天

POJ1376 --Robot+译文(BFS+地图转化+细心)

The Robot Moving Institute is using a robot in their local store to transport different items. Of course the robot should spend only the minimum time necessary when travelling from one place in the store to another. The robot can move only along a straight line (track). All tracks form a rectangular grid. Neighbouring tracks are one meter apart. The store is a rectangle N x M meters and it is entirely covered by this grid. The distance of the track closest to the side of the store is exactly one meter. The robot has a circular shape with diameter equal to 1.6 meter. The track goes through the center of the robot. The robot always faces north, south, west or east. The tracks are in the south-north and in the west-east directions. The robot can move only in the direction it faces. The direction in which it faces can be changed at each track crossing. Initially the robot stands at a track crossing. The obstacles in the store are formed from pieces occupying 1m x 1m on the ground. Each obstacle is within a 1 x 1 square formed by the tracks. The movement of the robot is controlled by two commands. These commands are GO and TURN. 
The GO command has one integer parameter n in {1,2,3}. After receiving this command the robot moves n meters in the direction it faces. 

The TURN command has one parameter which is either left or right. After receiving this command the robot changes its orientation by 90o in the direction indicated by the parameter. 

The execution of each command lasts one second. 

Help researchers of RMI to write a program which will determine the minimal time in which the robot can move from a given starting point to a given destination. 

Input
The input consists of blocks of lines. The first line of each block contains two integers M <= 50 and N <= 50 separated by one space. In each of the next M lines there are N numbers one or zero separated by one space. One represents obstacles and zero represents empty squares. (The tracks are between the squares.) The block is terminated by a line containing four positive integers B1 B2 E1 E2 each followed by one space and the word indicating the orientation of the robot at the starting point. B1, B2 are the coordinates of the square in the north-west corner of which the robot is placed (starting point). E1, E2 are the coordinates of square to the north-west corner of which the robot should move (destination point). The orientation of the robot when it has reached the destination point is not prescribed. We use (row, column)-type coordinates, i.e. the coordinates of the upper left (the most north-west) square in the store are 0,0 and the lower right (the most south-east) square are M - 1, N - 1. The orientation is given by the words north or west or south or east. The last block contains only one line with N = 0 and M = 0. 
Output
The output contains one line for each block except the last block in the input. The lines are in the order corresponding to the blocks in the input. The line contains minimal number of seconds in which the robot can reach the destination point from the starting point. If there does not exist any path from the starting point to the destination point the line will contain -1. 
Sample Input
9 10
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
7 2 2 7 south
0 0
Sample Output
12


译文::机器人搬运研究所正在当地商店中使用机器人来运输不同的物品。当然,机器人只需要花费从商店的一个地方到另一个地方旅行所需的最短时间。机器人只能沿着一条直线(轨道)移动。所有轨道形成一个矩形网格。相邻的轨道相隔一米。该商店是一个矩形的N×M米,它完全被这个网格覆盖。最接近商店一侧的跑道距离只有一米。机器人具有直径等于1.6米的圆形形状。轨道穿过机器人的中心。机器人总是面向北,南,西或东。轨道位于南北和东西方向。机器人只能朝它面对的方向移动。它的方向可以在每个轨道交叉处改变。最初机器人站在轨道交叉处。商店中的障碍物是由地面上的1m×1m的碎片组成的。每个障碍物都在由轨道形成的1×1方格内。机器人的运动由两个命令控制。这些命令是GO和TURN。
GO命令在{1,2,3}中有一个整数参数n。接收到这个命令后,机器人按照它所面对的方向移动n米。
TURN命令有一个参数可以是左或右。接收到该命令后,机器人按照参数指示的方向将其方向改变90°。
每个命令的执行持续一秒钟。

帮助RMI的研究人员编写一个程序,该程序将确定机器人从一个给定的起点移动到一个给定的目的地的最短时间。



题意:求机器人从起点到终点找一条用时最少的路径,有两种命令,一种是走,只能走1,2,3步,另一种是转弯,但只能向左或向右转,这两种操作都会耗时1s。 机器人有占有一定的空间,不能走到边界。题目给出的是面,不能直接构图,需要化成点坐标。


思路:直接BFS,但处理时需要注意很多细节。比如枚举每一步走的长度时,要判断中间点是否可走,而不是只判断目标点。对于方向的枚举,也要对应起来。


代码如下,仅供参考:

#include<stdio.h>
#include<queue>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
int mp[100][100];
int vis[100][100][10];
char s[20][20]= {"west","east","south","north"};//西东南北
int dir[4][2]= {0,-1,0,1,1,0,-1,0}; //与西东南北四个方向要对应(上北下南,左西右东)
int n,m;
struct note
{
    int x,y,t;
    int dir;  //记录此时方向
};
int check(int x,int y,int dir)   //判断是否越界,或已走过
{
    if(x>0 && x<n && y>0 && y<m && !vis[x][y][dir])  //边界不可走,机器人有一定的体积
        return 1;
    return 0;
}
int  bfs(int a,int b,int c,int d,int x)
{
    queue<note>Q;
    note p,q;
    p.x=a;
    p.y=b;
    p.dir=x;              //记录此时方向
    p.t=0;
    vis[p.x][p.y][p.dir]=1;   ///注意三维vis标记的是方向,不是时间
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        if(p.x==c&&p.y==d)
        {
            return p.t;
        }
        //转向(左右)
        if(p.dir==0||p.dir==1) //当方向·为西东时,枚举南北
        {
            q=p;
            q.t++;
            for(int i=2;i<=3;i++)
            if(!vis[q.x][q.y][i])
            {
                q.dir=i;             //记录此时方向
                vis[q.x][q.y][q.dir]=1;
                Q.push(q);
            }
        }
        if(p.dir==2||p.dir==3)//当方向为南北时,枚举西东
        {
            q=p;
            q.t++;
            for(int i=0;i<=1;i++)
            if(!vis[q.x][q.y][i])
            {
                q.dir=i;
                vis[q.x][q.y][q.dir]=1;
                Q.push(q);
            }
        }
        //前进
        for(int i=1; i<=3; i++)
        {
            q=p;
            q.x+=i*dir[q.dir][0];
            q.y+=i*dir[q.dir][1];
            if(mp[q.x][q.y]==1) ///判断是否可走,不为0就跳出,中间点也在此判断
                break;
            if(check(q.x,q.y,q.dir))
            {
                q.t++;
                vis[q.x][q.y][q.dir]=1;
                Q.push(q);
            }
        }
    }
    return -1;
}

int main()
{
    char st[20];
    while(~scanf("%d%d",&n,&m))
    {
        int t;
        int a,b,c,d;
        if(n==0&&m==0)break;
        memset(mp,0,sizeof(mp));
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                scanf("%d",&t);
                if(t)
                {
                    mp[i][j]=mp[i+1][j]=mp[i][j+1]=mp[i+1][j+1]=1;//建图
                }
            }
        }
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&a,&b);
        scanf("%d%d",&c,&d);
        scanf("%s",&st);
        int x;
        for(int i=0; i<4; i++)
        {
            if(strcmp(s[i],st)==0)   //记录此时方向
            {
                x=i;
                break;
            }
        }
        int flag=bfs(a,b,c,d,x);
        printf("%d\n",flag);
    }
    return 0;
}

阅读更多
版权声明:听说别人都在这里写版权声明。 https://blog.csdn.net/zitian246/article/details/79977053
文章标签: POJ1376 BFS Robot
个人分类: 搜索
想对作者说点什么? 我来说一句

OpenGL实现RGB到YUV的转化

-

学院

1970年01月01日 08:00

poj 1376

一道搜索题,用BFS

u011008379 u011008379

2014-10-07 11:04:36

阅读数:462

没有更多推荐了,返回首页

不良信息举报

POJ1376 --Robot+译文(BFS+地图转化+细心)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭