【HDU 1035】Robot Motion(DFS)
来源:互联网 发布:淘宝众筹赚钱 编辑:程序博客网 时间:2024/05/22 09:39
Robot Motion
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
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
题意
给出一个N*M地图,地图上表明了方向(N上S下W左E右),机器人从第一行第start列开始按照地图方向走:
若能走出地图,输出直到走出地图所用的步数,后接“step(s) to exit”
若不能走出地图(走进了一个死循环),输出走进循环之前的步数a,以及循环的步数b。输出格式“a step(s) before a loop of b step(s)”
- 以0 0 0结束输入
思路
挺水的DFS,不过是第一次思路无阻1A的DFS,还是写博客纪念一下。
能够走出迷宫,即没有走到循环死路的时候,步数还是很好统计的。遵循DFS套路,出口越界判断,vis数组标记,递归调用,统计步数。
难点就在于走入死循环情况步数的统计。要分别输出进入循环之前的步数和循环中的步数。容易想到,第一次走到循环入口将vis[i][j]标记为1,第二次必经过一个完整循环到达循环入口,第3次同样。故考虑将vis[row][col]=1改为vis[row][col]++,方便判断,在第三次循环到循环入口时即可输出,ans1-2*ans2即为进入循环之前的步数。
代码示例
#include<iostream>#include<cstring>using namespace std;char Map[15][15];int vis[15][15];int a,b; //a表示行,b表示列 int ans1,ans2;void DFS(int row,int col){ if(row<1||col<1||row>a||col>b) { cout<<ans1<<" step(s) to exit"<<endl; return ; } if(vis[row][col]==1) ans2++; if(vis[row][col]==2) { cout<<ans1-2*ans2<<" step(s) before a loop of "<<ans2<<" step(s)"<<endl; return ; } ans1++; vis[row][col]++; if(Map[row][col]=='N') DFS(row-1,col); if(Map[row][col]=='S') DFS(row+1,col); if(Map[row][col]=='W') DFS(row,col-1); if(Map[row][col]=='E') DFS(row,col+1);}int main(){ int start; while(cin>>a>>b>>start&&a&&b&&start) { ans1=0,ans2=0; memset(vis,0,sizeof(vis)); memset(Map,'0',sizeof(Map));//随意初始化一个值 for(int i=1;i<=a;i++) for(int j=1;j<=b;j++) cin>>Map[i][j]; DFS(1,start); } return 0; }
- HDU 1035 Robot Motion (搜索-DFS)
- 【HDU 1035】Robot Motion(DFS)
- hdu 1035 Robot Motion(dfs)
- hdu 1035Robot Motion(dfs)
- hdu 1035 Robot Motion(dfs)
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
- HDOJ(HDU).1035 Robot Motion (DFS)
- hdu 1035 Robot Motion
- HDU 1035 Robot Motion
- hdu 1035 Robot Motion
- hdu 1035 Robot Motion
- hdu 1035 Robot Motion
- HDU 1035 Robot Motion
- hdu 1035 Robot Motion
- hdu 1035 robot motion
- HDU-1035Robot Motion
- hdu 1035 Robot Motion
- 从应用角度看Android源码
- 朴素贝叶斯算法的简单理解
- linux常用命令
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
- 第一天阅读源码
- 【HDU 1035】Robot Motion(DFS)
- SpringBoot11-springboot-springboot概述
- Centos 7.3搭建LNMP环境
- 7.28之每天一点思维
- 轻量级网络
- MySQL流程控制的使用
- hdu2057 A + B Again(C语言)
- 在django项目中加入像bootstrap这样的css,js,图片等静态文件
- AES256加密方案