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 ...
随机推荐
- [dp+博弈]棋盘的必胜策略
链接:https://ac.nowcoder.com/acm/problem/21797来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K ...
- promise的优势
通过不同的方式读取在 files 文件夹下的三个文件来引出 promise 在处理异步时与回调函数相比的优势,files 文件夹有三个文件 a.json,b.json,c.json. // a.jso ...
- SpringCloud-Nacos/OpenFien/Gateway的基本介绍及快速上手
一.Spring-Cloud-Alibaba-Nacos 注册中心 1.下载.安装 Nacos 下载地址:https://github.com/alibaba/nacos/releases 下载后解压 ...
- Sublimeの虚拟环境(Venv)设置
这里主要介绍,在使用 Python 虚拟环境(Venv)时,SublimeText 该怎么设置 为什么使用虚拟环境(Venv) 因为,我有洁癖! 我就是喜欢看到,pip list 命令下什么 Pack ...
- K-Folds cross-validator-K折交叉验证实现
源码: import numpy as np from sklearn.model_selection import KFold X = np.array([[, ], [, ], [, ], [, ...
- 使用内部枚举类作为外部类的参数的Mybatis的参数该如何判断
新写了一个接口,期望根据不同的参数来给数据库中不同的字段进行传值.这里使用了内部静态枚举类的方式进行传值,在写mybatis动态sql时,如果是普通对象,一般使用,那么使用枚举类,如何判断枚举类的值呢 ...
- Azure安装win2016的服务器,并下载安装mysql数据库心得
随便写写 第一部分:新建虚拟机创建win2016服务器 这部分内容跟着微软云提示操作即可, 基本步骤:创建一堆名字,选择一个地区的服务器,配置一些基本信息,然后azure就会自动创建虚拟机并安装你选择 ...
- echarts整理
保存一些常用的echarts图表及制作方法
- VAuditDemo-任意文件读取
任意文件读取是属于文件操作漏洞的一种. 一般任意文件读取漏洞可以读取配置信息.甚至系统重要文件. 严重的话,就可能导致SSRF,进而漫游内网. 文件操作漏洞 任意文件删除--删除lock 任意文件复制 ...
- 关于wget下载jdk问题解决
问题: 直接从jdk官网下载会出现: 正在解析主机 login.oracle.com (login.oracle.com)... 156.151.58.18正在连接 login.oracle.com ...