Robot_bfs
Description
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;
【思路】
1、2种命令,一种是向现在所面向的方向走1-3步,另外一种是向左或向右90度转向(不能向后转)。
2、不允许出界和到达障碍物。
3、对搜索进行去重操作的时候需要记录所处位置的方向,因为这个题存在方向的问题,需要注意下,可以设置数组为vis用三维,第三维代表4个方向。
4、存在起点等于终点的情况,还有就是终点是无法到达的。
#include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=+;
struct node
{
int x,y;
int t;
int pos;
};
node cur;
int n,m,ex,ey,mp[N][N],ans;
bool vis[N][N][];
int dirx[]={-,,,},diry[]={,,,-};
int getPos(char *str)
{
if(!strcmp(str,"north")) return ;
if(!strcmp(str,"east")) return ;
if(!strcmp(str,"south")) return ;
return ;
}
bool isgo(int x,int y)
{
if(x<||x>=n||y<||y>=m)
return false;
if(mp[x][y]||mp[x+][y]||mp[x][y+]||mp[x+][y+])
return false ;
return true;
}
void bfs()
{
queue<node>qu;
qu.push(cur);
vis[cur.x][cur.y][cur.pos]=;
int ans=-;
if(cur.x==ex&&cur.y==ey)//起点相同
{
printf("0\n");
return ;
}
if(!isgo(ex,ey))//终点出界
{
printf("-1\n");
return ;
}
while(!qu.empty())
{
node now=qu.front();
qu.pop();
int xx=now.x;
int yy=now.y;
for(int i=;i<=;i++)
{
xx+=dirx[now.pos];
yy+=diry[now.pos];
if(!isgo(xx,yy)) break ;
if(xx==ex&&yy==ey)
{
ans=now.t+;
break;
}
if(!vis[xx][yy][now.pos])
{
node tmp;
tmp.x=xx;
tmp.y=yy;
tmp.t=now.t+;
tmp.pos=now.pos;
vis[xx][yy][tmp.pos]=;
qu.push(tmp);
}
}
for(int i=;i<;i++)
{
if(max(now.pos,i)-min(now.pos,i)==)
continue;
if(vis[now.x][now.y][i]) continue;
node next=now;
next.t=now.t+;
next.pos=i;
vis[next.x][next.y][next.pos]=;
qu.push(next);
}
if(ans!=-) break;
}
if(ans!=-) printf("%d\n",ans);
else printf("-1\n");
return ;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
if(n==&&m==) break;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&mp[i][j]);
}
} memset(vis,,sizeof(vis));
scanf("%d%d",&cur.x,&cur.y);
scanf("%d%d",&ex,&ey);
char op[];
scanf("%s",op);
cur.pos=getPos(op);
cur.t=;
bfs(); }
return ;
}
Robot_bfs的更多相关文章
随机推荐
- JavaScript 面向对象(三) —— 高级篇
JavaScript 面向对象(一) —— 基础篇 JavaScript 面向对象(二) —— 案例篇 一.json方式的面向对象 首先要知道,js中出现的东西都能够放到json中.关于json数据格 ...
- jquery触屏幻灯片
一.前言 去年接触了移动Web开发,做了些手机端的网站及应用,还有些小的微信游戏和活动页面.每个项目里或多或少的都会有一些触屏事件等.其中有两个用到了jquery触屏幻灯片.刚开始的时候也在百度上搜索 ...
- sql脚本多服务器操作
--创建链接服务器exec sp_addlinkedserver 'srv_lnk','','SQLOLEDB','IP' exec sp_addlinkedsrvlogin 'srv_lnk','f ...
- float和double的精度
作者: jillzhang 联系方式:jillzhang@126.com 原网址:http://blog.csdn.net/wuna66320/article/details/1691734 1 范围 ...
- 【EasyUI】combotree和combobox模糊查询
这里说的模糊查询指在输入框输入,然后自动在下拉框中显示匹配结果,类似Google搜索提示 EasyUI库已经实现了combobox的查询过滤功能,但只能从头匹配,原因是EasyUI库的代码限制: fi ...
- 做App还是微信公众号,你该如何抉择?
我不够聪明,因为我经常出于好奇被自己提出的问题所困扰,于是乎就有些强迫症似的拼命去寻求答案——我只是想说服自己,让自己从困扰的谜团中清醒.坚定方向,进而能从容不迫的走下去... 最近在考虑一个问题:做 ...
- Dependency Scope
Dependency Scope <dependency>中还引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: * compile,缺 ...
- C++ 之引用
int argc ,char * argv[] - argument count & argument vector argc - 命令行参数个数,argv[]依次指向每一个命令行参数,其中a ...
- Liunx下安装jdk
Liunx下安装jdk 1.首先进入ROOT权限 命令 sudo su 输入密码进入 root 权限 2.看下当前liunx 是否存在jdk 环境 ,输入命令 javac,如果存在则会显示对应jd ...
- R 语言机器学习同步推进~
教材就是传说中的机器学习和R语言--中文版,大家可以去图书馆借来看看~~~,例子都是来自书上的 首先介绍一下KNN算法,KNN还好吧,说白了就是一个算距离的公式然后以统计的方式呈现出来,以二维平面为例 ...