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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/4113e033cc1d7434efca9f56a572cc69.jpeg)
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
我感觉这很长道题有点坑啊,改了很长时间的bug,改出来了,我想说的是,机器人在轨道上行走,在1*1的正方形内放的障碍物
所以就限制了在0 1 相邻的轨道上机器人是不能行驶的,同样的道理,我们可以猜到机器人也不能在边界行驶,除此之外,输入
中的起始点和目标的位子,也应该弄清楚1或0应该填在哪个方块中,这道题需要好好的看看图来理解,
参考代码
#include<cstdio> #include<string.h> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> #include <numeric>//常用数字操作 一般和algorithm搭配使用 #include <functional>//STL定义运算函数(代替运算符) using namespace std; typedef long long ll; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 1000000000; const int maxn =1000008; struct node { int x,y; int tim,pos; }; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; int maze[110][110]; int b1,b2,a1,a2,sd,m,n; int vis[110][110][4]; int get_str(char ss[]) { if (!strcmp(ss, "north")) // 北 return 0; if (!strcmp(ss, "east")) //dong return 1; if (!strcmp(ss, "south")) //nan return 2; return 3; //xi } int check(int x,int y ) { if(x<1||x>=m||y<1||y>=n) return 0; if (maze[x][y] || maze[x+1][y] || maze[x][y+1] || maze[x+1][y+1]) return 0; return 1; } int bfs() { queue<node>q; node a; a.x=a1; a.y=a2; a.pos=sd; a.tim=0; q.push(a); vis[a1][a2][sd]=1; while(!q.empty()) { node b=q.front(); q.pop(); if(b.x==b1&&b.y==b2) return b.tim; int nx=b.x; int ny=b.y; for(int i=1; i<4; i++) { nx+=dx[b.pos]; ny+=dy[b.pos]; if(!check(nx,ny)) break; if(!vis[nx][ny][b.pos]) { vis[nx][ny][b.pos]=1; node cnt; cnt.x=nx; cnt.y=ny; cnt.pos=b.pos; cnt.tim=b.tim+1; q.push(cnt); } } for(int i=0; i<4; i++) { if(max(b.pos,i)-min(b.pos,i)==2) continue; if(vis[b.x][b.y][i]) continue; vis[b.x][b.y][i]=1; node cnt=b; cnt.pos=i; cnt.tim=b.tim+1; q.push(cnt); } } return -1; } int main() { while(~scanf("%d%d",&m,&n)&&m,n) { int i,j; memset(vis,0,sizeof(vis)); for(i=1; i<=m; i++) for(j=1; j<=n; j++) scanf("%d",&maze[i][j]); memset(vis,0,sizeof(vis)); scanf("%d%d%d%d",&a1,&a2,&b1,&b2); char direction[30]; scanf("%s",direction); sd=get_str(direction); if(a1==b1&&a2==b2) { printf("0\n"); continue; } if(!check(b1,b2)) { printf("-1\n"); continue; } int ans=bfs(); printf("%d\n",ans); } return 0; }