zoukankan      html  css  js  c++  java
  • HDU_1240——三维空间DFS

    Problem Description
    You're in space.
    You want to get home.
    There are asteroids.
    You don't want to hit them.
     
    Input
    Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

    A single data set has 5 components:

    Start line - A single line, "START N", where 1 <= N <= 10.

    Slice list - A series of N slices. Each slice is an N x N matrix representing a horizontal slice through the asteroid field. Each position in the matrix will be one of two values:

    'O' - (the letter "oh") Empty space

    'X' - (upper-case) Asteroid present

    Starting Position - A single line, "A B C", denoting the <A,B,C> coordinates of your craft's starting position. The coordinate values will be integers separated by individual spaces.

    Target Position - A single line, "D E F", denoting the <D,E,F> coordinates of your target's position. The coordinate values will be integers separated by individual spaces.

    End line - A single line, "END"

    The origin of the coordinate system is <0,0,0>. Therefore, each component of each coordinate vector will be an integer between 0 and N-1, inclusive.

    The first coordinate in a set indicates the column. Left column = 0.

    The second coordinate in a set indicates the row. Top row = 0.

    The third coordinate in a set indicates the slice. First slice = 0.

    Both the Starting Position and the Target Position will be in empty space.

     
    Output
    For each data set, there will be exactly one output set, and there will be no blank lines separating output sets.

    A single output set consists of a single line. If a route exists, the line will be in the format "X Y", where X is the same as N from the corresponding input data set and Y is the least number of moves necessary to get your ship from the starting position to the target position. If there is no route from the starting position to the target position, the line will be "NO ROUTE" instead.

    A move can only be in one of the six basic directions: up, down, left, right, forward, back. Phrased more precisely, a move will either increment or decrement a single component of your current position vector by 1.

     
    Sample Input
    START 1 O 0 0 0 0 0 0 END START 3 XXX XXX XXX OOO OOO OOO XXX XXX XXX 0 0 1 2 2 1 END START 5 OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO XXXXX XXXXX XXXXX XXXXX XXXXX OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO 0 0 0 4 4 4 END
     
    Sample Output
    1 0 3 4 NO ROUTE
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define MAX 11
     4 //const int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
     5 int N,Sx,Sy,Sz,Ex,Ey,Ez,min;
     6 char map[MAX][MAX][MAX],str[MAX];
     7 void dfs(int,int,int,int);
     8 int main()
     9 {
    10    int i,j,k,Step;
    11    while(scanf("%s",str)&&strcmp(str,"START")==0)
    12       {
    13          scanf("%d",&N);
    14          Step=0;min=10086;
    15          memset(map,0,sizeof(map));
    16          for(i=0;i<N;i++)
    17             for(j=0;j<N;j++)
    18                scanf("%s",map[i][j]);
    19          fflush(stdin);//fflushall();清空输入或清空所有的 
    20          scanf("%d %d %d",&Sx,&Sy,&Sz);
    21          scanf("%d %d %d",&Ex,&Ey,&Ez);
    22          scanf("%s",str);
    23          if(strcmp(str,"END")==0)   printf("YES\n");;
    24          /*   
    25          printf("\n\n");
    26          for(i=0;i<N;i++)
    27             {
    28                for(j=0;j<N;j++)
    29                   {
    30                      for(k=0;k<N;k++)
    31                         printf("%c",map[i][j][k]);
    32                      printf("\n");
    33                   }
    34             }
    35          */
    36          //printf("%d,%d,%d\n",Sx,Sy,Sz);
    37          //printf("%d,%d,%d\n",Ex,Ey,Ez);
    38          dfs(Sx,Sy,Sz,Step);
    39          if(min==10086)
    40             printf("NO ROUTE");
    41          else
    42             printf("%d %d",N,min);
    43       }
    44    return 0;
    45 }
    46 void dfs(int x,int y,int z,int Step)
    47 {
    48    map[z][y][x]='@';
    49    if(x==Ex&&y==Ey&&z==Ez)
    50       {
    51          if(min>Step)
    52             min=Step;
    53       }
    54    if(x+1<N&&map[z][y][x+1]=='O')     dfs(x+1,y,z,Step+1);
    55    if(x-1>=0&&map[z][y][x-1]=='O')    dfs(x-1,y,z,Step+1);
    56    if(y+1<N&&map[z][y+1][x]=='O')     dfs(x,y+1,z,Step+1);
    57    if(y-1>=0&&map[z][y-1][x]=='O')    dfs(x,y-1,z,Step+1);
    58    if(z+1<N&&map[z+1][y][x]=='O')     dfs(x,y,z+1,Step+1);
    59    if(z-1>=0&&map[z-1][y][x]=='O')    dfs(x,y,z-1,Step+1);
    60    map[z][y][x]='O';
    61 }
    ——现在的努力是为了小时候吹过的牛B!!
  • 相关阅读:
    Python——方法
    Python——类和对象(二)
    Python——类和对象(一)
    Python——函数的高级应用
    Python——函数入门(三)
    Python——函数入门(二)
    Python——函数入门(一)
    Python——序列封包与序列解包
    min(T)方法
    max(T)方法
  • 原文地址:https://www.cnblogs.com/pingge/p/3115976.html
Copyright © 2011-2022 走看看