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的更多相关文章
随机推荐
- Python 爬取所有51VOA网站的Learn a words文本及mp3音频
Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...
- 利用mysql_multi来管理多实例:
mysql_multi的现实意义: 1:随着连接数上升,性能会下降,通过多实例来分流大量连接来提高性能. 2:做资源隔离 3:分库分表 mysql_multi是官方管理多实例的一个脚本,利用perl语 ...
- 遇到bug怎么办
最近第一个完整的项目的第一期快完成了.期间,我怀疑过无数次人生,给难兄难弟辣椒相互吐槽过.被我师父点播后觉得人和人差距怎么可以这么大数次. 终于!基本功能实现了. 今天不总结具体问题了,说一下调试过程 ...
- BZOJ3000 Big Number
由Stirling公式: $$n! \approx \sqrt{2 \pi n} (\frac{n}{e})^n$$ 故:$$\begin{align} ans &= log_k n! + 1 ...
- Android-内存泄漏
单例造成的内存泄漏 单例模式非常受开发者的喜爱,不过使用的不恰当的话也会造成内存泄漏,由于单例的静态特性使得单例的生命周期和应用的生命周期一样长,这就说明了如果一个对象已经不需要使用了,而单例对象 ...
- vuejs开发组件分享之H5图片上传、压缩及拍照旋转的问题处理
一.前言 三年.net开发转前端已经四个月了,前端主要用webpack+vue,由于后端转过来的,前端不够系统,希望分享下开发心得与园友一起学习. 图片的上传之前都是用的插件(ajaxupload), ...
- 总结4点对学习Linux有帮助的建议(纯干货)
学习需要足够的毅力和耐心 有些人把Linux运维看作一项冗长而枯燥的工作:有些人把linux运维看作一项得力的工具.如果是前者建议还是改变一下认识,不然不建议入门这行.毕竟linux运维工作是对人的毅 ...
- hammer.js实现背景图手势缩放调整位置
<!DOCTYPE html> <html> <head> <script> function getxy(e){ var a=new Array() ...
- Android中的颜色设置
1.在android中经常看到设置的颜色为八位的十六进制的颜色值,例如 public static final class color { public static final int lightb ...
- 一个简单的游戏开发框架(五.对象Object)
前面提到我们把行为Action从对象Object中分离了出来,用各种不同的行为组合出对象的功能.大家都知道,面向对象的一个类,就是数据和操作的集合.操作(行为)被分离出来了,数据怎么办呢?操作依赖的数 ...