zoukankan      html  css  js  c++  java
  • 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
     
    Sample Output
    10 step(s) to exit 3 step(s) before a loop of 8 step(s)

    这道题就注意一开始成环的情况。 所以我们初始化的vis数组就不能是0了   我觉得坑点就在这

    1 1 1
    S
    2 1 1
    S
    N
    1 2 1
    EW
    答案:
    1 step(s) to exit
    0 step(s) before a loop of 2 step(s)
    0 step(s) before a loop of 2 step(s)

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <set>
     7 #include <queue>
     8 #include <math.h>
     9 #include <stdbool.h>
    10 
    11 #define LL long long
    12 #define inf 0x3f3f3f3f
    13 using namespace std;
    14 const int MAXN=1000005;
    15 
    16 int n,m,k;
    17 char map[15][15];
    18 int vis[15][15];
    19 int flag = 0;
    20 
    21 void dfs(int row,int col,int step)
    22 {
    23     if(flag) return ;
    24     if(row<0||col<0||row>=n||col>=m)
    25     {
    26         printf("%d step(s) to exit
    ",step);
    27         flag = 1;
    28         return ;
    29     }
    30     if (vis[row][col]!=-1)
    31     {
    32         printf("%d step(s) before a loop of %d step(s)
    ",vis[row][col],step-vis[row][col]);
    33         return ;
    34     }
    35     vis[row][col] = step;
    36     if (map[row][col] == 'S')
    37         row++;
    38     else if (map[row][col] == 'E')
    39         col++;
    40     else if (map[row][col] == 'N')
    41         row--;
    42     else if (map[row][col] == 'W')
    43         col--;
    44     dfs(row,col,step+1);
    45 }
    46 
    47 
    48 int main()
    49 {
    50 #ifndef ONLINE_JUDGE
    51     freopen("../in.txt","r",stdin);
    52 #endif
    53     while (~scanf("%d%d",&n,&m)){
    54         memset(vis,-1, sizeof(vis));
    55         flag = 0;
    56         if (n == 0 && m == 0)
    57             break;
    58         scanf("%d",&k);
    59         for (int i=0;i<n;i++)
    60         {
    61             for (int j=0;j<m;j++)
    62                 cin >> map[i][j];
    63         }
    64         dfs(0,k-1,0);
    65     }
    66     return 0;
    67 
    68 }
  • 相关阅读:
    Pyhon数据分析20——matplotlib可视化(二)之柱状图
    程序运行正常,数据库没反应
    Redis在Linux环境下安装的常见错误
    1.1-1.4 sqoop概述及安装cdh版hadoop
    3、css边框以及其他常用样式
    3.15-3.21 hive项目实战
    2、css的存在形式及优先级
    1、css选择器
    3.11-3.14 Hive 企业使用优化2
    3.7-3.10 Hive 企业使用优化1
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11221130.html
Copyright © 2011-2022 走看看