博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1035 Robot Motion
阅读量:7296 次
发布时间:2019-06-30

本文共 4432 字,大约阅读时间需要 14 分钟。

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 

 

Sample Output

10 step(s) to exit

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

解题思路:直接用深搜。此题有两种情况:

1.一种就是能走出来;另一种就是走不出来;

2.在里面有一个循环的路径。

对于情况1很好解决,只需用路径标记数组判断没有走到走过的路并且走出啦矩形方框。而情况2也差不多,当走到走过的路时,记录此时走的步数step1,并把再从此点走一次,再次回到此点时,记录步数为step2,那么循环的步数就为step2-step1,循环前的步数就为2step1-step2。如图(红线走两次,绿线走一次):

代码为:

 

#include
#include
#include
#include
using namespace std;int n,m,k,step,step1,flag;char s[100][100];int map[100][100];int f(int x,int y) //判断是否出去啦,减枝{    if(x<0||x>=n||y<0||y>=m)    {        return 0;    }    return 1;}void f1(int x,int y) //遇到啦重复点{    void dfs(int a,int b);    if(map[x][y])    {        if(flag==1)        {            step++;            step1=step;            flag=2;            memset(map,0,sizeof(map));            map[x][y]=1;            dfs(x,y);        }                else if(flag==2)        {            step++;            printf("%d step(s) before a loop of %d step(s)\n",2*step1-step,step-step1);                }    }}void dfs(int x,int y){    if(!f(x,y))    {        printf("%d step(s) to exit\n",step);        }        else    {            if(s[x][y]=='N')        {                if(!map[x-1][y])            {                map[x-1][y]=1;                step++;                dfs(x-1,y);            }            else            {                f1(x-1,y);                            }                    }        else if(s[x][y]=='S')        {            if(!map[x+1][y])            {                map[x+1][y]=1;                step++;                dfs(x+1,y);            }            else             {                f1(x+1,y);                        }        }        else if(s[x][y]=='W')        {            if(!map[x][y-1])            {                map[x][y-1]=1;                step++;                dfs(x,y-1);            }            else             {                f1(x,y-1);                        }        }        else if(s[x][y]=='E')        {            if(!map[x][y+1])            {                map[x][y+1]=1;                step++;                dfs(x,y+1);            }            else            {                f1(x,y+1);                        }        }    }}int main(){    while(scanf("%d%d",&n,&m)!=EOF&&n&&m)    {        scanf("%d",&k);        memset(map,0,sizeof(map));        for(int i=0;i

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/gantaiyuan/p/3212355.html

你可能感兴趣的文章
Android深入浅出之Binder机制
查看>>
动态磁盘的管理
查看>>
zookeeper 集群安装(单点与分布式成功安装)
查看>>
python中list,dirt方法说明
查看>>
lamp环境一键部署(yum)
查看>>
一个IT大学生来深圳2年半的经历感受
查看>>
VMware View 5.0 桌面虚拟化方案介绍视频
查看>>
理解Spring中的事务抽象
查看>>
java 设计模式 建造者模式
查看>>
mysql备份和恢复工作记录
查看>>
我的友情链接
查看>>
vFrank考VCDX的过程
查看>>
jQuery input同步发sims
查看>>
memcached起步
查看>>
lesson 10-你所不知道的邮件退信代码
查看>>
OSPF LSA过滤简述
查看>>
m283-tftp传输,nfs挂载rootfs
查看>>
Windows Server 2008搭建***服务
查看>>
实验一 路由配置(cisco packet tracer)
查看>>
装机流程
查看>>