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

  1. 9 10
  2. 0 0 0 0 0 0 1 0 0 0
  3. 0 0 0 0 0 0 0 0 1 0
  4. 0 0 0 1 0 0 0 0 0 0
  5. 0 0 1 0 0 0 0 0 0 0
  6. 0 0 0 0 0 0 1 0 0 0
  7. 0 0 0 0 0 1 0 0 0 0
  8. 0 0 0 1 1 0 0 0 0 0
  9. 0 0 0 0 0 0 0 0 0 0
  10. 1 0 0 0 0 0 0 0 1 0
  11. 7 2 2 7 south
  12. 0 0

Sample Output

  1. 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个方向

  1. 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[][],vis[][][],d;
  7. char b[];
  8. struct node {
  9. int x,y,step,fang;
  10. };
  11. int dx[]= {-,,,};
  12. int dy[]= {,,,-};
  13. int check(int x, int y) {
  14. if (x < || x >=n || y < || y >= m || tu[x][y] || tu[x+][y] || tu[x][y+] || tu[x+][y+]) return ;
  15. return ;
  16. }
  17. int bfs() {
  18. queue<node>q;
  19. node a;
  20. a.x=x1,a.y=y1,a.fang=d,a.step=;
  21. vis[a.x][a.y][d]=;
  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 = ; i < ; 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] = ;
  36. node cnt;
  37. cnt.x = nx, cnt.y = ny;
  38. cnt.fang =a.fang, cnt.step = a.step+;
  39. q.push(cnt);
  40. }
  41. }
  42. for (int i = ; i < ; i++) {
  43. if (max(a.fang, i)-min(a.fang, i) == )
  44. continue;
  45. if (vis[a.x][a.y][i])
  46. continue;
  47. vis[a.x][a.y][i] = ;
  48. node cnt = a;
  49. cnt.fang = i;
  50. cnt.step = a.step+;
  51. q.push(cnt);
  52. }
  53. }
  54. return -;
  55. }
  56. int main() {
  57. while (scanf("%d%d", &n, &m) != EOF) {
  58. if (n== && m== ) break;
  59. for (int i = ; i <= n; i++)
  60. for (int j = ; j <= m; j++)
  61. scanf("%d", &tu[i][j]);
  62. memset(vis, , sizeof(vis));
  63. scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  64. scanf("%s", b);
  65. if (b[]=='n') d=;
  66. if (b[]=='e') d=;
  67. if (b[]=='s') d=;
  68. if (b[]=='w') d=;
  69. printf("%d\n", bfs());
  70. }
  71. return ;
  72. }

Robot POJ - 1376的更多相关文章

  1. POJ 1376 Robot

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7866   Accepted: 2586 Description The R ...

  2. Cleaning Robot POJ - 2688

    题目链接:https://vjudge.net/problem/POJ-2688 题意:在一个地面上,有一个扫地机器人,有一些障碍物,有一些脏的地砖,问,机器热能不能清扫所有的地砖, (机器人不能越过 ...

  3. 模拟 POJ 1573 Robot Motion

    题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...

  4. POJ 1573 Robot Motion(BFS)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12856   Accepted: 6240 Des ...

  5. Poj OpenJudge 百练 1573 Robot Motion

    1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...

  6. POJ 1573 Robot Motion(模拟)

    题目代号:POJ 1573 题目链接:http://poj.org/problem?id=1573 Language: Default Robot Motion Time Limit: 1000MS ...

  7. Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏

    Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11262 Accepted: 5482 Descrip ...

  8. POJ 1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12978   Accepted: 6290 Des ...

  9. poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】

                                                                                                         ...

随机推荐

  1. 332. Reconstruct Itinerary

    class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...

  2. json模块、os模块

    一.eval模拟序列化操作 1.序列化 内存中的数据-------->转成一种中间格式(字符串)---------->存到文件中 dic={'name':'egon','age':18} ...

  3. ADB工具的安装

    1.Windows ADB工具下载地址: https://developer.android.google.cn/studio/releases/platform-tools ADB工具官网教程: h ...

  4. java 获取图片大小(尺寸)

    1,获取本地图片大小(尺寸) File picture=new File(strSrc);BufferedImage sourceImg=ImageIO.read(new FileInputStrea ...

  5. HBase配置和使用

    参考官方文档 整体实现框架 图1 以下几个为组成部件 21892 HMaster 22028 HRegionServer 21553 QuorumPeerMain 2366 NameNode 2539 ...

  6. 利尔达NB-IOT模块对接移动onenet平台步骤

    1. 首先登陆浙江移动onenet网站,http://openiot.zj.chinamobile.com/,进入右上角的开发者中心,然后才能看到创建产品 2. 填写产品的信息,其他信息按照个人实际填 ...

  7. SGU刷题之路,开始了

    0. 关于SGU的简介 SGU的网址是:acm.sgu.ru 向不了解的同学介绍一下SGU这个题库: 1. 题目难度很高,题目大多很经典. 2. 其数据范围很小,时间和空间要求也都很小,同时很精确.甚 ...

  8. iOS-Debug Symbol(调试符号)

    Debug Symbol(调试符号) 编译警告 从svn下载下来的文件,到处都是编译警告,看着不爽,找下原因,没想到还是一条大鱼 warning: (i386) /UsersLibrary/Devel ...

  9. 树莓派i2c功能

    默认i2c是关闭的,用raspi-config 命令,会弹出一个配置框图 选择enable i2c就可以了 reboot之后 没有在/dev/目录下发现i2c-x的设备,这个时候需要做以下操作 1.添 ...

  10. Vm-Ubuntu下配置Qt开发环境

    在昨天的Ubuntu换降下,安装Qt发现编译的时候是缺少opengl的 奈何找了好多方式都无法安装opengl 今天看到另一位大神写的,才发下自己找的还是有问题 大神帖子网址:http://blog. ...