zoukankan      html  css  js  c++  java
  • HDOJ1035 搜索模拟问题[深搜]

    Robot Motion

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3528    Accepted Submission(s): 1644


    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)
     
    Source
     
     
    明明是一道模拟题,在搜索专题里面,于是乎把函数名也写成了dfs,其实和搜索没什么关系,主要是模拟,递归就好。才几天没写代码,就水的一b,这种水题做的慢死。
     

    code:

     1 #include<iostream>
     2 using namespace std;
     3 
     4 char map[12][12];
     5 int vst[12][12];
     6 int row,col,loc;
     7 int cnt; 
     8 
     9 void dfs(int r,int c,int step)
    10 {    
    11     if(r<0||r>=row||c<0||c>=col)            //边界 
    12     {
    13         printf("%d step(s) to exit\n",step);
    14            return;
    15     }
    16     if(vst[r][c])                          //回路 
    17     {
    18         printf("%d step(s) before a loop of %d step(s)\n",vst[r][c]-1,step-vst[r][c]+1);
    19         return;
    20     }
    21     cnt++;
    22     vst[r][c]=cnt;
    23     if(map[r][c]=='N')
    24        dfs(r-1,c,step+1);
    25     else if(map[r][c]=='S')
    26        dfs(r+1,c,step+1);
    27     else if(map[r][c]=='E')
    28        dfs(r,c+1,step+1);
    29     else
    30        dfs(r,c-1,step+1);
    31     return;
    32 } 
    33 
    34 int main()
    35 {    
    36     int i,j;
    37     while(~scanf("%d%d%d",&row,&col,&loc)&&(row||col))
    38     {
    39        cnt=0;
    40        memset(vst,0,sizeof(vst));
    41        for(i=0;i<row;i++)
    42           scanf("%s",map[i]);
    43        dfs(0,loc-1,0);
    44     }
    45     return 0;
    46 }






                If you have any questions about this article, welcome to leave a message on the message board.



    Brad(Bowen) Xu
    E-Mail : maxxbw1992@gmail.com


  • 相关阅读:
    2020春,不一样的学期不一样的软工实践
    尘埃落下,我抓住透明的阳光,温暖留在掌心
    敏捷软工
    《构建之法》& CI/CD调研
    2021年-软件工程-热身阅读作业
    从学生到科技工作者
    希望我能帮到你:给同学们软件开发的建议
    CC2020 分享信息
    【计算机教育】看《计算机科学导论》,发展计算思维能力
    【计算机教育】创新工程实践课程的反馈
  • 原文地址:https://www.cnblogs.com/XBWer/p/2574069.html
Copyright © 2011-2022 走看看