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的更多相关文章
随机推荐
- [转]EntityFramework状态变化AutoDetectChangesEnabled与SaveChanged参数说明
一.约定OnModelCreating 有一些限制需要注意,例如:1.表名不支持使用标签进行标注2.最小长度在 OnModelCreating 中不支持3.正则表达式在 OnModelCreating ...
- 怎么把Windows主机上的目录共享到Ubuntu上
使用Oracle VM VirtualBox在Windows主机上创建了一台Ubuntu虚拟机,怎么把宿主机上的目录共享到Ubuntu上,可使用以下方法: eg.把Windows主机上D盘里的test ...
- ubuntu下安装python各类运维用模块(以后补充用途)
环境:ubuntu 16.04LTS,python3,python2 已安装:pip3,pip2 注:基于Python自动化运维这本书上介绍的各模块而来 1.python-rrdtool(just f ...
- Handle机制的原理
Android提供了Handle和Looper来满足线程间的通信.Handle先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(Message Exchange). 1.Looper: ...
- AseBulkCopy 若干问题的解决方法
场景:数据库 Ase 15.0, Ado.net客户端15.7 Sybase.AdoNet4.AseClient 错误1:使用Transaction报错 错误2: 存储过程"sp_drv_ ...
- Asp.net页面引用SAP IQ 16 iAnywhere.Data.SQLAnywhere.V4.0.dll报错,语言文件没找到
参考http://sqlanywhere-forum.sap.com/questions/20420/saconnection-threw-an-exception-cannot-find-the-l ...
- Python 的 pyinotify 模块 监控文件夹和文件的变动
官方参考: https://github.com/seb-m/pyinotify/wiki/Events-types https://github.com/seb-m/pyinotify/wiki/I ...
- Bootstrap<基础十三> 按钮组
按钮组允许多个按钮被堆叠在同一行上.当你想要把按钮对齐在一起时,这就显得非常有用.你可以通过Bootstrap 按钮(Button) 插件 添加可选的 JavaScript 单选框和复选框样式行为. ...
- IIC总线解析
IIC简介: IIC 即Inter-Integrated Circuit(集成电路总线),这种总线类型是由飞利浦半导体公司在八十年代初设计出来的,主要是用来连接整体电路(ICS) ,IIC是一种多向控 ...
- Three.Js学习第一天
因为工作需求,最近接触到了ThreeJs库,国内学习文档的确少,所以在这里写下bolgs记录学习史,并且给后面学习的人尽一份微博之力. 3D场景依靠WebGL技术.目前支持比较好的浏览器,谷歌.火狐. ...