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 ...
随机推荐
- html5——多媒体(四)
全屏兼容 box.requestFullscreen(); box.webkitRequestFullScreen(); box.mozRequestFullScreen(); <!DOCTYP ...
- html——ico
下载: 网址+/favicon.ico 引用: 1.<link href="favicon.ico" rel="icon" /> 2.<lin ...
- Zynq7000系列之芯片系统结构概述
相比较经典的FPGA,Zynq7000系列最大的特点是将处理系统PS和可编程资源PL分离开来,固化了PS系统的存在,实现了真正意义上的SOC(System On Chip). 1. Zynq7000 ...
- dos2unix xxx.sh
前几天写了一个Linux的自动化脚本,没有使用变量,就是一些Linux命令的集合 今天试着把一些相同的字段提出来用变量表示,然后在Linux里运行,就发现一直在报错: : command not fo ...
- 轻松理解 Android Binder,只需要读这一篇
在 Android 系统中,Binder 起着非常重要的作用,它是整个系统 IPC 的基石.网上已经有很多文章讲述 Binder 的原理,有的讲的比较浅显,没有触及到关键,有的讲的太过于深入底层,难以 ...
- CAD在图纸保存的同时,也把基本信息保存了(网页版)
主要用到函数说明: MxDrawXCustomFunction::Mx_SaveDwgToURLEx 保存DWG文件到服务器上的扩展函数.详细说明如下: 参数 说明 pszServerUrl 服务器网 ...
- windows程序设为开机自启动
在Windows文件管理器中输入 %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup 把程序快捷方式放到此处即可.
- centos7安装个人网盘owncloud
现在个人资料越来越重要,网络速度也已经满足日常需要,网盘已经是生活着存取个人数据不可缺少的工具. 下面在linxu centos7下面安装owncloud搭建自己私人网盘: 1.新建一个账号用来安装个 ...
- BZOJ 4561: [JLoi2016]圆的异或并 扫描线 + set
看题解看了半天...... Code: #include<bits/stdc++.h> #define maxn 200010 #define ll long long using nam ...
- Centos 7 关闭firewall防火墙启用iptables防火墙
一.关闭firewall防火墙 1.停止firewall systemctl stop firewalld.service 2.禁止firewall开机启动 systemctl disable fir ...