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

There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.

Output

For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.

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的更多相关文章

  1. POJ 1573 Robot Motion(BFS)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12856   Accepted: 6240 Des ...

  2. hdoj 1035 Robot Motion

    Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. hdu1035 Robot Motion (DFS)

    Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  4. poj1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12507   Accepted: 6070 Des ...

  5. Robot Motion(imitate)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11065   Accepted: 5378 Des ...

  6. 模拟 POJ 1573 Robot Motion

    题目地址:http://poj.org/problem?id=1573 /* 题意:给定地图和起始位置,robot(上下左右)一步一步去走,问走出地图的步数 如果是死循环,输出走进死循环之前的步数和死 ...

  7. Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏

    Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11262 Accepted: 5482 Descrip ...

  8. POJ 1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12978   Accepted: 6290 Des ...

  9. Poj OpenJudge 百练 1573 Robot Motion

    1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...

随机推荐

  1. [noip模拟]祖孙询问<LCA>

    [问题描述] 已知一棵n个节点的有根树.有m个询问.每个询问给出了一对节点的编号x和y,询问x与y的祖孙关系. [输入格式] 输入第一行包括一个整数n表示节点个数. 接下来n行每行一对整数对a和b表示 ...

  2. 改进ls的实现

    一.要求 参见附图,改进你的ls的实现.提交代码运行截图和码云链接 二.步骤 目录 ls 功能:列出目录内容,显示文件信息 ls -l:显示当前工作目录下包含目录及属性详细信息(共7列) 第一列:文件 ...

  3. Codeforces Round #627 (Div. 3)

    1324A - Yet Another Tetris Problem(思维) 题意 给一个数组,每一个数组中的元素大小表示在竖直方向的方块数量,元素相邻怎竖直方向的方块也相邻,类似于俄罗斯方块当底层被 ...

  4. Oracle给权限和同义词

    在同一个DB下,用户A创建了一个Table(student),用户B无法访问.如果B想要访问,就需要A赋予B权限. 登录用户A执行下面语句: GRANT SELECT, INSERT, UPDATE, ...

  5. Spring ApplicationContext 容器

    Spring ApplicationContext 容器 Application Context 是 BeanFactory 的子接口,也被成为 Spring 上下文. Application Con ...

  6. linux下使用笔记本的相关设置

    目录 无线连接 Wi-Fi 蓝牙 触摸板 电源管理 电源管理工具 电源相关行为的响应动作 按键和盖子的响应动作 电池低电量的响应动作 处理器调整 调频工具 关闭睿频 intel_pstate 休眠配置 ...

  7. vue按需引入/全局引入echarts

    npm install echarts -S 1.按需引入 新建echarts.js公共引入 // 文件路径 @/lib/echarts.js 自行配置 // 加载echarts,注意引入文件的路径 ...

  8. CSS的选择器分为两大类

    CSS的选择器分为两大类:基本选择题和扩展选择器. 基本选择器: 标签选择器:针对一类标签 ID选择器:针对某一个特定的标签使用 类选择器:针对你想要的所有标签使用 通用选择器(通配符):针对所有的标 ...

  9. js获取dom节点之 id 获取

    在JavaScript中,标准的id选择器调用语法是: document.getElementById('myid').style.width = pc + "%"; 但是,今天发 ...

  10. urllib笔记

    在Python 3中,urllib2被合并到了urllib中,叫做urllib.request 和 urllib.error .urllib整个模块分为urllib.request, urllib.p ...