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 ...
随机推荐
- hdu3336 Counting the string kmp的next数组的应用
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3336/ 题意就是要求一个字符串的所有前缀在字符串中出现的次数之和,我们容易想到kmp中的next数组,next[ ...
- PhpStorm+XAMPP+Xdebug 集成开发和断点调试环境配置
0x01 Xdebug安装 参考:https://xdebug.org/docs/install cd xdebug-/ phpize sudo ./configure --enable-xdebug ...
- 逻辑回归和sigmoid函数分类
逻辑回归和sigmoid函数分类:容易欠拟合,分类精度不高,计算代价小,易于理解和实现 sigmoid函数与阶跃函数的区别在于:阶跃函数从0到1的跳跃在sigmoid函数中是一个逐渐的变化,而不是突变 ...
- ORA-01017的一种情况:sysdba可以登录,normal不可登录
在arcCatalog中创建完sde数据库之后,用PLSQL登录提示只能用SYSDBA登录. 用户名:sde 密码:123456 数据库:ORCLZLL 连接为:Normal 点击登录 ...
- 移动端rem布局实现(vw)
什么是rem?在W3C官网上是这样描述的:“font size of the root element (根元素的字体大小)”.就是说rem是相当于html的,因为网页的默认字体大小是 16px,所以 ...
- JS烟花案例优化版
不明白为什么是烟花优化版本的先参考作者的烟花基础版本 烟花优化版本主要实在优化爆炸的范围和运动上做了优化,爆炸范围我们采用已圆的爆炸方式,以鼠标点击的位置为圆形爆炸的烟花效果 <!DOCTYPE ...
- RbbitMQ详解
高性能消息队列RabbitMQ 1.为什么要使用mq 主要解决应用解耦,流量削峰,异步消息,实现高性能,可升缩,最终一致性的架构. 2.activeMq的通讯模式 基于队列(点对点)与发布订阅(有多个 ...
- IDEA+Mybatis-generator代码生成工具
IDEA+Mybatis-generator代码生成工具(小白也能用) 插件介绍 MyBatis Generator简称MBG,是MyBatis 官方出的代码生成器.MBG能够自动生成实体类.Mapp ...
- 360网络安全学习笔记——SQLmap
SQLmap简介 SQLmap是一个开源的自动化的SQL注入工具,其主要功能是扫描,发现并利用给定的URL的SQL注入漏洞. SQL注入模式 1.基于布尔的盲注 2.基于时间的盲注 3.基于报错注入 ...
- Vim查找与替换命令大全,功能完爆IDE!
Vi/Vim 可以说是文本编辑中的一代传奇人物,直至现在,它仍然在高级程序员的武器库中占有一席之地.每个 Linux 发行版默认都包含Vim ,而且即使你不是 Linux 系统用户,你也可以安装 Vi ...