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 ...
随机推荐
- 贪心-Course Schedule III
2020-02-01 21:37:39 问题描述: 问题求解: 对于课程来说截止时间在前面的肯定需要优先安排,所以首先需要将courses按照deadline进行排序. 然后只需要不断的加入当前的课程 ...
- windows常用系统命令
dir指定要列出的驱动器.目录和/或文件 显示当前路径下的所有文件的绝对路径,包含子文件夹中的内容 dir /b / s /o:n /a:a /b 表示去除摘要信息,且顶格显示完整路径 /s 表示枚举 ...
- 正则表达式(R&Python)
regular expression 1.R,strongly recommend this blog The table_info examples are following: du_mtime_ ...
- 一个完整的机器学习项目在Python中的演练(一)
大家往往会选择一本数据科学相关书籍或者完成一门在线课程来学习和掌握机器学习.但是,实际情况往往是,学完之后反而并不清楚这些技术怎样才能被用在实际的项目流程中.就像你的脑海中已经有了一块块"拼 ...
- coding++:TransactionDefinition 接口介绍
TransactionDefinition类结构: 作用: 1.TransactionDefinition接口被用于Spring事物支持的核心PlatformTransactionManager接口, ...
- nodejs使用express中静态资源托管(express.static())时遇到的bug
如下:将test.html的页面挂载在服务器上, const express= require('express') const fs= require('fs') let app = express ...
- leetcode并发题解
按序打印 解法一:使用volatile public class FooWithVolatile { private volatile int count; public FooWithVolatil ...
- ConcurrentHashMap1.7和1.8的源码分析比较
ConcurrentHashMap 在多线程环境下,使用HashMap进行put操作时存在丢失数据的情况,为了避免这种bug的隐患,强烈建议使用ConcurrentHashMap代替HashMap,为 ...
- TCP/IP中的传输层协议TCP、UDP
TCP提供可靠的通信传输,而UDP则常用于让广播和细节控制交给应用的通信传输. 传输层协议根据IP数据报判断最终的接收端应用程序. TCP/IP的众多应用协议大多以客户端/服务端的形式运行.客户端是请 ...
- Android 数据库框架 DBFlow 的使用
原文首发于微信公众号:jzman-blog,欢迎关注交流! DBFlow 是一个基于注解处理器开发的使用方便的 ORM Android 数据库,该库简化了很多多余的代码,并且提供了好用的 API 来处 ...