ACM 搜索 hdu1240 Asteroids!

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

这是一道典型的BFS题目,这次的迷宫是一个三维空间!O为空地,X为墙壁。 可移动方向从4个变成了6个(多出了上下)仅此而已。 求从出发点到终点的步数。

  1. #include <string.h>  
  2. #include <stdio.h>  
  3. #include <queue>  
  4. using namespace std;  
  5.   
  6. char map[20][20][20];  
  7. int vis[20][20][20];  
  8. int n;  
  9. int sx,sy,sz;  
  10. int ex,ey,ez;  
  11. int tx[] = {1,-1,0,0,0,0};  
  12. int ty[] = {0,0,1,-1,0,0};  
  13. int tz[] = {0,0,0,0,1,-1};  
  14.   
  15. struct node  
  16. {  
  17.     int x,y,z,step;  
  18. };  
  19.   
  20. int check(int x,int y,int z)  
  21. {  
  22.     if(x<0 || y<0 || z<0 || x>=n || y>=n || z>=n || vis[x][y][z] )  
  23.     return 0;  
  24.     return 1;  
  25. }  
  26.   
  27. int bfs(int x,int y,int z)  
  28. {  
  29.     int i;  
  30.     queue<node> Q;  
  31.     node a,next;  
  32.     a.x = x;  
  33.     a.y = y;  
  34.     a.z = z;  
  35.     a.step = 0;  
  36.     vis[x][y][z] = 1;  
  37.     Q.push(a);  
  38.     while(!Q.empty())  
  39.     {  
  40.         a = Q.front();  
  41.         Q.pop();  
  42.         if(a.x == ex && a.y == ey && a.z == ez)  
  43.         return a.step;  
  44.         for(i = 0;i<6;i++)  
  45.         {  
  46.             next = a;  
  47.             next.x+=tx[i];  
  48.             next.y+=ty[i];  
  49.             next.z+=tz[i];  
  50.             if(check(next.x,next.y,next.z))  
  51.             {  
  52.                 next.step++;  
  53.                 vis[next.x][next.y][next.z] = 1;  
  54.                 Q.push(next);  
  55.             }  
  56.         }  
  57.     }  
  58.     return -1;  
  59. }  
  60.   
  61. int main()  
  62. {  
  63.     char s[10];  
  64.     int i,j,k;  
  65.     while(~scanf("%s%d",s,&n))  
  66.     {  
  67.         for(i = 0;i<n;i++)  
  68.         for(j = 0;j<n;j++)  
  69.         scanf("%s",map[i][j]);  
  70.         scanf("%d%d%d%d%d%d",&sx,&sy,&sz,&ex,&ey,&ez);  
  71.         scanf("%s",s);  
  72.         int ans = bfs(sx,sy,sz);  
  73.         if(ans>=0)  
  74.         printf("%d %d\n",n,ans);  
  75.         else  
  76.         printf("NO ROUTE\n");  
  77.     }  
  78.   
  79.     return 0;  
  80. }  

你可能感兴趣的:(ACM 搜索 hdu1240 Asteroids!)