zoukankan      html  css  js  c++  java
  • hdu 1035(DFS)

    Robot Motion

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


    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
    DFS简单题 
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<map>
     8 #include<set>
     9 #include<vector>
    10 #include<cstdlib>
    11 #include<string>
    12 #define eps 0.000000001
    13 typedef long long ll;
    14 typedef unsigned long long LL;
    15 using namespace std;
    16 const int N=100;
    17 char mp[N][N];
    18 int visited[N][N];
    19 int m,n;
    20 int flag;
    21 int step,loopstep;
    22 void DFS(int x,int y){
    23     if(x<0||x>=n||y<0||y>=m){
    24         return ;
    25     }
    26     if(visited[x][y]!=0){
    27         flag=1;
    28         loopstep=step-visited[x][y]+1;
    29         step=visited[x][y]-1;
    30         return ;
    31     }
    32     step++;
    33     visited[x][y]=step;
    34     if(mp[x][y]=='N')DFS(x-1,y);
    35     else if(mp[x][y]=='S')DFS(x+1,y);
    36     else if(mp[x][y]=='E')DFS(x,y+1);
    37     else if(mp[x][y]=='W')DFS(x,y-1);
    38 }
    39 int main(){
    40     while(scanf("%d%d",&n,&m)!=EOF){
    41         if(n==0&&m==0)break;
    42         int k;
    43         scanf("%d",&k);
    44         flag=0;
    45         for(int i=0;i<n;i++)scanf("%s",mp[i]);
    46         loopstep=step=0;
    47         memset(visited,0,sizeof(visited));
    48         DFS(0,k-1);
    49         if(flag==0) printf("%d step(s) to exit
    ", step);
    50         else
    51             printf("%d step(s) before a loop of %d step(s)
    ", step,loopstep);
    52 
    53     }
    54 }
     
    Sample Output
    10 step(s) to exit 3 step(s) before a loop of 8 step(s)
  • 相关阅读:
    函数指针的调用方式
    C++构造函数和析构函数顺序
    往android主项目中添加辅助项目
    Qt每次运行都是重新编译问题
    函数参数检验的研究
    动态链接库和静态链接库的区别(未完待续)
    MySQL 查看最大连接数, 当期连接数.
    Linux 命令
    Ext treelist 动态切换TreeStore
    Java 日期加减计算.
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6387721.html
Copyright © 2011-2022 走看看