Asteroids!

我不是女神ヾ 2022-06-12 03:15 181阅读 0赞

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 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 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

  1. START 1
  2. O
  3. 0 0 0
  4. 0 0 0
  5. END
  6. START 3
  7. XXX
  8. XXX
  9. XXX
  10. OOO
  11. OOO
  12. OOO
  13. XXX
  14. XXX
  15. XXX
  16. 0 0 1
  17. 2 2 1
  18. END
  19. START 5
  20. OOOOO
  21. OOOOO
  22. OOOOO
  23. OOOOO
  24. OOOOO
  25. OOOOO
  26. OOOOO
  27. OOOOO
  28. OOOOO
  29. OOOOO
  30. XXXXX
  31. XXXXX
  32. XXXXX
  33. XXXXX
  34. XXXXX
  35. OOOOO
  36. OOOOO
  37. OOOOO
  38. OOOOO
  39. OOOOO
  40. OOOOO
  41. OOOOO
  42. OOOOO
  43. OOOOO
  44. OOOOO
  45. 0 0 0
  46. 4 4 4
  47. END

Sample Output

  1. 1 0
  2. 3 4
  3. NO ROUTE
  4. 题意:一个三维空间地图,求起点到终点最少的步数
  5. #include<iostream>
  6. #include<cstdio>
  7. #include<cstring>
  8. #include<string>
  9. #include<queue>
  10. #include<algorithm>
  11. using namespace std;
  12. int n,sx,sy,sz,ex,ey,ez;
  13. char map1[11][11][11];
  14. int vis[11][11][11];
  15. int dx[]= {-1,0,1,0,0,0};
  16. int dy[]= {0,0,0,0,1,-1};
  17. int dz[]= {0,1,0,-1,0,0};
  18. struct A
  19. {
  20. int x,y,z,step;
  21. };
  22. int bfs(int x,int y,int z)
  23. {
  24. queue<A> Q;
  25. A f,g;
  26. f.x=x;
  27. f.y=y;
  28. f.z=z;
  29. f.step=0;
  30. vis[x][y][z]=1;
  31. Q.push(f);
  32. while(!Q.empty())
  33. {
  34. f=Q.front();
  35. Q.pop();
  36. if(f.x==ex&&f.y==ey&&f.z==ez)
  37. {
  38. return f.step;
  39. }
  40. for(int i=0; i< 6; i++)
  41. {
  42. g.step=f.step;
  43. g.x=f.x+dx[i];
  44. g.y=f.y+dy[i];
  45. g.z=f.z+dz[i];
  46. if(g.x>=0&&g.y>=0&&g.z>=0&&g.x<n&&g.y<n&&g.z<n&&!vis[g.x][g.y][g.z]&map1[g.x][g.y][g.z]!='X')
  47. {
  48. vis[g.x][g.y][g.z]=1;
  49. g.step=f.step+1;
  50. Q.push(g);
  51. }
  52. }
  53. }
  54. return -1;
  55. }
  56. int main()
  57. {
  58. char str[10]; //int n;和全局变量冲突了
  59. while(~scanf("%s %d",str,&n))
  60. {
  61. memset(vis,0,sizeof(vis));
  62. for(int i=0; i<n; i++)
  63. for(int j=0; j<n; j++)
  64. {
  65. getchar();
  66. for(int l=0; l<n; l++)
  67. scanf("%c",&map1[j][l][i]);
  68. }
  69. scanf("%d %d %d %d %d %d",&sx,&sy,&sz,&ex,&ey,&ez);
  70. scanf("%s",str);
  71. int ans=bfs(sx,sy,sz);
  72. if(ans>=0)
  73. printf("%d %d\n",n,ans);
  74. else
  75. printf("NO ROUTE\n");
  76. }
  77. return 0;
  78. }

发表评论

表情:
评论列表 (有 0 条评论,181人围观)

还没有评论,来说两句吧...

相关阅读