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 h


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)


AC Code:


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 15
char map[N][N];
int m,n,p,a,b,vis[N][N],flag,s;
void dfs(int x,int y,int step)
{
  if(x<1||x>m||y<1||y>n)//只要出界,就说明走出了迷宫 
  {
    flag=1;
    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];//用总步数减去之前走过这个点时的步数,就是循环圈的步数
      //对本题第二组样例,s=12,vis[x][y]=4,a=8 
      b=vis[x][y]-1;//用之前走过这个点时的步数减去刚进入迷宫的第一步,就是进入循环之前的步数
      //对本题第二组样例,b=4-1=3
    }
  }
}
int main()
{
  while(~scanf("%d %d %d",&m,&n,&p))
  {
    if(!m&&!n&&!p)
      break;
    for(int i=1;i<=m;i++)
    {
      getchar();
      for(int j=1;j<=n;j++)
      {
        scanf("%c",&map[i][j]);
      }
    }
    flag=0;
    memset(vis,0,sizeof(vis));
    dfs(1,p,1);//从第1行第p列进入,此时站在第一个点,步数为1 
    if(flag)
      printf("%d step(s) to exit\n",s);
    else
      printf("%d step(s) before a loop of %d step(s)\n",b,a);
  }
  return 0;
}


相关文章
|
算法 UED 索引
Nmslib高维空间最近邻逼近搜索算法介绍
业务场景 上一次介绍图像搜索的基本原理,现在记录下使用的数据包的问题。查询图片先进行特征提取,使用一个向量来表示,之后使用该向量与数据库中所有的商品向量进行计算相似度指标,比如cos距离,欧式距离,汉明距离。
6266 0
|
8月前
|
机器学习/深度学习 前端开发 算法
基于STP文件的智能比对系统技术介绍
基于STP文件的智能比对系统通过集成多项先进技术,实现设计图纸与实物的自动化、高精度比对。系统采用分布式架构,包含前端Web界面、后端处理服务器、图像数据库和深度学习模型模块,支持STP文件解析、3D模型可视化、多视角图片生成及实物照片智能匹配。该系统显著提升机械制造和质量控制领域的效率与准确性,减少人工操作误差,广泛应用于设计验证、质量检测等场景。
448 3
|
人工智能 城市大脑 运维
阿里云官网政企业务频道上线!
阿里云官网政企业务频道上线!
339 0
|
XML 机器学习/深度学习 监控
性能监控之Telegraf+InfluxDB+Grafana NVIDIA GPU实时监控
【6月更文挑战12天】性能监控之Telegraf+InfluxDB+Grafana NVIDIA GPU实时监控
455 0
|
Python
Python实现简易文件管理系统
Python实现简易文件管理系统
640 5
|
存储 缓存 网络协议
2.8 基于DPDK的UDP用户态协议栈实现
2.8 基于DPDK的UDP用户态协议栈实现
546 0
|
存储 JSON Java
Nacos心跳机制解读(含简单源码分析)
Nacos心跳机制解读(含简单源码分析)
|
传感器 并行计算 自动驾驶
无人化「萝卜快跑」实际体验+论证分析:Apollo真的超越Waymo了吗?
无人化「萝卜快跑」实际体验+论证分析:Apollo真的超越Waymo了吗?
1104 0

热门文章

最新文章