Robot Motion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10072    Accepted Submission(s): 4697

Problem 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
DFS简单题 
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=;
char mp[N][N];
int visited[N][N];
int m,n;
int flag;
int step,loopstep;
void DFS(int x,int y){
if(x<||x>=n||y<||y>=m){
return ;
}
if(visited[x][y]!=){
flag=;
loopstep=step-visited[x][y]+;
step=visited[x][y]-;
return ;
}
step++;
visited[x][y]=step;
if(mp[x][y]=='N')DFS(x-,y);
else if(mp[x][y]=='S')DFS(x+,y);
else if(mp[x][y]=='E')DFS(x,y+);
else if(mp[x][y]=='W')DFS(x,y-);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(n==&&m==)break;
int k;
scanf("%d",&k);
flag=;
for(int i=;i<n;i++)scanf("%s",mp[i]);
loopstep=step=;
memset(visited,,sizeof(visited));
DFS(,k-);
if(flag==) printf("%d step(s) to exit\n", step);
else
printf("%d step(s) before a loop of %d step(s)\n", step,loopstep); }
}
 
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)

最新文章

  1. FastFourierTransform (FFT)
  2. static静态变量的理解
  3. C语言 内存四大存储区域
  4. CSS 实现加载动画之三-钢琴按键
  5. android布局 FrameLayout(帧布局)详解
  6. tcpproxy:基于 Swoole 实现的 TCP 数据包转发工具的方法
  7. 为App签名(为apk签名)
  8. VS2008注册码
  9. hdu 2121 , hdu 4009 无定根最小树形图
  10. CodeForces 148D-Bag of mice(概率dp)
  11. Spark SQL概念学习系列之Spark SQL 架构分析(四)
  12. NodeJS V8 GC概览
  13. BZOJ_1610_[Usaco2008_Feb]_Line连线游戏_(计算几何基础+暴力)
  14. 【翻译】Selenium IDE v1.0.11 支持转换格式吗?
  15. 协助ScriptCase7.1做些汉化矫正工作
  16. Delphi 进阶基础技能说明
  17. windows搭建web服务器
  18. 关于loadrunner使用web_add_header添加HTTP信息头(比如Content-Type,token等)和使用
  19. 第一册:lesson seventeen。
  20. html button 点击 显示倒计时秒数

热门文章

  1. ZfNet解卷积:可视化CNN模型( PythonCode可视化Cifar10)
  2. 【转载】HTTP 响应头与状态码
  3. UICollectionView框架总结
  4. java主要集合类的数据结构
  5. 浏览器加载 CommonJS 模块的原理与实现 (阮一峰大哥的 http://www.ruanyifeng.com/blog/2015/05/commonjs-in-browser.html)
  6. .net core发布程序
  7. .net 程序集加载,版本不匹配的解决方法
  8. 【Linq】标准查询操作符
  9. Linux常用解压缩命令
  10. gitlab的添加密钥