poj 1376

原创 2018年04月17日 15:56:57

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



这道题意思没看懂,在队友的讲解下,我在错误的道路上越走越远,好在自己又走回来了



题目的意思就是给你一个地图,但这个时候地图是一个面,所以要转换一下,就是开一个数组,当遇到不能走的方格的时候即输入值为1的时候,就把此时的 i 与 j看成点,mp[i][j]=mp[i+1][j]=mp[i][j+1]=mp[i+1][j+1]=1;这样标记完成以后,这个面就变成了一个个点,并且此时的机器人是有半径的,所以在走的时候不可能走到边界上.

最重要的部分来了:  这道题目,规定的是你在这一秒内只能完成一个动作,即要不向左右转向,要不向前走1-3步,不能转完向之后立马走三步,这样是不允许的,而且不能转180度.

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
struct note
{
    int x,y,id,s;
    bool operator == (const note &a) const
    {
        return a.y==y&&a.x==x;
    }
} st,ed;
int vis[100][100][10];
int mp[100][100];
int n,m;
int dir[4][2]= {0,1,1,0,0,-1,-1,0};
int check(int x,int y,int id)
{
    if(x>0&&y>0&&x<n&&y<m&&vis[x][y][id]==0)
        return 1;
    return 0;
}
int bfs()
{
    queue<note>Q;
    memset(vis,0,sizeof(vis));
    st.s=0;
    Q.push(st);
    note now,temp;
    while(!Q.empty())
    {
        now=Q.front();
        Q.pop();
        if(now==ed)
        {
            printf("%d\n",now.s);
            return 1;
        }

        temp=now;
        temp.s++;
        temp.id=(temp.id+1)%4;
        if(vis[temp.x][temp.y][temp.id]==0)
        {
            vis[temp.x][temp.y][temp.id]=1;
            Q.push(temp);
        }
        temp=now;
        temp.s++;
        temp.id=(temp.id-1+4)%4;
        if(vis[temp.x][temp.y][temp.id]==0)
        {
            vis[temp.x][temp.y][temp.id]=1;
            Q.push(temp);
        }

        for(int i=1; i<=3; i++)
        {
            temp=now;
            temp.x=now.x+dir[temp.id][0]*i;
            temp.y=now.y+dir[temp.id][1]*i;
            if(mp[temp.x][temp.y]==1)break;
            if(check(temp.x,temp.y,temp.id))
            {
                temp.s++;
                vis[temp.x][temp.y][temp.id]=1;
                Q.push(temp);
            }
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        int t;
        memset(mp,0,sizeof(mp));
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                scanf("%d",&t);
                if(t==1)
                {
                    mp[i][j]=mp[i+1][j]=mp[i][j+1]=1;
                    mp[i+1][j+1]=1;
                }
            }
        }
        scanf("%d%d%d%d",&st.x,&st.y,&ed.x,&ed.y);
        char mp[20];
        scanf("%s",mp);
        if(mp[0]=='s')
            st.id=1;
        else if(mp[0]=='e')
            st.id=0;
        else if(mp[0]=='w')
            st.id=2;
        else
            st.id=3;
        if(bfs()==0)
            printf("-1\n");
    }
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wang19980123/article/details/79975945

POJ-1376-Robot

题目大意是给一个图,然后给你起点和终点,以及开始所处的方向,要求你求出从起点到终点的最小步数,如果无法到达则输出-1。这个题注意有4点: 1、它一秒可以执行2种命令,一种是向现在所面向的方向走1-3...
  • z309241990
  • z309241990
  • 2013-02-27 09:32:25
  • 2039

POJ1376-Robot(广度优先搜索)

题目大意:有一张矩阵地图,是由一堆1*1的小个子组成的 ,有一个机器人可以再格子的交界处运动。机器人要占四个格子 机器人可以左转或是右转或是前进1,2,3个单位,这些操作的完成时间都是1s。现在机...
  • qq_29660153
  • qq_29660153
  • 2017-03-28 17:22:03
  • 156

POJ 1376 Robot (BFS)

处理起来有个技巧:输入中的“1”表示真实图中黑框的左上角,所以以1为左上角,将右,下,和右下的格子置1,于是有面积的robot缩小成一个点了................. 很多要注意的地方: 1....
  • paradiserparadiser
  • paradiserparadiser
  • 2013-08-15 20:28:13
  • 815

poj1376 bfs,机器人

开始时候有点怕, 感觉什么也不会,不过静下来思考思考也就想出来了,一个简单的BFS即可,但是由于队列没有重判,一直爆队列(MLE!)下次一定要注意! (bfs第一次到达便最优?) #include...
  • u011498819
  • u011498819
  • 2014-01-18 19:05:31
  • 573

POJ - 1376 Robot

题意:求在可以一秒沿着既定方向走1到3步和向左或右转90度的情况下,从起点到终点的最短时间 思路:坑的是这机器人还有体积,所以不能走到边界,然后就是单纯的BFS #include #includ...
  • u011345136
  • u011345136
  • 2014-06-09 18:32:44
  • 851

poj 1376 Robot 广搜

//poj 1376 //sep9 #include #include using namespace std; const int maxN=128; int n,m,sx,sy,sdir,e...
  • sepNINE
  • sepNINE
  • 2016-07-15 13:26:08
  • 289

POJ1376(A*)

题目链接:http://poj.org/problem?id=1376 优先队列默认为大根堆,如果是结构体的比较或者是小根堆的话要自定义cmp函数,但是这里cmp函数和sort里的函数是不一样的,要这...
  • qust1508060414
  • qust1508060414
  • 2016-09-04 16:00:42
  • 147

POJ1376简单广搜

题意:       给你一个n*m的矩阵,然后给你机器人的起点和终点,还有起点的方向,然后每次机器人有两种操作,左右旋转90度,或者是朝着原来的方向走1,2或者3步,机器人再走的过程中不能碰到格子,...
  • u013761036
  • u013761036
  • 2015-02-06 16:37:25
  • 728

poj 1376 Robot(bfs)

Robot Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6813   Accepted...
  • u014634338
  • u014634338
  • 2015-08-25 14:33:12
  • 412

poj 2688 Cleaning Robot

题意:给一个 n*m的图,有一个机器人从一点开始清理垃圾,要求把所有的垃圾清理完,求最短的路径思路:这个题可以先用bfs求出任意两点间的距离然后转换成tsp问题,用dfs解。这个题还可以用状态压缩bf...
  • Euler_M
  • Euler_M
  • 2011-01-13 16:39:00
  • 1497
收藏助手
不良信息举报
您举报文章:poj 1376
举报原因:
原因补充:

(最多只允许输入30个字)