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 ...
随机推荐
- C#入门经典 Chapter3 变量和表达式
3.1 C#基本语法 分号结束语句 花括号字符不需要附带分号 缩进 注释:/*....*/,//,/// 区分大小写 3.2 C#控制台应用程序的基本结构 namespace Chapter3 ...
- Anaconda——Python包管理工具
Anaconda是一个用于科学计算的Python发行版,支持 Linux, Mac, Windows系统,提供了包管理与环境管理的功能 主要用于Python包管理和版本管理. 下载地址:https:/ ...
- Robomongo 0.9.0 连接mongo数据库时,提示连接失败 的解决方案
Robomongo 0.9.0 连接mongo数据库时,提示连接失败.(IP和端口号确定是对的) 基本注意点: 1.mongodb服务打开,打开时,指定端口号,默认为27017,使用默认值,则不用指定 ...
- C# WinForm窗体应用(第四天)
一.点击登录按钮,将两个窗体进行连接,并进行用户名和密码验证. /// <summary> /// 登录设置 /// </summary> /// <param name ...
- dubbo之令牌验证
防止消费者绕过注册中心访问提供者 在注册中心控制权限,以决定要不要下发令牌给消费者 注册中心可灵活改变授权方式,而不需修改或升级提供者 可以全局设置开启令牌验证 <!--随机token令牌,使用 ...
- zabbix--TCP状态监控
Tcp的连接状态对于我们web服务器来说是至关重要的,尤其是并发量ESTAB:或者是syn_recv值,假如这个值比较大的话我们可以认为是不是受到了攻击(例如SYN攻击),或是是time_wait值比 ...
- python tkinter模块 创建窗口V1.2
先上图 代码如下 #-*-coding:utf-8-*- import os from tkinter import * root=Tk() root.title('执行窗口') "&quo ...
- MongoDB主库和从库的数据大小不一致原因判断
1. 环境(MongoDB的版本是3.2.16) [root@xxx-mongodb-primary ~]# cat /etc/redhat-release CentOS Linux release ...
- VMware Workstation搭建Linux操作系统
1.单击“创建新的虚拟机”选项,并在弹出的“新建虚拟机向导”界面中选择“自定义”单选按钮,然后单击“下一步”. 新建虚拟机向导 2.选择虚拟机硬件兼容性,是否兼容之前旧的版本. 兼容性选择 3.选中“ ...
- 远程连接Ubuntu的桌面
参考:http://www.linuxidc.com/Linux/2016-06/132442.htm http://teliute.org/linux/TeUbt/lesson52/lesson52 ...