HDU 1240(bfs)

红太狼 2022-09-20 11:28 239阅读 0赞
  1. 题意:给出一个三维空间,宇宙飞船的起始地点,目标地点,求最短飞行距离。
  2. #include <iostream>
  3. #include <string>
  4. #include <cstring>
  5. #include <queue>
  6. using namespace std;
  7. int N;
  8. int step[6][3] = { {1, 0 ,0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1} };
  9. char space[12][12][12];
  10. struct Point
  11. {
  12. int x, y, z, s;
  13. Point() : s(0) {}
  14. Point(int _x, int _y, int _z, int _s) : x(_x), y(_y), z(_z), s(_s) {}
  15. friend istream& operator >>(istream& is, Point& rhs)
  16. {
  17. is >> rhs.x >> rhs.y >> rhs.z;
  18. rhs.x += 1, rhs.y += 1, rhs.z += 1, rhs.s = 0;
  19. return is;
  20. }
  21. bool operator ==(const Point& rhs) const
  22. {
  23. return x == rhs.x && y == rhs.y && z == rhs.z;
  24. }
  25. Point operator +(const int S[]) const
  26. {
  27. return Point(x + S[0], y + S[1], z + S[2], s + 1);
  28. }
  29. bool legal()
  30. {
  31. return space[x][y][z] == 'O';
  32. }
  33. void visit()
  34. {
  35. space[x][y][z] = 'X';
  36. }
  37. } start, end;
  38. void BFS()
  39. {
  40. Point p, n;
  41. queue<Point> q;
  42. q.push(start);
  43. start.visit();
  44. while (!q.empty())
  45. {
  46. p = q.front();
  47. q.pop();
  48. for (int i = 0; i < 6; ++i)
  49. {
  50. n = p + step[i];
  51. if (!n.legal())
  52. continue;
  53. else if (n == end)
  54. {
  55. cout << N << " " << n.s << endl;
  56. return;
  57. }
  58. else
  59. {
  60. n.visit();
  61. q.push(n);
  62. }
  63. }
  64. }
  65. cout << "NO ROUTE" << endl;
  66. }
  67. int main()
  68. {
  69. string temp;
  70. int i, j, k;
  71. for (i = 0; i < 12; ++i)
  72. for (j = 0; j < 12; ++j)
  73. space[0][i][j] = space[i][0][j] = space[i][j][0] = 'X';
  74. while (cin >> temp >> N)
  75. {
  76. for (i = 1; i <= N; ++i)
  77. for (j = 1; j <= N; ++j)
  78. for (k = 1; k <= N; ++k)
  79. {
  80. cin >> space[j][k][i];
  81. space[j][k][i] = toupper(space[j][k][i]);
  82. }
  83. for (i = 0; i < 12; ++i)
  84. for (j = 0; j < 12; ++j)
  85. space[N+1][i][j] = space[i][N+1][j] = space[i][j][N+1] = 'X';
  86. cin >> start >> end;
  87. cin >> temp;
  88. if (!(start.legal() && end.legal()))
  89. {
  90. cout << "NO ROUTE" << endl;
  91. continue;
  92. }
  93. else if (start == end)
  94. {
  95. cout << N << " " << 0 << endl;
  96. continue;
  97. }
  98. BFS();
  99. }
  100. }

发表评论

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

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

相关阅读

    相关 HDU 1312(BFS)

    题意:“.”是red方格,“\”是black方格,“@”是起点。求从起点开始走,可以到达的方格(包括起点),red方格不能走,相当于墙壁。   include <c

    相关 HDU 1242(bfs)

    题意:一个朋友拯救angel,'a' 是angel, 'x'是士兵, 'r'是朋友,'\'是墙壁。没走一步花费一个单位的时间,遇到士兵与士兵搏斗花费一个单位的时间,求最短时间。

    相关 HDU 1240(bfs)

    题意:给出一个三维空间,宇宙飞船的起始地点,目标地点,求最短飞行距离。   include <iostream> include <str

    相关 HDU 1728(BFS)

    问题描述: 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些