当前位置:   article > 正文

地下城夺宝游戏——杭电OJ 1044题解析_杭电oj1044

杭电oj1044

题目来源:杭电OJ-1044

题目大意:一个探险家身处一个危险的地下城,城中很危险,并且城中分散着若干个珠宝;现在地下城即将塌陷,冒险家需要在有限的时间内逃出去,但他希望在逃生的过程中获取一些珠宝并使价值最大化。题目要求用程序计算出最优方案所能获取的珠宝价值。


以前也遇到过相似的题目,但一直未真正搞明白,最近再次遇到,阅读了一些大牛的博客,结合自己的理解,将这个知识点记录一下。

这道题之所以不能简单的使用DFS或BFS算法解决,是因为探险家为了获取更多价值,可以走重复路径。最近学习并实现了这道题的两种解决思路,一种是BFS Plus,另一种是BFS+DFS。


一.BFS Plus方案

经典的BFS算法用一个二维数组保存地图的访问状况(可阅读我上一篇转载的文章,讲解的很深刻),而这一题的不同在于每次获取一个珠宝后,可以走重复路径,为了解决这个问题,我们可以在每次获取一个珠宝之后用一个新的二位数组保存后面遍历的访问情况。本方案使用二进制中的一位来标识某个珠宝是否获得,已获得取1,否则取0,这样就可以用一个值来表示珠宝的获取状态,比如说有四个珠宝A,B,C,D,那么我们可以用四位二进制数来表示获取状态,如果已经获取A和D,那么状态值就为9(二进制形式为1001)。本方案就是使用这个状态值来表示不同的二位访问状态数组,所以总体来说需要一个三维的数组来记录访问信息。而本题中最多有10个珠宝,所以需要十位二进制数来标识珠宝的访问状态,故状态数组的第一维为1024。

接下来给出该方案的源代码:

  1. #include <iostream>
  2. #include <queue>
  3. #include <algorithm>
  4. #include <string.h>
  5. using namespace std;
  6. int W, H, L, M;
  7. char dun[55][55]; //保存地下城地图
  8. int jew[10]; //保存每个珠宝的价值
  9. bool isVisited[1024][55][55]; //记录BFS过程中某个位置是否经过
  10. //结构体保存每到一个位置时的信息
  11. struct State
  12. {
  13. int J, x, y;
  14. int jews, time;
  15. };
  16. int dx[] = { -1, 0, 1, 0 };
  17. int dy[] = { 0, 1, 0, -1 };
  18. int CollectJewels()
  19. {
  20. memset(isVisited, 0, sizeof(isVisited));
  21. State begin, pos, nextpos;
  22. for (int i = 0; i < H; i++)
  23. {
  24. int j;
  25. for (j = 0; j < W; j++)
  26. if (dun[i][j] == '@')
  27. {
  28. begin.x = i;
  29. begin.y = j;
  30. break;
  31. }
  32. if (j != W) break;
  33. }
  34. begin.J = begin.jews = begin.time = 0;
  35. isVisited[0][begin.x][begin.y] = true;
  36. queue<State> Q;
  37. Q.push(begin);
  38. int res = -1;
  39. while (!Q.empty())
  40. {
  41. pos = Q.front();
  42. Q.pop();
  43. if (pos.time == L) break; //优化操作,如果当前位置的时间已经达到
  44. //L,那么队列后面位置的时间一定>=L,可终止遍历
  45. int i;
  46. for (i = 0; i < 4; i++)
  47. {
  48. nextpos = pos;
  49. nextpos.x += dx[i];
  50. nextpos.y += dy[i];
  51. nextpos.time++;
  52. if(nextpos.x<0 || nextpos.x>=H ||
  53. nextpos.y<0 || nextpos.y>=W) continue;
  54. char ch = dun[nextpos.x][nextpos.y];
  55. if (ch == '*' || isVisited[nextpos.J][nextpos.x][nextpos.y])
  56. continue;
  57. isVisited[nextpos.J][nextpos.x][nextpos.y] = true;
  58. if (ch >= 'A' && ch <= 'J')
  59. {
  60. int t = ch - 'A';
  61. //如果当前位置的珠宝还在,取之
  62. if (!(nextpos.J & (1 << t)))
  63. {
  64. nextpos.jews += jew[t];
  65. nextpos.J |= (1 << t);
  66. isVisited[nextpos.J][nextpos.x][nextpos.y] = true;
  67. }
  68. }
  69. else if (ch == '<')
  70. {
  71. res = max(res, nextpos.jews);
  72. //优化操作,如果某种方案可以取得城内所有珠宝,则可以停止
  73. //尝试其他方案
  74. if (nextpos.J == ((1 << M) - 1)) break;
  75. else continue;
  76. }
  77. Q.push(nextpos);
  78. }
  79. if(i!=4) break;
  80. }
  81. return res;
  82. }
  83. int main() {
  84. int T;
  85. cin >> T;
  86. for (int i = 1; i <= T; i++)
  87. {
  88. cin >> W >> H >> L >> M;
  89. for (int j = 0; j < M; j++)
  90. cin >> jew[j];
  91. for (int j = 0; j < H; j++)
  92. cin >> dun[j];
  93. int res = CollectJewels();
  94. cout << "Case " << i << ":\n";
  95. if (res == -1) cout << "Impossible\n";
  96. else cout << "The best score is " << res << ".\n";
  97. if (i != T) cout << endl;
  98. }
  99. return 0;
  100. }

