poj-robit

Robot
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7737 Accepted: 2548

Description

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

Source

Central Europe 1996

 题目大意是给一个图,然后给你起点和终点,以及开始所处的方向,要求你求出从起点到终点的最小步数,如果无法到达则输出-1。这个题注意有4点:

1、它一秒可以执行2种命令,一种是向现在所面向的方向走1-3步,另外一种是向左或向右90度转向(不能向后转)。

2、图中为1的是障碍物,是不允许通过的,包括边界也不能允许,这一点需要注意下。

3、对于题目所给处理,存在起点等于终点的情况,还有就是终点是无法到达的(比如终点为障碍物)需要特殊处理一下。

这道题有几个难点,如果解决了,那么这道题会非常简单

(1)判断方向(最难点),当时为了这个判断方向,我想了很长时间,如何才能判断方向?

然后我就记录了一下i  如果相邻两步的i不相同,那么就说明转弯了

(2)图转换问题;因为给你的图并不是机器人走的图,那只是商店的平面图,

而机器人走的是两边的线,所以应该把图转换一下,换成机器人可以走的图。

剩下的就和平时的最短路径一样了

第一次代码

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int map1[55][55],map[55][55];
int n,m;
int vis[55][55];
int dir[4][2]={{1,0},{0,-1},{-1,0},{0,1}};
int x1,y1,x2,y2,di;
char s[10];
struct node
{
    int x,y,time,di,step;
}q1,q2,q3;
void nextstep()
{
    if(q2.di!=q1.di)   //不必担心有转方向转2次的可能
    {
        q2.time+=2;
        q2.step=1;
    }
    else        //如果不转方向,那么就step++ 凑够3步了再算成1次指令
    {
        q2.step++;
        if(q2.step>3)
        {
            q2.step=0;
            q2.time++;
        }
    }
}
int dfs()
{
    queue<node>q;
    q1.x=x1,q1.y=y1,q1.di=di,q1.time=0,q1.step=0;
    q.push(q1);
    while(!q.empty())
    {
        q1=q.front();
        q.pop();
        printf("%d %d %d\n",q1.x,q1.y,q1.time);
        if(q1.x==x2&&q2.y==y2)
        {
            return q1.time;
        }
        for(int i=0;i<4;i++)
        {
            q2.x=q1.x+dir[i][0];
            q2.y=q1.y+dir[i][1];
            q2.di=i;
            if(q2.x>0&&q2.y>0&&q2.x<m&&q2.y<n&&map[q2.y][q2.x]!=1&&vis[q2.y][q2.x]==false)
            {
                vis[q2.y][q2.x]=true;
                nextstep();
                q.push(q2);

            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
        {
            return 0;
        }
        memset(map1,0,sizeof(map1));
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&map1[i][j]);
            }
        }
        scanf("%d%d%d%d%s",&x1,&y1,&x2,&y2,s);
        if(s[0]=='n')
        {
            di=0;
        }
        if(s[0]=='w')
        {
            di=1;
        }
        if(s[0]=='s')
        {
            di=2;
        }
        if(s[0]=='e')
        {
            di=3;
        }
        memset(map,0,sizeof(map));
        for(int i=0;i<=n;i++)   //转换图
        {
            for(int j=0;j<=m;j++)
            {
                if(map1[i][j]==1)
                {
                    map[i][j]=1;
                    map[i+1][j]=1;
                    map[i][j+1]=1;
                    map[i+1][j+1]=1;
                }
            }
        }
        if(map[y2][x2]==1)
        {
            printf("-1\n");
            return 0;
        }
        int TL=dfs();
        printf("%d\n",TL);
    }
}

下面就是愉快的dbug时间

首先样例都不正确

各步骤都检查一遍,发现 图形转换都错了

哎!粗心害死人

还有非常重要的一点

这个坐标不是胡写的

需要和下面的方向对应,比如下面s[0]='n' di=0  那么 dir中第一个方向一定要保证是向北的 

写完了

