ACDream 1191 - Dragon Maze

来源:互联网 发布:linux 时区 编辑:程序博客网 时间:2024/05/22 06:18

题意

给一个图,每个点有能量,求出在能走到终点的情况下的最大能量,或者走不到。

思路

用优先队列,这样可以保证每一次取出来的都是当前最小步数的最大能量!

重载结构体的小于号竟然无效?(求原因),只能写一个cmp函数。

代码

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <stack>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <queue>
  9. #include <cstring>
  10. #include <string>
  11. #include <sstream>
  12. #include <map>
  13. #include <cmath>
  14. #define LL long long
  15. #define lowbit(x) ((x) & (-x))
  16. #define MP(a, b) make_pair(a, b)
  17. const int MAXN = 1000 + 5;
  18. const int INF = 0x3f3f3f3f;
  19. using namespace std;
  20. typedef pair<int, int> pii;
  21. typedef vector<int>::iterator viti;
  22. typedef vector<pii>::iterator vitii;
  23. struct POINT
  24. {
  25. int x, y, eng, steps;
  26. POINT()
  27. {
  28. x = y = eng = steps = 0;
  29. }
  30. POINT(int xx, int yy): x(xx), y(yy), eng(0), steps(0){}
  31. };
  32. struct cmp
  33. {
  34. bool operator() (const POINT &a, const POINT &b)
  35. {
  36. if (a.steps == b.steps) return a.eng < b.eng;
  37. else return a.steps > b.steps;
  38. }
  39. };
  40. priority_queue<POINT, vector<POINT>, cmp>pqu;
  41. int mp[MAXN][MAXN], vis[MAXN][MAXN], row, col;
  42. const int dir[][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
  43. POINT ed;
  44. POINT BFS(POINT st)
  45. {
  46. while (!pqu.empty())
  47. pqu.pop();
  48. memset(vis, 0, sizeof vis);
  49. pqu.push(st);
  50. while (!pqu.empty())
  51. {
  52. POINT cur = pqu.top(); pqu.pop();
  53. for (int i = 0; i < 4; i++)
  54. {
  55. int xx = cur.x + dir[i][0], yy = cur.y + dir[i][1];
  56. if (!vis[xx][yy] && mp[xx][yy] != -1 && xx >= 0 && xx < row && yy >= 0 && yy < col)
  57. {
  58. vis[xx][yy] = 1;
  59. POINT tmp(xx, yy);
  60. tmp.eng = cur.eng + mp[xx][yy]; tmp.steps = cur.steps + 1;
  61. if (xx == ed.x && yy == ed.y)
  62. return tmp;
  63. pqu.push(tmp);
  64. }
  65. }
  66. }
  67. POINT fal;
  68. return fal;
  69. }
  70. int main()
  71. {
  72. //freopen("input.txt", "r", stdin);
  73. int T, i, j, cases = 0;
  74. scanf("%d", &T);
  75. while (T--)
  76. {
  77. POINT st;
  78. scanf("%d%d", &row, &col);
  79. scanf("%d%d%d%d", &st.x, &st.y, &ed.x, &ed.y);
  80. for (i = 0; i < row; i++)
  81. for (j = 0; j < col; j++) scanf("%d", &mp[i][j]);
  82. st.eng = mp[st.x][st.y];
  83. POINT ans = BFS(st);
  84. printf("Case #%d: ", ++cases);
  85. if (ans.steps != 0)
  86. printf("%d\n", ans.eng);
  87. else
  88. puts("Mission Impossible.");
  89. }
  90. return 0;
  91. }
0 0
原创粉丝点击