【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; }