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

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.
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.
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.
#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); }
}
3 step(s) before a loop of 8 step(s)
hdu 1035(DFS)的更多相关文章
- hdu 1342(DFS)
Lotto Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- F - JDG HDU - 2112 (最短路)&& E - IGNB HDU - 1242 (dfs)
经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...
- F - Auxiliary Set HDU - 5927 (dfs判断lca)
题目链接: F - Auxiliary Set HDU - 5927 学习网址:https://blog.csdn.net/yiqzq/article/details/81952369题目大意一棵节点 ...
- hdu - 1072(dfs剪枝或bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...
- HDU——2647Reward(DFS或差分约束)
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- P - Sudoku Killer HDU - 1426(dfs + map统计数据)
P - Sudoku Killer HDU - 1426 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为 ...
- hdu 1142(DFS+dijkstra)
#include<iostream> #include<cstdio> #include<cmath> #include<map> #include&l ...
- hdu 1015(DFS)
Safecracker Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1181(DFS)变 形 课
变形课 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submis ...
随机推荐
- ubuntu14.3安装phpmyadmin
一.安装 sudo apt-get install phpmyadmin 二.软连接 cd /var/www/html/ sudo ln -s /usr/share/phpmyadmin phpmya ...
- cocos自动图集
对于图像资源,为什么要用图集,cocos官网的解释: 1.合成图集时会去除每张图片周围的空白区域,加上可以在整体上实施各种优化算法,合成图集后可以大大减少游戏包体和内存占用2.多个 Sprite 如果 ...
- xamarin.forms模拟rem动态大小值,实现屏幕适配
开发app的时候,比较麻烦的地方,就是处理屏幕适配,比如文字设为12的大小,测试的时候,看得文字挺正常,可是,放到高分辨率设备一看,文字就变得特别小, 怎样实现随着分辨率变大或者变小,所有的size数 ...
- Python 模块的导入 day5
一.模块 1.标准模块 python自带的 2.第三方模块 需要自己安装的模块 3.自己写的python文件 一个python文件,就是一个模块 (1)导入模块的实质 就是把你导入的模块运行了一遍 ( ...
- C# DataTable扩展方法
在日常搬砖中,总结了一些简单的扩展方法. public static bool IsNullOrEmpty(this DataTable dt) { ; } public static bool Is ...
- Delphi / Pascal 语法知识干货
********************************************* Pascal.Delph干货 *************************************** ...
- UNIX C XSI_IPC对象、共享内存
1.创建IPC对象 #include <sys/ipc.h> key_t ftok(const char* pathname,int proj_id); 成功返回可用于创建或获取IPC的键 ...
- Centos6防火墙基本配置
1. 允许x.x.x.x访问本机 iptables -I INPUT -p tcp -j DROP #若要添加多了ip,该条也是只执行一次 iptables -I INPUT -s x.x.x.x - ...
- check_nrpe: ERROR - could not complete SSL handshake
情景描述: 发现的问题是 在监控端执行 ./check_nrpe -H 被监控端ip 正常返回nrpe版本 在被监控端执行 ./check_nrpe -H 监控端ip 报错 check_nrpe: E ...
- 【郑轻邀请赛 I】这里是天堂!
[题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2135 [题意] [题解] 答案应该为C(n,a)∗C(m,b)/C(n+m,a+b) ...