Robot Motion (DFS)

题目链接Robot Motion

题目

Robot Motion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15129 Accepted Submission(s): 7041

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.给你一张N*M的地图,N向上 S向下 W向左 E向右。
2.一个机器人从第一行的第star列开始走,若能走出整张地图,输出直到走出地图所用的步数,后接“step(s) to exit”
3.如果机器人陷入循环输出走进循环之前的步数a,以及循环的步数b。输出格式“a step(s) before a loop of b step(s)”
4.输入0 0 0结束

思路 :

首先要从第一行的第star开始进行试探,也就是dfs(1,star);此时行数 h ,列数 l都在随着mapp[h][l]这个位置的符号所决定。

当行数 h 列数 l 不满足边界条件时代表走出整张地图;每进行一次dfs都要对步数foot进行加1;

难点就在于走入死循环情况步数的统计。要分别输出进入循环之前的步数和循环中的步数。容易想到,第一次走到循环入口将vis[h][l]标记为1,第二次必经过一个完整循环到达循环入口,第3次同样。故考虑将vis[h][l]=1改为vis[h][l]++,方便判断,在第三次循环到循环入口时即可输出,ans1-2*ans2即为进入循环之前的步数。

AC代码:

#include <bits/stdc++.h>

using namespace std;
int n,m,star;
char mapp[15][15];
int vist[15][15];
int foot;
int size;
void dfs(int h,int l)
{
    if(h<1||l<1||h>n||l>m)
    {
        cout <<foot<<" step(s) to exit"<<endl;
        return ;
    }
    if(vist[h][l]==1) size++;
    if(vist[h][l]==2)
    {
        cout <<foot-size*2<<" step(s) before a loop of "<<size<<" step(s)"<<endl;
        return ;
    }

    foot++;
    vist[h][l]++;
    if(mapp[h][l]=='N') dfs(h-1,l);
    if(mapp[h][l]=='S') dfs(h+1,l);
    if(mapp[h][l]=='E') dfs(h,l+1);
    if(mapp[h][l]=='W') dfs(h,l-1);
}
int main()
{
    while(cin >>n>>m>>star)
    {
        foot=size=0;
        if(!n&&!m&&!star) break;
        memset(mapp,'0',sizeof(mapp));
        memset(vist,0,sizeof(vist));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                cin >>mapp[i][j];
        dfs(1,star);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值