Robot(一道简单的BFS)

Robot     POJ - 1376

https://cn.vjudge.net/problem/POJ-1376
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,2,3步
向左转,向右转
每个步骤耗时为1

思路

BFS

这道题的坑

只能向左转向右转,不能向后转
若第一步走不了,第二步和第三步一定走不了
机器人很胖,不能走边界
障碍物是障碍物,路线是路线
有可能起始位置有障碍物
有可能终点位置有障碍物
若没有路径一定要跳出来
障碍物不可以走,但走过的路还是可以走的
有可能起始位置就是终点位置
(别问我怎么知道这么多坑,要不是20多个WA我就信了,还是不细心,漏洞太多了)

代码:
#include <cstdio>
#include <cstring>
using namespace std;
int bx,by,ex,ey,bs,n,m;
char bf[10];
int mp[100][100][4];
int ans;
void bl(int x,int y,int z){
	if(z==0){//向这个方向前进1,2,3步,若第一步有障碍,则1,2,3走不了
		if(x-1>=0&&mp[x-1][y][z]==0)                    mp[x-1][y][z]=ans+1;
		if(x-2>=0&&mp[x-2][y][z]==0&&mp[x-1][y][z]!=-1) mp[x-2][y][z]=ans+1;
		if(x-3>=0&&mp[x-3][y][z]==0&&mp[x-1][y][z]!=-1) mp[x-3][y][z]=ans+1;
	}
	if(z==2){
		if(x+1<=n&&mp[x+1][y][z]==0)                    mp[x+1][y][z]=ans+1;
		if(x+2<=n&&mp[x+2][y][z]==0&&mp[x+1][y][z]!=-1) mp[x+2][y][z]=ans+1;
		if(x+3<=n&&mp[x+3][y][z]==0&&mp[x+1][y][z]!=-1) mp[x+3][y][z]=ans+1;
	}
	if(z==1){
		if(y-1>=0&&mp[x][y-1][z]==0)                    mp[x][y-1][z]=ans+1;
		if(y-2>=0&&mp[x][y-2][z]==0&&mp[x][y-1][z]!=-1) mp[x][y-2][z]=ans+1;
		if(y-3>=0&&mp[x][y-3][z]==0&&mp[x][y-1][z]!=-1) mp[x][y-3][z]=ans+1;
	}
	if(z==3){
		if(y+1<=m&&mp[x][y+1][z]==0)                    mp[x][y+1][z]=ans+1;
		if(y+2<=m&&mp[x][y+2][z]==0&&mp[x][y+1][z]!=-1) mp[x][y+2][z]=ans+1;
		if(y+3<=m&&mp[x][y+3][z]==0&&mp[x][y+1][z]!=-1) mp[x][y+3][z]=ans+1;
	}
	if(mp[x][y][(z+1+4)%4]==0) mp[x][y][(z+1+4)%4]=ans+1;//转向
	if(mp[x][y][(z-1+4)%4]==0) mp[x][y][(z-1+4)%4]=ans+1;
}
int bfs(){
	for(int i=0;i<4;i++){//判断是否找到终点
		if(mp[ex][ey][i]!=0){
			return mp[ex][ey][i];
		}
	}
    int a=0;
	ans++;
	for(int i=0;i<=n;i++){//找下一步所有可能的情况并记录最小步数
		for(int j=0;j<=m;j++){
			for(int k=0;k<4;k++){
				if(ans==mp[i][j][k]){
                    bl(i,j,k);
                    a=1;
                }
			}
		}
	}
    if(a==0)//从起点遍历完都没有找到终点
    return 0;
	bfs();//这是一个循环,不断的找下步可能的情况,直到找到终点或者遍历完
}
int main() {
	int a;
	while(~scanf("%d %d", &n, &m)&&(n||m)){
		memset(mp,0,sizeof(mp));
		ans=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				scanf("%d", &a);
				if(a==0) for(int k=0;k<4;k++){//这里是将点转化为路线
                    if(mp[i][j][k]==0)     mp[i][j][k]=0;
                    if(mp[i+1][j][k]==0)   mp[i+1][j][k]=0;
                    if(mp[i+1][j+1][k]==0) mp[i+1][j+1][k]=0;
                    if(mp[i][j+1][k]==0)   mp[i][j+1][k]=0;
                }
				else     for(int k=0;k<4;k++) mp[i][j][k]=-1,mp[i+1][j][k]=-1,mp[i+1][j+1][k]=-1,mp[i][j+1][k]=-1;
			}
		}
		scanf("%d %d %d %d %s", &bx, &by, &ex, &ey, bf);
		for(int i=0;i<=m;i++){//处理边界,机器人太胖,不能走边界
			for(int k=0;k<4;k++){
				mp[0][i][k]=-1;
				mp[n][i][k]=-1;
			}
		}
		for(int i=0;i<=n;i++){
			for(int k=0;k<4;k++){
				mp[i][0][k]=-1;
				mp[i][m][k]=-1;
			}
		}
		if(bf[0]=='n') bs=0;//初始方向
		if(bf[0]=='s') bs=2;
		if(bf[0]=='w') bs=1;
		if(bf[0]=='e') bs=3;
		mp[bx][by][bs]=1;
		if(mp[ex][ey][0]==-1||mp[bx][by][0]==-1)//有可能开始或终点处有障碍物
		printf("-1\n");
		else
		printf("%d\n", bfs()-1);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44410512/article/details/86738706