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

题解:

具有两种操作的广搜,和普通的广搜相比有些不同,普通的广搜一般是一种操作,上下左右,
这一题加上了转向。同时还要注意的是这个机器人本身的大小,这个也就确定了,边界上它是走不了的。

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define ll long long
using namespace std;
const int MAX = ;
const int INF = <<;
const int dir[][]{{-,},{,},{,},{,-}};
struct Nod{
int x,y,t,d;
bool operator < (const Nod &a) const{
return t > a.t;
}
};
int g[MAX][MAX], n, m, mark[MAX][MAX][];
int bfs(int bx, int by, int ex, int ey, int f) {
priority_queue<Nod> que;
Nod nod;
mark[bx][by][f] = ;
que.push((Nod){bx,by,,f});
while(!que.empty()) {
nod = que.top();
que.pop();
int x = nod.x, y = nod.y;
if(x == ex && y == ey) return nod.t;
int d = nod.d;
if(mark[x][y][(d+)%] == - || mark[x][y][(d+)%] > nod.t + ){
mark[x][y][(d+)%] = nod.t+;
que.push((Nod){x,y,nod.t+,(d+)%});
}
if(mark[x][y][(d-+)%] == - || mark[x][y][(d-+)%] > nod.t + ){
mark[x][y][(d-+)%] = nod.t + ;
que.push((Nod){x,y,nod.t+,(d-+)%});
}
int nx = x, ny = y;
for(int i = ; i <= ; i ++) {
nx += dir[d][];
ny += dir[d][];
if(nx <= || nx >= m || ny <= || ny >= n)break;
if(g[nx][ny] == || g[nx-][ny] == || g[nx][ny-] == || g[nx-][ny-] == ) break;
if(mark[nx][ny][d] == - || mark[nx][ny][d] > nod.t+){
mark[nx][ny][d] = nod.t+;
que.push((Nod){nx,ny,nod.t+,d});
}
}
}
return -;
}
int main() {
int bx, by, ex, ey, d, ans;
char str[];
while(scanf("%d%d",&m,&n)&&(n+m)) {
for(int i = ; i < m; i ++) {
for(int j = ; j < n; j ++) {
scanf("%d",&g[i][j]);
}
}
scanf("%d %d %d %d %s",&bx,&by,&ex,&ey,str);
if(str[] == 'n') d = ;
else if(str[] == 'e') d = ;
else if(str[] == 's') d = ;
else if(str[] == 'w') d = ;
memset(mark,-,sizeof(mark));
ans = bfs(bx,by,ex,ey,d);
printf("%d\n",ans);
}
return ;
}

ZOJ1310-Robot (BFS)的更多相关文章

  1. UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)

    题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...

  2. poj 2688 Cleaning Robot bfs+dfs

    题目链接 首先bfs, 求出两两之间的距离, 然后dfs就可以. #include <iostream> #include <cstdio> #include <algo ...

  3. Uva 1600 Patrol Robot (BFS 最短路)

    这道题运用的知识点是求最短路的算法.一种方法是利用BFS来求最短路. 需要注意的是,我们要用一个三维数组来表示此状态是否访问过,而不是三维数组.因为相同的坐标可以通过不同的穿墙方式到达. #inclu ...

  4. Japan 2005 Domestic Cleaning Robot /// BFS 状压 二进制位运算 结构体内构造函数 oj22912

    题目大意: 输入w h,接下来输入h行w列的图 ' . ':干净的点:  ' * ' :垃圾:  ' x ' : 墙:  ' o ' : 初始位置: 输出 清理掉所有垃圾的最短路径长度 无则输出-1 ...

  5. UVa 1600 Patrol Robot(BFS)

    题意: 给定一个n*m的图, 有一个机器人需要从左上角(1,1)到右下角(n,m), 网格中一些格子是空地, 一些格子是障碍, 机器人每次能走4个方向, 但不能连续穿越k(0<= k <= ...

  6. POJ 1573 Robot Motion(BFS)

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

  7. UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)

    UVA 1600 Patrol Robot   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   ...

  8. C. Robot(BFS)

    C. Robot Time Limit: 3000ms Case Time Limit: 3000ms Memory Limit: 262144KB 64-bit integer IO format: ...

  9. UVA12569-Planning mobile robot on Tree (EASY Version)(BFS+状态压缩)

    Problem UVA12569-Planning mobile robot on Tree (EASY Version) Accept:138  Submit:686 Time Limit: 300 ...

  10. UVA1600-Patrol Robot(BFS进阶)

    Problem UVA1600-Patrol Robot Accept:529  Submit:4330 Time Limit: 3000 mSec Problem Description A rob ...

随机推荐

  1. pikachu-XXE(xml external entity-injection)

    部分图片来自于网络,如有侵权,请联系我及时删除~ 一.XXE的概念 1.1 什么是xml xml是一种可拓展的标记语言,可以用来存储数据,例如:我们经常看到一些.xml的文件:它还可以用来传输数据,我 ...

  2. html基本介绍,了解html与css,html语法和结构

    一般来说,制作自己第一个网页通常书写的文字是"hello world!你好,全世界",代码如下展示: <!DOCTYPE html> <html lang=&qu ...

  3. 聊聊GIS中的坐标系|再版 详细定义、计算及高程系统

    本篇讲坐标系统的详细定义,有关坐标系的变换公式,以及简单说说高程坐标系统. 本文约6000字,阅读时间建议45分钟.硬内容比较多,如有疏漏错误请指出,建议有兴趣的朋友进一步阅读. 作者:博客园/B站/ ...

  4. Maven 阿里云仓库地址

    https://maven.aliyun.com/mvn/view 一般使用聚合仓库(group),path是仓库地址.可点击右上角“使用指南”: 附   目前阿里云仓库的地址 https://mav ...

  5. SpringBoot整合NoSql--(四)Session共享

    简介: 正常情况下,HttpSession是通过Servlet 容器创建并进行管理的,创建成功之后都是保存在内存中.如果开发者需要对项目进行横向扩展搭建集群,那么可以利用一些硬件或者软件工具来做负载均 ...

  6. 应用场景不同,是无代码和低代码的最大区别 ZT

    随着媒体对低代码.无代码等先进技术的持续关注,我们发现大多数人都听说过低代码开发和无代码开发这两个概念,但是对两者之间的区别其实并不清楚.事实上,低代码开发和无代码开发之间存在着很多非常显著的差异,如 ...

  7. 破解“低代码”的4大误区,拥抱低门槛高效率的软件开发新选择 ZT

    最近,每个人似乎都在谈论“低代码”.以美国的Outsystems.Kinvey,以及国内的活字格为代表的低代码开发平台,正在风靡整个IT世界.毕竟,能够以最少的编码快速开发应用的想法本身就很吸引人.但 ...

  8. 剑指offer-面试题56_1-数组中只出现一次的两个数字-位运算

    /* 题目: 求数组A中只出现一次的数字,该数组中有2个数字a.b仅出现一次,其余均出现两次 */ /* 思路: 两个相同的数字异或为0. 遍历数组,得到数组中各数字异或后的结果x,结果x=a^b. ...

  9. Page Object设计模式(二)——poium测试库

    一.简介 poium是一个基于Selenium/appium的Page Object测试库,最大的特点是简化了Page层元素的定义. 项目地址:https://github.com/SeldomQA/ ...

  10. bitset刷题记录

    大佬的bitset用法小结 https://www.cnblogs.com/zwfymqz/p/8696631.html BZOJ3687简单题 题意:求子集的算术和的异或和,子集大小为n(n< ...