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

Description




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)
开始是将vis[0][star-1]=0,WA了好几次,后来想明白了,有循环到开始的情况,如果设置为零则无法判断,果断改为1了
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std; const int Max=1100000; char Map[1100][1100];
int vis[1100][1100];
int n,m,star;
int main()
{
while(scanf("%d %d %d",&n,&m,&star))
{
if(!n&&!m)
{
break;
}
scanf("%d",&star);
for(int i=0; i<n; i++)
{
scanf("%s",Map[i]);
}
memset(vis,0,sizeof(vis));
vis[0][star-1]=1;
int u=0;
int v=star-1;
while(1)
{
if(Map[u][v]=='N')
{
if(u-1==-1)
{
printf("%d step(s) to exit\n",vis[u][v]);
break;
}
else if(vis[u-1][v])
{
printf("%d step(s) before a loop of %d step(s)\n",vis[u-1][v]-1,vis[u][v]+1-vis[u-1][v]);
break;
} vis[u-1][v]=vis[u][v]+1;
u--; }
else if(Map[u][v]=='S')
{
if(u+1==n)
{
printf("%d step(s) to exit\n",vis[u][v]);
break;
}
else if(vis[u+1][v])
{
printf("%d step(s) before a loop of %d step(s)\n",vis[u+1][v]-1,vis[u][v]+1-vis[u+1][v]);
break;
} vis[u+1][v]=vis[u][v]+1;
u++; }
else if(Map[u][v]=='E')
{
if(v+1==m)
{
printf("%d step(s) to exit\n",vis[u][v]);
break;
}
else if(vis[u][v+1])
{
printf("%d step(s) before a loop of %d step(s)\n",vis[u][v+1]-1,vis[u][v]+1-vis[u][v+1]);
break;
} vis[u][v+1]=vis[u][v]+1;
v++; }
else if(Map[u][v]=='W')
{
if(v-1==-1)
{
printf("%d step(s) to exit\n",vis[u][v]);
break;
}
else if(vis[u][v-1])
{
printf("%d step(s) before a loop of %d step(s)\n",vis[u][v-1]-1,vis[u][v]+1-vis[u][v-1]);
break;
} vis[u][v-1]=vis[u][v]+1;
v--; }
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏的更多相关文章

  1. 百度地图-省市县联动加载地图 分类: Demo JavaScript 2015-04-26 13:08 530人阅读 评论(0) 收藏

    在平常项目中,我们会遇到这样的业务场景: 客户希望把自己的门店绘制在百度地图上,通过省.市.区的选择,然后加载不同区域下的店铺位置. 先看看效果图吧: 实现思路: 第一步:整理行政区域表: 要实现通过 ...

  2. 【Heritrix基础教程之2】Heritrix基本内容介绍 分类: B1_JAVA H3_NUTCH 2014-06-01 13:02 878人阅读 评论(0) 收藏

    1.版本说明 (1)最新版本:3.3.0 (2)最新release版本:3.2.0 (3)重要历史版本:1.14.4 3.1.0及之前的版本:http://sourceforge.net/projec ...

  3. Who's in the Middle 分类: POJ 2015-06-12 19:45 11人阅读 评论(0) 收藏

    Who's in the Middle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34155   Accepted: 1 ...

  4. Segment Tree 扫描线 分类: ACM TYPE 2014-08-29 13:08 89人阅读 评论(0) 收藏

    #include<iostream> #include<cstdio> #include<algorithm> #define Max 1005 using nam ...

  5. Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13:08 99人阅读 评论(0) 收藏

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, ...

  6. Segment Tree 分类: ACM TYPE 2014-08-29 13:04 97人阅读 评论(0) 收藏

    #include<iostream> #include<cstdio> using namespace std; struct node { int l, r, m; int ...

  7. 积分图像的应用(一):局部标准差 分类: 图像处理 Matlab 2015-06-06 13:31 137人阅读 评论(0) 收藏

    局部标准差在图像处理邻域具有广泛的应用,但是直接计算非常耗时,本文利用积分图像对局部标准差的计算进行加速. 局部标准差: 标准差定义如下(采用统计学中的定义,分母为): 其中. 为了计算图像的局部标准 ...

  8. iOS开发~CocoaPods使用详细说明 分类: ios相关 2015-04-01 16:45 68人阅读 评论(0) 收藏

    iOS开发-CocoaPods使用详细说明 一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来 ...

  9. c++map的用法 分类: POJ 2015-06-19 18:36 11人阅读 评论(0) 收藏

    c++map的用法 分类: 资料 2012-11-14 21:26 10573人阅读 评论(0) 收藏 举报 最全的c++map的用法 此文是复制来的0.0 1. map最基本的构造函数: map&l ...

随机推荐

  1. Silverlight动画显示Line线

    目的:在silverlight中显示两点之间的连线,要求动画显示连线效果. 如果需实现动画效果不得不了解,Storyborad对象: Storyboard Silverlight   通过时间线控制动 ...

  2. eclipse的debug模式启动缓慢

      这个问题可能是由于eclipse和服务器的交互而产生的,在以debug模式启动服务器时,发生了读取文件错误,eclipse自动设置了断点,导致服务器不能正常启动. 解决方法如下:以debug模式启 ...

  3. c# 获取路径的几种方法

    1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDomain.Bas ...

  4. mybatis的xml中特殊转义字符和模糊查询like的写法

    做个备忘: xml特殊符号转义写法 <          < >          > <>   <> &      & &ap ...

  5. 夺命雷公狗jquery---4内容选择器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. JSP-02- 使用JSP实现输出

    二. 使用JSP实现输出 JSP的页面构成: 静态内容.指令.表达式.Scriptlet.声明.动作.注释 Jsp脚本: 表达式.Scriptlet.声明 表达式: <%=  内容  %> ...

  7. linux强制umount设备的方法

    假如挂载时使用了:mount /dev/sda1 /mnt/sda1 #查找占用设备的pid fuser -m /mnt/sda1 #假如此时得到的pid为12345 kill -9 12345 um ...

  8. 【rails3教材】博客构建过程2

    2. 使用脚手架快速搭建网页 rails的脚手架可以快速生成应用程序的一些片段,如果你需要为一个资源创建一系列的控制器视图模型,那么脚手架就是你需要的工具 3. 创建资源 对于一个博客程序,你可以以生 ...

  9. OpenStack 虚拟机监控方案确定

    Contents [hide] 1 监控方案调研过程 1.1 1. 虚拟机里内置监控模块 1.2 2. 通过libvirt获取虚拟机数据监控. 2 a.测试openstack的自待组件ceilomet ...

  10. decimal.tostring()格式

    nt/Decimal.ToString 方法 (String, IFormatProvider)   decimal value = 16325.62m; string specifier; Cult ...