Robot Motion 分类: POJ 2015-06-29 13:45 11人阅读 评论(0) 收藏
| 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
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
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) 收藏的更多相关文章
- 百度地图-省市县联动加载地图 分类: Demo JavaScript 2015-04-26 13:08 530人阅读 评论(0) 收藏
在平常项目中,我们会遇到这样的业务场景: 客户希望把自己的门店绘制在百度地图上,通过省.市.区的选择,然后加载不同区域下的店铺位置. 先看看效果图吧: 实现思路: 第一步:整理行政区域表: 要实现通过 ...
- 【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 ...
- 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 ...
- Segment Tree 扫描线 分类: ACM TYPE 2014-08-29 13:08 89人阅读 评论(0) 收藏
#include<iostream> #include<cstdio> #include<algorithm> #define Max 1005 using nam ...
- Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13:08 99人阅读 评论(0) 收藏
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, ...
- 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 ...
- 积分图像的应用(一):局部标准差 分类: 图像处理 Matlab 2015-06-06 13:31 137人阅读 评论(0) 收藏
局部标准差在图像处理邻域具有广泛的应用,但是直接计算非常耗时,本文利用积分图像对局部标准差的计算进行加速. 局部标准差: 标准差定义如下(采用统计学中的定义,分母为): 其中. 为了计算图像的局部标准 ...
- iOS开发~CocoaPods使用详细说明 分类: ios相关 2015-04-01 16:45 68人阅读 评论(0) 收藏
iOS开发-CocoaPods使用详细说明 一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来 ...
- 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 ...
随机推荐
- PostgreSQL 中日期类型转换与变量使用及相关问题
PostgreSQL中日期类型与字符串类型的转换方法 示例如下: postgres=# select current_date; date ------------ 2015-08-31 (1 row ...
- C++Builder 笔记
1.界面窗口如何不显示标题栏? 在Form属性栏里面把BorderStyle的值设为None 2.wchar_t wchar_t是C/C++的字符类型,是一种扩展的存储方式,wchar_t类型主要用在 ...
- 。。。JDBC里面的sql与hibernate里面的hql有关占位符"?"的总结。。。
今天在看Hibernate的时候,似乎又有了一些收获的东东,嘻嘻... 我记得很清楚:以前用JDBC操作数据库的时候是这样的: String sql = "select * from use ...
- springday02-go2
1.复制xml文件到container.auto下2.Waiter类实现构造函数3.Bar类中Waiter作为其成员变量,并实现其get/set方法,有参和无参构造器,toString方法4.分别修改 ...
- 事务——sql server中的事务应用举例
sql中事务只针对一个update,delete,insert语句,如果一段程序中有超过一个这样的语句,就需要每个都判断是否出错,否则就会出现若干我们不希望的情形出现,举例如下(表结构见最后): 1, ...
- Demo13
this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void o ...
- paper 47 :Latex中文显示(转)
中文支持需要cjk-latex,总得来说中文可以使用GB和GBK两种字体,GBK需要从windows下copy *.ttc或*.ttf, GB字体则在linux下就用. 先说支持GB的中文显示,安装以 ...
- String,StringBuffer,StringBuilder三者区别
String:每次改变,String都会重新构造,内存指针都会改变 StringBuffer:主要用在全局变量中 StringBuilder:在线程内完成字符拼接,因为线程是不安全的,所以完成后可以丢 ...
- 夺命雷公狗---TP商城----TP之配置环境---1
下载到tp3.2.3版本后架设到自己的wamp环境下,然后配置虚拟主机,完事后直接开工 环境下创建一个文件夹,然后里面存放这这两个文件即可开始新的旅途了 这里完了,下一步就开始配置index.php文 ...
- DDR(二)
DDR与SDRAM的最大区别:内部L-Bank的规格不同. SDRAM中的L-Bank存储单元的容量与芯片位宽相同, DDRAM中的存储单元的容量是芯片位宽的一倍. 所以一次的地址访问,可以进行2-P ...