不用交我也知道是WA

有一个部分我还没处理

那就是开头的部分

开头的部分可能会出现转头2次的情况

而这是我没考虑的

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int map1[55][55],map[55][55];
int n,m;
int vis[55][55];
int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
int x1,y1,x2,y2,di;
char s[10];
struct node
{
    int x,y,time,di,step;
}q1,q2,q3;
void nextstep()   //从q1走到q2时间time的变化
{
    if(q2.di!=q1.di)   //不必担心有转方向转2次的可能
    {
        if(q2.di-q1.di==2||q2.di-q1.di==-2)
        {
            q2.time++;
        }
        if(q1.step!=0)
        {
            q2.time++;
        }
        q2.time+=1;
    }
    else        //如果不转方向,那么就step++ 凑够3步了再算成1次指令
    {
        q2.step=q1.step+1;
        if(q2.step>3)
        {
            q2.step=0;
            q2.time++;
        }
    }
}
int dfs()
{
    queue<node>q;
    q1.x=x1,q1.y=y1,q1.di=di,q1.time=0,q1.step=0;
    q.push(q1);
    while(!q.empty())
    {
        q1=q.front();
        q.pop();
        //printf("%d %d %d\n",q1.x,q1.y,q1.time);
        if(q1.x==x2&&q2.y==y2)
        {
            return q1.time;
        }
        for(int i=0;i<4;i++)
        {
            q2.x=q1.x+dir[i][0];
            q2.y=q1.y+dir[i][1];
            q2.di=i;
            q2.step=q1.step;q2.time=q1.time;
            if(q2.x>0&&q2.y>0&&q2.x<m&&q2.y<n&&map[q2.y][q2.x]!=1&&vis[q2.y][q2.x]==false)
            {
                vis[q2.y][q2.x]=true;
                nextstep();
                //printf("%d %d %d*\n",q2.x,q2.y,q2.time);
                q.push(q2);
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
        {
            return 0;
        }
        memset(map1,0,sizeof(map1));
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&map1[i][j]);
            }
        }
        scanf("%d%d%d%d%s",&x1,&y1,&x2,&y2,s);
        if(s[0]=='n')
        {
            di=0;
        }
        if(s[0]=='w')
        {
            di=1;
        }
        if(s[0]=='s')
        {
            di=2;
        }
        if(s[0]=='e')
        {
            di=3;
        }
        memset(map,0,sizeof(map));
        for(int i=0;i<=n;i++)   //转换图  转换成机器人可以走的直观图
        {
            for(int j=0;j<=m;j++)
            {
                if(i==0||i==n||j==0||j==m)
                {
                    map[i][j]=1;
                }
                if(map1[i][j]==1)
                {
                    map[i][j]=1;
                    map[i-1][j]=1;
                    map[i-1][j-1]=1;
                    map[i][j-1]=1;
                }
            }
        }
        /*for(int i=0;i<=n;i++)  //测试数据
        {
            for(int j=0;j<=m;j++)
            {
                printf("%d ",map[i][j]);
            }
            printf("\n");
        }*/
        if(map[y2][x2]==1)
        {
            printf("-1\n");
            return 0;
        }
        int TL=dfs();
        printf("%d\n",TL);
    }
    return 0;
}
提交  WA

可能是我没有找到最小的值;

应该找最小值,同时发现一个小错误,

 if(q2.step>=3)
