F - Robot Motion 栈加BFS
A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are
N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)
For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.
Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.
You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
Input
Output
Sample Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
题目大意 : 看机器人是否可以按照给的指令走出迷宫,,如果可以的话,输出步数,如果发生循环的话输出走了多少步开始循环还要输出循环步数的大小
思路 : BFS+栈 利用栈先进去先出的性质,不用立刻删除栈中元素,,,简而言之就是保留路线,,用一个二维数组标记每一个走过方格,方便判断循环
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<stack>
using namespace std;
int n,m,l;
struct stu{
int a,b;
};
char arr[][];
int mark[][]={};
int arr1[][]={};
int d[][]={{-,},{,-},{,},{,}};//N,W,S,E
void bfs(int x,int y){
stack<stu>st;
st.push({x,y});
mark[x][y]=;
arr1[x][y]=;
while(st.size()){
int x=st.top().a;
int y=st.top().b;
int dx,dy,i;
if(arr[x][y]=='N') i=;
if(arr[x][y]=='W') i=;
if(arr[x][y]=='S') i=;
if(arr[x][y]=='E') i=;
dx=x+d[i][];
dy=y+d[i][];
// cout<<dx<<"ADS"<<dy<<endl;
// cout<<arr1[x][y]<<"--"<<endl;
if(dx<||dy<||dx>=n||dy>=m){//越界,说明走出了迷宫
printf("%d step(s) to exit\n",arr1[x][y]+);
break;
}
if(mark[dx][dy]==){//说明从dx dy处开始循环
// cout<<dx<<"--"<<dy<<endl;
int sum=;
st.pop();//第一个肯定位dx,dy
while(st.size()){
// cout<<que.top().a<<"__"<<que.top().b<<endl; if(st.top().a==dx&&st.top().b==dy){
break;
}
else {
sum++;
st.pop();
}
}
printf("%d step(s) before a loop of %d step(s)\n",st.size()-,sum+);
//剩余的部分包括重复的第一个元素 所以要减去1
//开头 删除了一个 然后又删除了一个, 还有一个没删除的即第一个重复元素 所以应加2
break;
}
arr1[dx][dy]=arr1[x][y]+;
mark[dx][dy]=;
que.push({dx,dy});
}
}
int main()
{
while(cin>>n>>m>>l){
if(n==&&m==&&l==) break;
memset(mark,,sizeof(mark));
for(int i=;i<n;i++){
scanf("%s",arr[i]);
}
bfs(,l-);
}
return ;
}
F - Robot Motion 栈加BFS的更多相关文章
- POJ 1573 Robot Motion(BFS)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
- hdoj 1035 Robot Motion
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu1035 Robot Motion (DFS)
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- poj1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12507 Accepted: 6070 Des ...
- Robot Motion(imitate)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11065 Accepted: 5378 Des ...
- 模拟 POJ 1573 Robot Motion
题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...
- Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11262 Accepted: 5482 Descrip ...
- POJ 1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12978 Accepted: 6290 Des ...
- Poj OpenJudge 百练 1573 Robot Motion
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...
随机推荐
- 最通俗易懂的 HashMap 源码分析解读
HashMap 作为最常用的集合类之一,有必要深入浅出的了解一下.这篇文章会深入到 HashMap 源码,刨析它的存储结构以及工作机制. 1. HashMap 的存储结构 HashMap 的数据存储结 ...
- vue cli3配置开发环境、测试环境、生产(线上)环境
cli3创建vue项目是精简版的少了build和config这2个文件,所以配置开发环境.测试环境.生产环境的话需要自己创建env文件. 需要注意2点: 1.cli2创建项目生成的config文件里的 ...
- 卷积的发展历程,原理和基于 TensorFlow 的实现
欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 稀疏交互 在生物学家休博尔和维瑟尔早期关于猫视觉皮层的研究中发现, ...
- 深度学习框架Keras与Pytorch对比
对于许多科学家.工程师和开发人员来说,TensorFlow是他们的第一个深度学习框架.TensorFlow 1.0于2017年2月发布,可以说,它对用户不太友好. 在过去的几年里,两个主要的深度学习库 ...
- 常见SQL语句和SQL基础知识
引自:http://blog.csdn.net/u012467492/article/details/46790205 SQL语句考察(一) 1.查询出每门课都大于80 分的学生姓名 name k ...
- JS烟花案例
html代码部分 <!DOCTYPE html> <!-- * @Descripttion: * @version: * @Author: 小小荧 * @Date: 2020-03- ...
- JS 剑指Offer(六) 用两个栈实现队列
题目:用两个栈实现队列,实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入整数和在队列头部删除整数,若队列中没有元素deleteHead返回-1 分析:在队列的尾部插入 ...
- Docker基础修炼1--Docker简介及快速入门体验
本文作为Docker基础系列第一篇文章,将详细阐述和分析三个问题:Docker是什么?为什么要用Docker?如何快速掌握Docker技术? 本系列文章中Docker的用法演示是基于CentOS7进行 ...
- css中(单冒号):after和(双冒号)::after的区别
:after是伪元素 .::after是伪类 什么是伪类? CSS伪类是添加到选择器的关键字,指定要选择的元素的特殊状态.例如,:hover可被用于在用户将鼠标悬停在按钮上时改变按钮的颜色. ::af ...
- 【linux】【tomcat】linux下定时重启tomcat 【CentOS 6.4】【CentOS 7.6】
本章内容以CentOS 6.4 和 CentOS 7.6 两个版本为例.[6和7的命令不同] 转载 :https://www.cnblogs.com/sxdcgaq8080/p/10730 ...