二.BFS+DFS方案
本方案先进行BFS操作,计算出每个珠宝以及起点和出口两两之间的距离(也即时间),然后将这些位置作为结点,将距离作为边的权值,构造出一个虚拟图;接下来使用DFS算法计算出图中从起点到出口的最优路径。这种方案相对于上一方案来说更容易理解。
接下来给出该方案的源代码:
  1. #include <iostream>
  2. #include <queue>
  3. #include <string.h>
  4. using namespace std;
  5. int W, H, L, M, ans, sum;
  6. char dun[55][55]; //保存地下城地图信息
  7. int jew[15]; //保存每个珠宝的价值
  8. bool isVisited[55][55]; //记录BFS过程中地图的访问状态
  9. int Graph[15][15]; //保存BFS过程中构造的虚拟图的信息
  10. bool isUsed[15]; //记录DFS过程中图中节点的访问状态
  11. struct State
  12. {
  13. int x, y;
  14. int time;
  15. State(int a, int b, int c) :x(a), y(b), time(c){}
  16. };
  17. int dx[] = { -1, 0, 1, 0 };
  18. int dy[] = { 0, 1, 0, -1 };
  19. void BFS(int x, int y)
  20. {
  21. memset(isVisited, 0, sizeof(isVisited));
  22. char ch = dun[x][y];
  23. int u;
  24. if(ch=='@') u=0;
  25. else if (ch >= 'A') u = ch - 'A' + 1;
  26. State pos(0, 0, 0), nextpos(0, 0, 0);
  27. queue<State> Q;
  28. isVisited[x][y] = true;
  29. Q.push(State(x, y, 0));
  30. while (!Q.empty())
  31. {
  32. pos = Q.front();
  33. Q.pop();
  34. if (pos.time == L) break; //优化操作
  35. for (int i = 0; i < 4; i++)
  36. {
  37. nextpos.x = pos.x + dx[i];
  38. nextpos.y = pos.y + dy[i];
  39. nextpos.time = pos.time + 1;
  40. if (nextpos.x < 0 || nextpos.x >= H ||
  41. nextpos.y < 0 || nextpos.y >= W) continue;
  42. if (dun[nextpos.x][nextpos.y] == '*' || isVisited[nextpos.x][nextpos.y])
  43. continue;
  44. isVisited[nextpos.x][nextpos.y] = true;
  45. char ch = dun[nextpos.x][nextpos.y];
  46. if (ch == '@')
  47. {
  48. Graph[u][0] = Graph[0][u] = nextpos.time;
  49. }
  50. else if (ch >= 'A' && ch <= 'J')
  51. {
  52. int v = ch - 'A' + 1;
  53. Graph[u][v] = Graph[v][u] = nextpos.time;
  54. }
  55. else if (ch == '<')
  56. {
  57. Graph[u][M + 1] = Graph[M + 1][u] = nextpos.time;
  58. }
  59. Q.push(nextpos);
  60. }
  61. }
  62. }
  63. void DFS(int pos, int Time, int je)
  64. {
  65. if (Time > L) return;
  66. if(ans==sum) return; //剪枝操作
  67. if (pos == M + 1)
  68. {
  69. if (je > ans) ans = je;
  70. return;
  71. }
  72. for (int i = 1; i < M + 2; i++)
  73. {
  74. if (isUsed[i] || Graph[pos][i] == 0) continue;
  75. isUsed[i] = true;
  76. DFS(i, Time + Graph[pos][i], je + jew[i]);
  77. isUsed[i] = false;
  78. }
  79. }
  80. int main()
  81. {
  82. int T;
  83. cin >> T;
  84. for (int i = 1; i <= T; i++)
  85. {
  86. cin >> W >> H >> L >> M;
  87. sum=0;
  88. for (int j = 1; j <= M; j++)
  89. {
  90. cin >> jew[j];
  91. sum+=jew[j];
  92. }
  93. //把起始位置和出口记作价值为0的位置,分别用0和M+1标识
  94. jew[0]=jew[M+1]=0;
  95. for (int i = 0; i < H; i++)
  96. cin >> dun[i];
  97. memset(Graph,0,sizeof(Graph));
  98. //BFS过程中构造虚图
  99. for (int i = 0; i < H;i++)
  100. for (int j = 0; j < W; j++)
  101. {
  102. if (dun[i][j] == '@' || (dun[i][j] >= 'A' && dun[i][j] <= 'J'))
  103. BFS(i, j);
  104. }
  105. memset(isUsed, 0, sizeof(isUsed));
  106. isUsed[0]=true;
  107. ans = -1;
  108. //DFS在途中寻找一条最优路径
  109. DFS(0, 0, 0);
  110. cout<<"Case "<<i<<":\n";
  111. if (ans == -1) cout << "Impossible\n";
  112. else cout << "The best score is " << ans << ".\n";
  113. if (i != T) cout << endl;
  114. }
  115. return 0;
  116. }

两方案比较:可以很容易看出,方案一占用的空间较大,效率方面方案一在杭电OJ上运行,耗时390MS,占用空间5784K;而方案二耗时46MS,占用空间1624K。总的来说方案二更胜一筹。



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/645492
推荐阅读
相关标签
  

闽ICP备14008679号