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
Output
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米,它完全被这个网格覆盖。最接近商店一侧的跑道距离只有一米。机器人具有直径等于1.6米的圆形形状。轨道穿过机器人的中心。机器人总是面向北,南,西或东。轨道位于南北和东西方向。机器人只能朝它面对的方向移动。它的方向可以在每个轨道交叉处改变。最初机器人站在轨道交叉处。商店中的障碍物是由地面上的1m×1m的碎片组成的。每个障碍物都在由轨道形成的1×1方格内。机器人的运动由两个命令控制。这些命令是GO和TURN。
GO命令在{1,2,3}中有一个整数参数n。接收到这个命令后,机器人按照它所面对的方向移动n米。
TURN命令有一个参数可以是左或右。接收到该命令后,机器人按照参数指示的方向将其方向改变90°。
每个命令的执行持续一秒钟。
帮助RMI的研究人员编写一个程序,该程序将确定机器人从一个给定的起点移动到一个给定的目的地的最短时间。
这个机器人有两种指令走向当前放下走1-3步或者是向左或向右90度转向(不能向后转)。
注意构图,机器人在线上走动,而给的数据是一个一个的方块格子,
标记数组为vis[100][100][4],4代表4个方向
if (!check(nx,ny)) break;这个的break说明如果你前方已经走不通了 那么你就不能再往前走,于是break
这题的方向特别容易卡 要注意
int dx[4]= {-1,0,1,0}; int dy[4]= {0,1,0,-1};
if (b[0]=='n') d=0;
if (b[0]=='e') d=1;
if (b[0]=='s') d=2;
if (b[0]=='w') d=3;
这些都是一一对应关系
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 using namespace std; 6 int n,m,x1,y1,x2,y2,tu[105][105],vis[105][105][4],d; 7 char b[20]; 8 struct node { 9 int x,y,step,fang; 10 }; 11 int dx[4]= {-1,0,1,0}; 12 int dy[4]= {0,1,0,-1}; 13 int check(int x, int y) { 14 if (x < 1 || x >=n || y < 1 || y >= m || tu[x][y] || tu[x+1][y] || tu[x][y+1] || tu[x+1][y+1]) return 0; 15 return 1; 16 } 17 int bfs() { 18 queue<node>q; 19 node a; 20 a.x=x1,a.y=y1,a.fang=d,a.step=0; 21 vis[a.x][a.y][d]=1; 22 q.push(a); 23 while(!q.empty()) { 24 a=q.front(); 25 q.pop(); 26 if (a.x==x2 && a.y==y2) return a.step; 27 int nx=a.x; 28 int ny=a.y; 29 for (int i = 1; i < 4; i++) { 30 nx += dx[a.fang]; 31 ny += dy[a.fang]; 32 if (!check(nx, ny)) 33 break; 34 if (!vis[nx][ny][a.fang]) { 35 vis[nx][ny][a.fang] = 1; 36 node cnt; 37 cnt.x = nx, cnt.y = ny; 38 cnt.fang =a.fang, cnt.step = a.step+1; 39 q.push(cnt); 40 } 41 } 42 for (int i = 0; i < 4; i++) { 43 if (max(a.fang, i)-min(a.fang, i) == 2) 44 continue; 45 if (vis[a.x][a.y][i]) 46 continue; 47 vis[a.x][a.y][i] = 1; 48 node cnt = a; 49 cnt.fang = i; 50 cnt.step = a.step+1; 51 q.push(cnt); 52 } 53 } 54 return -1; 55 } 56 int main() { 57 while (scanf("%d%d", &n, &m) != EOF) { 58 if (n==0 && m==0 ) break; 59 for (int i = 1; i <= n; i++) 60 for (int j = 1; j <= m; j++) 61 scanf("%d", &tu[i][j]); 62 memset(vis, 0, sizeof(vis)); 63 scanf("%d%d%d%d",&x1,&y1,&x2,&y2); 64 scanf("%s", b); 65 if (b[0]=='n') d=0; 66 if (b[0]=='e') d=1; 67 if (b[0]=='s') d=2; 68 if (b[0]=='w') d=3; 69 printf("%d ", bfs()); 70 } 71 return 0; 72 }