应该这样改一下

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int map1[55][55],map[55][55];
int n,m,minnum;
int vis[55][55];
int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
int x1,y1,x2,y2,di;
char s[10];
struct node
{
    int x,y,time,di,step;
}q1,q2,q3;
void nextstep()   //从q1走到q2时间time的变化
{
    if(q2.di!=q1.di)   //不必担心有转方向转2次的可能
    {
        if(q2.di-q1.di==2||q2.di-q1.di==-2)
        {
            q2.time++;
        }
        if(q1.step!=0)
        {
            q2.time++;
        }
        q2.time+=1;
    }
    else        //如果不转方向,那么就step++ 凑够3步了再算成1次指令
    {
        q2.step=q1.step+1;
        if(q2.step>=3)
        {
            q2.step=0;
            q2.time++;
        }
    }
}
int dfs()
{
    queue<node>q;
    q1.x=x1,q1.y=y1,q1.di=di,q1.time=0,q1.step=0;
    q.push(q1);
    while(!q.empty())
    {
        q1=q.front();
        q.pop();
        //printf("%d %d %d\n",q1.x,q1.y,q1.time);
        if(q1.x==x2&&q1.y==y2)
        {
            if(q1.time<minnum)
            {
                if(q1.step!=0)
                {
                    q1.time++;
                }
                minnum=q1.time;
            }
        }
        for(int i=0;i<4;i++)
        {
            q2.x=q1.x+dir[i][0];
            q2.y=q1.y+dir[i][1];
            q2.di=i;
            q2.step=q1.step;q2.time=q1.time;
            if(q2.x>0&&q2.y>0&&q2.x<m&&q2.y<n&&map[q2.y][q2.x]!=1&&vis[q2.y][q2.x]==false)
            {
                vis[q2.y][q2.x]=true;
                nextstep();
                //printf("%d %d %d*\n",q2.x,q2.y,q2.time);
                q.push(q2);
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
        {
            return 0;
        }
        memset(map1,0,sizeof(map1));
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&map1[i][j]);
            }
        }
        scanf("%d%d%d%d%s",&x1,&y1,&x2,&y2,s);
        if(s[0]=='n')
        {
            di=0;
        }
        if(s[0]=='w')
        {
            di=1;
        }
        if(s[0]=='s')
        {
            di=2;
        }
        if(s[0]=='e')
        {
            di=3;
        }
        minnum=99999;
        memset(map,0,sizeof(map));
        for(int i=0;i<=n;i++)   //转换图  转换成机器人可以走的直观图
        {
            for(int j=0;j<=m;j++)
            {
                if(i==0||i==n||j==0||j==m)
                {
                    map[i][j]=1;
                }
                if(map1[i][j]==1)
                {
                    map[i][j]=1;
                    map[i-1][j]=1;
                    map[i-1][j-1]=1;
                    map[i][j-1]=1;
                }
            }
        }
        /*for(int i=0;i<=n;i++)  //测试数据
        {
            for(int j=0;j<=m;j++)
            {
                printf("%d ",map[i][j]);
            }
            printf("\n");
        }*/
        if(x1==x2&&y1==y2)
        {
            printf("-1\n");
            return 0;
        }
        if(map[y2][x2]==1)
        {
            printf("-1\n");
            return 0;
        }
        dfs();
        if(minnum==99999)
        {
            printf("-1\n");
        }
        else
        {
            printf("%d\n",minnum);
        }
    }
    return 0;
}

还是WA;

改了一下bug  发现bug太多了  首先 判重 标记位置的时候要标记方向

从一个地方到另一个地方 有很多种走法,如果不标记方向,那么走一次,没找到最小值的时候就不能走了

还有种情况就是  如果第一种方法走到这里最小步数为3 第二种为2  走下一步的时候会用3  而不是2 ,轮到2时候  排重会把它排除了

