HDU 1254(BFS)

快来打我* 2022-09-20 12:35 295阅读 0赞

题意:如题。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <queue>
  5. using namespace std;
  6. const int S[][3] = { {0, 1, 0}, {1, -1, 0}, {2, 0, 1}, {3, 0, -1} };
  7. char map[9][9];
  8. bool visit[9][9][4];
  9. int M, N;
  10. inline bool inMap(int x, int y)
  11. {
  12. return x >= 0 && x < M && y >= 0 && y < N;
  13. }
  14. struct P
  15. {
  16. int x, y;
  17. P() {}
  18. P(int _x, int _y) : x(_x), y(_y) {}
  19. bool operator ==(const P& rhs) const
  20. {
  21. return x == rhs.x && y == rhs.y;
  22. }
  23. friend ostream& operator <<(ostream& os, const P& rhs)
  24. {
  25. return os << "-----" << rhs.x << " " << rhs.y;
  26. }
  27. };
  28. struct Point
  29. {
  30. int x, y, s;
  31. int p_x, p_y;
  32. Point() : s(-1) {}
  33. Point(int _x, int _y) : x(_x), y(_y), s(-1) {}
  34. Point(int _x, int _y, int _s) : x(_x), y(_y), s(_s) {}
  35. Point(int _x, int _y, int px, int py, int _s) : x(_x), y(_y), s(_s), p_x(px), p_y(py) {}
  36. void set(int _x, int _y, int _s)
  37. {
  38. x = _x, y = _y, s = _s;
  39. }
  40. bool operator ==(const Point& rhs) const
  41. {
  42. return x == rhs.x && y == rhs.y;
  43. }
  44. bool personCanGo(int bx, int by) const
  45. {
  46. if (bx == p_x && by == p_y) return true;
  47. //static bool flag;
  48. static int i, j;
  49. static P p, n;
  50. static int v[9][9];
  51. static queue<P> q1, q2;
  52. while (!q1.empty()) q1.pop();
  53. //while (!q2.empty()) q2.pop();
  54. for (i = 0; i < M; ++i)
  55. for (j = 0; j < N; ++j) v[i][j] = 0;
  56. //cout << p_x << " ..... " << p_y << endl;
  57. q1.push(P(p_x, p_y));//, q2.push(P(bx, by));
  58. v[p_x][p_y] = 1;//, v[bx][by] = 2;
  59. while (!q1.empty())
  60. {
  61. p = q1.front(), q1.pop();
  62. //cout << "***" << p << endl;
  63. for (i = 0; i < 4; ++i)
  64. {
  65. n = P(p.x + S[i][1], p.y + S[i][2]);
  66. // cout << n << endl;
  67. if (inMap(n.x, n.y) && map[n.x][n.y] == '0' && !(n.x == x && n.y == y))
  68. {
  69. if (v[n.x][n.y])
  70. continue;
  71. if (n.x == bx && n.y == by)
  72. return true;
  73. v[n.x][n.y] = 1;
  74. /*
  75. for (j = 0; j < N; ++j) printf("%d",v[i][j]);
  76. for (i = 0; i < M; ++i){
  77. puts("");
  78. }
  79. puts("");
  80. */
  81. // cout << "push" << n << endl;
  82. q1.push(n);
  83. }
  84. }
  85. }
  86. return false;
  87. }
  88. bool newPoint(int i, Point& rhs) const
  89. {
  90. int nx = x + S[i][1], ny = y + S[i][2];
  91. int bx = x - S[i][1], by = y - S[i][2];
  92. if (!(inMap(nx, ny) && inMap(bx, by)))
  93. return false;
  94. if (map[nx][ny] == '1' || visit[nx][ny][i] || map[bx][by] == '1')
  95. return false;
  96. if (!personCanGo(bx, by))
  97. return false;
  98. visit[nx][ny][i] = true;
  99. rhs = Point(nx, ny, x, y, s + 1);
  100. return true;
  101. }
  102. } start, target;
  103. int BFS(int M, int N)
  104. {
  105. queue<Point> q;
  106. Point p, n;
  107. memset(visit, false, sizeof visit);
  108. q.push(start);
  109. while (!q.empty())
  110. {
  111. p = q.front(), q.pop();
  112. if (p == target)
  113. return p.s;
  114. for (int i = 0; i < 4; ++i)
  115. if (p.newPoint(i, n))
  116. {
  117. //printf("(%d, %d) ---> (%d, %d)\n", p.x, p.y, n.x, n.y);
  118. q.push(n);
  119. }
  120. }
  121. return -1;
  122. }
  123. int main()
  124. {
  125. int T, i, j;
  126. scanf("%d", &T);
  127. while (T--)
  128. {
  129. scanf("%d%d", &M, &N);
  130. for (i = 0; i < M; ++i)
  131. {
  132. //scanf("%s", map[i]);
  133. for (j = 0; j < N; ++j)
  134. {
  135. scanf(" %c", &map[i][j]);
  136. if (map[i][j] == '2')
  137. start.set(i, j, 0), map[i][j] = '0';
  138. else if (map[i][j] == '3')
  139. target = Point(i, j), map[i][j] = '0';
  140. else if (map[i][j] == '4')
  141. start.p_x = i, start.p_y = j, map[i][j] = '0';
  142. }
  143. }
  144. //for (i = 0; i < M; ++i)
  145. // for (j = 0; j < N; ++j)
  146. // putchar(map[i][j]);
  147. printf("%d\n", BFS(M, N));
  148. }
  149. return 0;
  150. }

发表评论

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

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

相关阅读

    相关 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可以穿越,有些