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列地图,起点坐标sx,sy,终点坐标,ex,ey,和起始时机器人所朝向的方向sdir

下列情况算走1步:

1.向前移动:沿着一个方向走1,2或3个格子,算1步;

2.转向:向左转或者向右转,算1步。

另外需要注意的是机器人有他的半径,(好像是)0.8m,格子边长是1m。


解题思路:BFS。

这道题的判断条件很重要,理一理大概有以下几点:

1.方向与坐标应该对应正确,在输入时可以直接处理下初始方向,用0,1,2,3对应。

2.判断下一个格子是不能只判断单独一个格子,而是要把周围四个格子都判断下,因为机器人有半径,判断能不能放得下他;

3.在进行下一步时有两种情况,一是移动,二是转向。移动和转向需要分开写,互不影响;

4.在移动的情况下,如果移动1个格子行不通则直接break,第1个格子不行就算第2个格子或第3个格子符合条件也不能走。(通常情况下对于不符合条件的都是continue。此处用break可以避免很多麻烦)。

5.好像没了



AC代码:(今晚的POJ炸了0.0)

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>   
using namespace std;

int n,m,ex,ey;    
int mp[110][110],book[110][110][4];
int nx[4][2]={-1,0,0,1,1,0,0,-1};//北(上)0东(右)1南(下)2西(左)3 

struct node
{
    int x,y,time,dir;
};

node getnode(int x,int y,int time,int dir)
{
	node q;
	q.x=x;
	q.y=y;
	q.time=time;
	q.dir=dir;
	return q;
}

void bfs(int x,int y,int time,int dir)
{    
    queue<node> q;
    q.push(getnode(x,y,time,dir));

    while(!q.empty())
	{
        int tx=q.front().x;
        int ty=q.front().y;
        for(int i=1;i<4;i++)//123步 
		{
            tx+=nx[q.front().dir][0];
            ty+=nx[q.front().dir][1];   
            //一定要是break,走1步走不通就不再继续2,3步了,还要注意下个格子能不能走下机器人 
    		if(tx<1||tx>=n||ty<1||ty>=m||mp[tx][ty]||mp[tx+1][ty]||mp[tx][ty+1]||mp[tx+1][ty+1]) break;
            if(book[tx][ty][q.front().dir]==0)
			{
                book[tx][ty][q.front().dir]=1;
                if(tx==ex&&ty==ey)
				{
					printf("%d\n",q.front().time+1);
					return;
				}
                q.push(getnode(tx,ty,q.front().time+1,q.front().dir));
            }
        }
        for(int i=0;i<4;i++)
		{
            if(abs(q.front().dir-i)==2) continue;//就是从上一步走过来的就别往回走了 
            if(book[q.front().x][q.front().y][i]==0)//原地转向不需要判断边界 
            {
            	book[q.front().x][q.front().y][i]=1;
            	q.push(getnode(q.front().x,q.front().y,q.front().time+1,i));
			}
            
        }
		q.pop();
    }
    printf("-1\n");
}
    
int main()
{   
    while(~scanf("%d%d",&n,&m)&&n+m)
	{    
        for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++) 
			{
				scanf("%d",&mp[i][j]); 
			}
		}
		
		int sx,sy,sdir;
		char str[10];
        scanf("%d%d%d%d%s",&sx,&sy,&ex,&ey,str);
        if(str[0]=='n') sdir=0;
		if(str[0]=='e') sdir=1;
		if(str[0]=='s') sdir=2;
		if(str[0]=='w') sdir=3;   
		
        if(sx==ex&&sy==ey)
		{
            printf("0\n");
            continue;
        }

        memset(book,0,sizeof(book));
		book[sx][sy][sdir]=1;
		
        bfs(sx,sy,0,sdir);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值