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 ...
随机推荐
- 类似 select 选择框效果及美化
网上有各种各样的关于 select 选择框的美化,找了很多,并没有好的样式效果.所以就找了一个利用 ul li 做的类似 select 选择框的效果,不废话了,先上图,效果如下: 对于上图的箭头效果, ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- Lintcode: Rehashing
The size of the hash table is not determinate at the very beginning. If the total size of keys is to ...
- csuoj 1022: 菜鸟和大牛
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1022 1022: 菜鸟和大牛 Time Limit: 1 Sec Memory Limit: 1 ...
- windows系统调用 线程 启动与挂起
#include "iostream" #include "windows.h" using namespace std; class CWorkerThrea ...
- java正则表达式练习
package shb.java.demo3; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 正则表达式简 ...
- paper 27 :图像/视觉显著性检测技术发展情况梳理(Saliency Detection、Visual Attention)
1. 早期C. Koch与S. Ullman的研究工作. 他们提出了非常有影响力的生物启发模型. C. Koch and S. Ullman . Shifts in selective visual ...
- oracle中的常用函数
一.运算符算术运算符:+ - * / 可以在select 语句中使用连接运算符:|| select deptno|| dname from dept; 比较运算符:> >= = != &l ...
- php防止表单重复提交
解决方案一:引入cookie机制来解决 提交页面代码如下a.php代码如下: <form id="form1" name="form1" method=& ...
- 九、Java基础---------面向对象封装、继承、多态
封装 1.1 基本概念 封装(encapsulation)是面向对象三大特征之一,它是指将对象的状态信心隐藏在对象的内部,不允许外部直接进行访问,而是通过该类提供的方法来实现对内部信息的操作和访问. ...