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

题目大意:
就是给机器人编一个行走的代码,求最短的时间,其实就是最短路。
这个有点不同,第一个是有方向,第二个是他是一个圆,不是点有面积。
思路:
用bfs,先处理这三种步法,走一步,走两步,走三步,
处理完之后再处理方向。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int n,m,x1,y1,x2,y2,d;
char b[20];
int tu[105][105];
bool vis[105][105][4];
int dx[4]={-1,0,1,0};//这里要注意东南西北相对应
int dy[4]={0,1,0,-1};
struct node
{
int x,y,step,fang;
};
int check(int x,int y)
{
if(x<1||y<1||x>=n||y>=m||tu[x][y]||tu[x+1][y]||tu[x][y+1]||tu[x+1][y+1]) return 0;//因为是左上角,所以x不能等于n,同理y也不能等于m。
return 1;
}
int bfs()
{
queue<node>que;
node a;
a.x=x1;
a.y=y1;
a.fang=d;
a.step=0;
que.push(a);
vis[a.x][a.y][a.fang]=1;
while(!que.empty())
{ a=que.front();que.pop();
if(a.x==x2&&a.y==y2) return a.step;
node ex=a;//注意这个ex要定义在外面,因为ex会累加,累加一次说明走一步,两次走两步,以此类推
for(int i=1;i<4;i++)//第一个for来走步数,三种情况,所以三次循环
{
ex.x+=dx[a.fang];
ex.y+=dy[a.fang];
if(!check(ex.x,ex.y)) break;
if(!vis[ex.x][ex.y][a.fang])
{
ex.fang=a.fang;
vis[ex.x][ex.y][ex.fang]=1;
ex.step=a.step+1;
que.push(ex);
}
}
for(int i=0;i<4;i++)//第二个for来走方向,四种情况,四次循环
{
if(max(a.fang,i)-min(a.fang,i)==2) continue;
if(vis[a.x][a.y][i]) continue;
vis[a.x][a.y][i]=1;
node ex=a;
ex.fang=i;
ex.step=a.step+1;
que.push(ex);
}
}
return -1;
} int main()
{
while(scanf("%d%d",&n,&m)&&(n+m))
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
scanf("%d",&tu[i][j]);
}
memset(vis,0,sizeof(vis));
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
scanf("%s",b);
if(b[0]=='n') d=0;
if(b[0]=='e') d=1;
if(b[0]=='s') d=2;
if(b[0]=='w') d=3;
printf("%d\n",bfs());
}
return 0;
}

  

寒假训练——搜索——C - Robot的更多相关文章

  1. 寒假训练——搜索 K - Cycle

    A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...

  2. 寒假训练——搜索 E - Bloxorz I

    Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...

  3. 寒假训练——搜索 G - Xor-Paths

    There is a rectangular grid of size n×mn×m . Each cell has a number written on it; the number on the ...

  4. J - Abbott's Revenge 搜索 寒假训练

    题目 题目大意:这个题目就是大小不超过9*9的迷宫,给你起点终点和起点的方向,让你进行移动移动特别之处是不一定上下左右都可以,只有根据方向确定可以走的方向.思路:需要写一个读入函数,这个需要读入起点, ...

  5. 寒假训练 A - A Knight's Journey 搜索

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  6. 算法专题训练 搜索a-T3 Ni骑士(ni)

    搞了半天八数码弄不出来就只好来打题解  这道题是在搜索a碰到的(链接: http://pan.baidu.com/s/1jG9rQsQ ) 感觉题目最大亮点就是这英文简写"ni", ...

  7. HRBUST - 2347 - 递归画图 - vj大一上寒假训练2.11

    其他题可由本题变形得到. 思路:利用坐标dfs搜索. 注意:1,初始化.2,坐标实时更新(x,y) 代码: #include<iostream> #include<cstdio> ...

  8. 寒假集训——搜索 B - Sudoku

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream&g ...

  9. 寒假集训——搜索 D - Cubes for Masha

    #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h&g ...

随机推荐

  1. sql server 查询时会锁表吗?

    sql server在执行查询语句时会锁表.在锁表期间禁止增删改操作. 如果不想锁表,那就再表名或别名后面加上WITH(NOLOCK) 如下所示:

  2. [日常] HTTP的媒体类型

    HTTP的媒体类型 1.MIME类型的数据格式标签(MultIpurpose Internet Mail Extension) 2.最初用于电子邮件系统之间搬移,多用途互联网邮件扩展 3.MIME类型 ...

  3. 2.QT-窗口组件(QWidget),QT坐标系统,初探消息处理(信号与槽)

    本章主要内容如下: 1) 窗口组件(QWidget) 2) QT坐标系统 3) 消息处理(信号与槽) 窗口组件(QWidget) 介绍 Qt以组件对象的方式构建图形用户界面 Qt中没有父组件的顶级组件 ...

  4. springMVC_04controller四种配置总结

    一.通过url对应bean,加粗部分为必须有的 <bean class=" org.springframework.web.servlet.handler.BeanNameUrlHan ...

  5. Java_Comparable,Comparator两接口区别

    Comparable和Comparator的区别 根本区别 1.Comparable是一个内比较器,Comparator是一个外比较器 封装的包不同 java.util.Comparator java ...

  6. Java集合类源码解析:AbstractMap

    目录 引言 源码解析 抽象函数entrySet() 两个集合视图 操作方法 两个子类 参考: 引言 今天学习一个Java集合的一个抽象类 AbstractMap ,AbstractMap 是Map接口 ...

  7. C#设计模式之五原型模式(Prototype Pattern)【创建型】

    一.引言 在开始今天的文章之前先说明一点,欢迎大家来指正.很多人说原型设计模式会节省机器内存,他们说是拷贝出来的对象,这些对象其实都是原型的复制,不会使用内存.我认为这是不对的,因为拷贝出来的每一个对 ...

  8. 浅谈pc和移动端的响应式

    身为一个前端攻城狮,是不是经常遇到各种各样的响应式问题?下面我们来说一下: 1.响应式跟自适应有什么区别? 有些人可能还不知道响应式跟自适应的区别,甚至认为他们是同一个东西,其实不是的. 自适应是最早 ...

  9. iphone怎么投屏到电脑屏幕上

    随着苹果手机的更显换代,苹果手机的功能越来越强大,其中iphone手机更新了airplay镜像功能,所以想要手机投屏电脑的小伙伴就更加方便了,但是iphone怎么投屏到电脑呢?大家不用着急,下面即将为 ...

  10. mybatis的三种批量插入以及次效率比较

    1.表结构 CREATE TABLE `t_user` ( `id` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT '主键', `name` varc ...