需要用优先队列;把最优的情况找出来

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int map1[55][55],map[55][55];
int n,m,minnum;
int vis[55][55][4];
int dir[4][2]= {{0,-1},{-1,0},{0,1},{1,0}};
int x1,y1,x2,y2,di;
char s[10];
struct node
{
    int x,y,time,di,step;
    friend bool operator < (node a,node b)
    {
        return a.time>b.time;
    }
} q1,q2,q3;
void nextstep()   //从q1走到q2时间time的变化
{
    if(q2.di!=q1.di)   //如果方向改变,那就先加1
    {
        if(q2.di-q1.di==2||q2.di-q1.di==-2)
        {
            q2.time++;
        }
        q2.time+=2;
        q2.step=1;
    }
    else        //如果不转方向,那么就先判断step是否为0,为0就加一(先计步)
    {
        q2.step++;
        if(q2.step==1)  //跨出第一步的时候开始+1 后面凑够三了再+1
        {
            q2.time++;
        }
        //printf("%d %d %d (%d) [%d]*-*\n",q2.x,q2.y,q2.time,q2.step,q2.di);
        if(q2.step==3)
        {
            q2.step=0;
        }
    }
}
int dfs()
{
    priority_queue<node>q;   //优先队列
    q1.x=x1,q1.y=y1,q1.di=di,q1.time=0,q1.step=0;
    q.push(q1);
    while(!q.empty())
    {
        q1=q.top();    //取出time最小的继续广搜
        q.pop();
        //printf("%d %d %d (%d) [%d]\n",q1.x,q1.y,q1.time,q1.step,q1.di);
        if(q1.x==x2&&q1.y==y2)
        {
            if(q1.time<minnum)
            {
                minnum=q1.time;
            }
        }
        for(int i=0; i<4; i++)
        {
            q2.x=q1.x+dir[i][0];
            q2.y=q1.y+dir[i][1];
            q2.di=i;               //记录q2的方向
            q2.step=q1.step;
            q2.time=q1.time;
            //printf("%d %d %d (%d) [%d]*\n",q1.x,q1.y,q1.time,q1.step,q1.di);
            if(q2.x>0&&q2.y>0&&q2.x<m&&q2.y<n&&map[q2.y][q2.x]!=1&&vis[q2.y][q2.x][q2.di]==false)
            {
                vis[q2.y][q2.x][q2.di]=true;
                //printf("%d %d %d (%d) [%d]*\n",q2.x,q2.y,q2.time,q2.step,q2.di);
                nextstep();
                //printf("%d %d %d (%d) [%d]\n",q2.x,q2.y,q2.time,q2.step,q2.di);
                q.push(q2);
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
        {
            return 0;
        }
        memset(map1,0,sizeof(map1));
        memset(vis,false,sizeof(vis));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                scanf("%d",&map1[i][j]);
            }
        }
        scanf("%d%d%d%d%s",&y1,&x1,&y2,&x2,s);
        if(s[0]=='n')    //把输入的方向转化为数字方向
        {
            di=0;
        }
        if(s[0]=='w')
        {
            di=1;
        }
        if(s[0]=='s')
        {
            di=2;
        }
        if(s[0]=='e')
        {
            di=3;
        }
        minnum=99999;
        memset(map,0,sizeof(map));
        for(int i=0; i<=n; i++) //转换图  转换成机器人可以走的直观图
        {
            for(int j=0; j<=m; j++)
            {
                if(i==0||i==n||j==0||j==m)
                {
                    map[i][j]=1;    //边界不能走,直接就判为1
                }
                if(map1[i][j]==1)   //物品周围的4个点都判为1,表示不能走
                {
                    map[i][j]=1;
                    map[i-1][j]=1;
                    map[i-1][j-1]=1;
                    map[i][j-1]=1;
                }
            }
        }
        /*for(int i=0;i<=n;i++)  //测试数据
        {
            for(int j=0;j<=m;j++)
            {
                printf("%d ",map[i][j]);
            }
            printf("\n");
        }*/
        if(x1==x2&&y1==y2)//  如果起点等于终点,特判
        {
            printf("0\n");
            continue;
        }
        if(map[y2][x2]==1)  //如果终点直接被堵,直接输出-1,减少运算时间
        {
            printf("-1\n");
            continue;
        }
        vis[y1][x1][di]==true;
        dfs();
        if(minnum==99999)
        {
            printf("-1\n");
        }
        else
        {
            printf("%d\n",minnum);
        }
    }
    return 0;
}

样例过了,找的几组数据也都正确,可是提交上去还是WA;

崩溃,有没有大神帮我看看实在不会写啊

找了很长时间,先放下,等等再找bug吧,还要赶着往下学





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值