HDU-1035,Robot Motion(DFS+模拟)

简介: HDU-1035,Robot Motion(DFS+模拟)

Problem 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 0


Sample Output:


10 step(s) to exit


3 step(s) before a loop of 8 step(s)


解题思路:


题目的意思就是说:给你一个地图和一些字符串指令,按要求如果能走出去,输出步数;如果走不出去,输出2个数据,循环之前走了几步,循环周期是多少。首先看看怎么判断出现循环的情况,用一个标记数组,走过的点都标记上(用来装当前的这个点对应的步数),如果出现循环,就可以判断vis标记数组有没有数据。如果能走出去,就看是否越界地图就可以了。


程序代码:


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 20
bool flag;
char map[N][N];
int m,n,p,s,a,b,vis[N][N];
void dfs(int x,int y,int step)
{
  if(x<1||x>m||y<1||y>n)
  {
    flag=true;
    return ;
  }
  s=step;
  if(!flag)
  {
    if(!vis[x][y])
    {
      vis[x][y]=step;
      if(map[x][y]=='N')
        dfs(x-1,y,step+1);
      else if(map[x][y]=='S')
        dfs(x+1,y,step+1);
      else if(map[x][y]=='E')
        dfs(x,y+1,step+1);
      else
        dfs(x,y-1,step+1);
    }
    else
    {
      a=s-vis[x][y];
      b=vis[x][y]-1;
    }
  }
}
int main()
{
  while(cin>>m>>n>>p)
  {
    if(m==0&&n==0&&p==0)
      break;
    for(int i=1;i<=m;i++)
      for(int j=1;j<=n;j++)
        cin>>map[i][j];
    flag=false;
    memset(vis,0,sizeof(vis));
    dfs(1,p,1);
    if(flag)
      cout<<s<<" step(s) to exit"<<endl;
    else
      cout<<b<<" step(s) before a loop of "<<a<<" step(s)"<<endl;
  }
  return 0;
}


相关文章
|
机器人
hdu1035 Robot Motion(简单模拟题)
hdu1035 Robot Motion(简单模拟题)
39 0
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
49 0
|
机器人
HDU-1035,Robot Motion(DFS)
HDU-1035,Robot Motion(DFS)
HDOJ/HDU 1022 Train Problem I(模拟栈)
HDOJ/HDU 1022 Train Problem I(模拟栈)
120 0
HDOJ/HDU 1022 Train Problem I(模拟栈)
|
Java
HDOJ1175连连看 DFS
HDOJ1175连连看 DFS
113 0
|
算法 机器人 定位技术
算法学习之路|hdu 1035 Robot Motion(模拟)
给一个地图,由ESWN(东南西北)组成,机器人根据脚下的指令移动,求如果机器人能走出地图,走的步数多少,如果不能走出,求每绕一圈的步数和绕圈之前走的步数。
1103 0