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验证相关

    asp.net mvc中有验证,当然在silverlight中也包含有验证规则的但这个就离不开mvvm(其实就是实体层,页面这些东西的组成,没有用过呀,悲哀!连这个概念都理解不了). 关于MVVM验证 ...

  2. 在 Node.js 上调用 WCF Web 服务

    摘要:有时我们需要在WCF中做一些复杂数据处理和逻辑判断等,这时候就需要在NodeJS中调用WCF服务获取数据,这篇文件介绍如何在Node中调用WCF服务获取数据. Node项目中调用WCF服务获取数 ...

  3. Lintcode: Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  4. 面向对象 理解 C#复习

    面向对象: 是基于万物皆对象这个哲学观点. 所谓的面向对象就是将我们的程序模块化,对象化,把具体事物的特性属性和通过这些属性来实现一些动作的具体方法放到一个类里面 通俗点讲: 一切都是对象 举例: 将 ...

  5. [转]easyui data-options的使用

    原文地址:easyui data-options的使用 data-options是jQuery Easyui 最近两个版本才加上的一个特殊属性.通过这个属性,我们可以对easyui组件的实例化可以完全 ...

  6. 每天一个shell知识--数组

    1.shell中数组的定义: 数组名=(value value1 value2 ) 也可以单独的设定数组的分量: arrayL[0]=value arrayL[1]=value1 2.${arrayL ...

  7. C++之路进阶——codevs4416(FFF的后宫)

    4416 FFF 团卧底的后宫  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold       题目描述 Description 你在某日收到了 FFF 团卧底的求 ...

  8. SQL2008无法连接到.\SQLEXPRESS,用户'sa'登录失败(错误18456)图文解决方法

      出现问题 :标题: 连接到服务器 ------------------------------ 无法连接到 .\SQLEXPRESS. ------------------------------ ...

  9. System Hold, Fix Manager before resetting counters

    程序pending http://www.askmaclean.com/archives/2011/11 http://blog.itpub.net/35489/viewspace-717132/ 1 ...

  10. 夺命雷公狗---node.js---11之文件上传

    我们在做文件上传前需要用npm来安装一个插件先, 首先打开项目所在的目录,然后按住shift键然后右键鼠标进入命令行安装formidable 然后开始编写上传的静态页面: <!DOCTYPE h ...