hdu 1044 Collect More Jewels

并没有做过这种类型的题目,不太会做...看了一下大神的代码,然后自己敲...额..思路一样了,代码也差不多..

http://www.cnblogs.com/kuangbin/archive/2012/08/14/2637512.html

先通过BFS预处理任意两点之间的距离,然后通过DFS暴力模拟路径,算出最优解.

感觉自己可能对BFS理解比DFS更深一点,或者说,BFS比较简单一点吧...

这题还有一种解法是状态压缩+BFS...通过开设一个int型变量记录是否拿到宝物,然后暴力..不过这种方法一不小心就超时了..需要注意一些优化的细节;

例如: 0000000000 | 0000000001 ,这样就拿到第一个宝箱...解法和HDU 1429 是相似的.

  1 #include <iostream>
  2 #include <string.h>
  3 #include <queue>
  4 #define MAXN 55
  5 int T, H, W, L, M,Sum,ans,Case;
  6 int dir[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };
  7 int dis[12][12];
  8 int value[12];
  9 bool visit_bfs[MAXN][MAXN];
 10 bool visit_dfs[MAXN];
 11 char map[MAXN][MAXN];
 12 struct node{
 13     int x, y;
 14     int step;
 15 };
 16 using namespace std;
 17 bool check(int x, int y)
 18 {
 19     if (x<0 || x>=W  || y<0 || y>=H  || map[x][y] == ‘*‘)
 20         return false;
 21     return true;
 22 }
 23 int bfs(int x,int y,int begin)
 24 {
 25     node cur, next;
 26     queue<node> game;
 27     memset(visit_bfs, false, sizeof(visit_bfs));
 28     cur.x = x, cur.y = y, cur.step = 0;
 29     visit_bfs[x][y] = true;
 30     game.push(cur);
 31     while (!game.empty())
 32     {
 33         cur = game.front();
 34         game.pop();
 35         for (int i = 0; i < 4; i++)
 36         {
 37             next.x = cur.x + dir[i][0];
 38             next.y = cur.y + dir[i][1];
 39             if (!check(next.x, next.y) || visit_bfs[next.x][next.y])
 40                 continue;
 41         //    cout << next.x << " " << next.y << endl;
 42             visit_bfs[next.x][next.y] = true;
 43             next.step = cur.step + 1;
 44             if (map[next.x][next.y] == ‘@‘){
 45                 dis[begin][0] = next.step;
 46             }
 47             else if (map[next.x][next.y] == ‘<‘){
 48                 dis[begin][M+1] = next.step;
 49             }
 50             else if (map[next.x][next.y] >= ‘A‘ && map[next.x][next.y] <= ‘Z‘)
 51             {
 52                 dis[begin][map[next.x][next.y] - ‘A‘ + 1] = next.step;
 53             }
 54             game.push(next);
 55         }
 56     }
 57     return 0;
 58 }
 59 void dfs(int cur,int score,int time)
 60 {
 61     if (ans == Sum)
 62         return;
 63     if (time > L)
 64         return;
 65     if (cur > M){
 66         if (score > ans)
 67             ans = score;
 68     }
 69     for (int i = 0; i <= M+1; i++)
 70     {
 71         if (!visit_dfs[i]&&dis[cur][i]){
 72             visit_dfs[i] = true;
 73             dfs(i, score + value[i], time + dis[cur][i]);
 74             visit_dfs[i] = false;
 75         }
 76     }
 77 }
 78 int main()
 79 {
 80     //freopen("test.txt", "r", stdin);
 81     Case = 1;
 82     scanf("%d", &T);
 83     while (T--)
 84     {
 85         scanf("%d%d%d%d", &H, &W, &L, &M);
 86         Sum = 0;
 87         for (int i = 1; i <= M; i++){
 88             scanf("%d", value + i);
 89             Sum += value[i];
 90         }
 91         value[0] = value[M+1] = 0;
 92         memset(visit_dfs, false, sizeof(visit_dfs));
 93         memset(dis, 0, sizeof(dis));
 94         for (int i = 0; i < W; i++)
 95             scanf("%s", map + i);
 96         for (int i = 0; i < W;i++)
 97         for (int j = 0; j < H; j++)
 98         {
 99             if (map[i][j] == ‘@‘)
100                 bfs(i,j,0);
101             else if (map[i][j] == ‘<‘)
102                 bfs(i,j,M+1);
103             else if (map[i][j] >= ‘A‘ && map[i][j] <= ‘J‘)
104                 bfs(i,j,map[i][j]-‘A‘+1);
105         }
106         ans = -1;
107         dfs(0, 0, 0);
108         if (Case != 1)
109             puts("");
110         printf("Case %d:\n", Case++);
111         if (ans == -1){
112             puts("Impossible");
113         }
114         else{
115             printf("The best score is %d.\n", ans);
116         }
117     }
118     return 0;
119 }

时间: 2024-08-29 03:02:43

hdu 1044 Collect More Jewels的相关文章

HDU 1044 Collect More Jewels 【经典BFS+DFS】

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6597    Accepted Submission(s): 1527 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

HDU 1044 Collect More Jewels【BFS+DFS+建立距离图】

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6707    Accepted Submission(s): 1556 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

hdu.1044.Collect More Jewels(bfs + 状态压缩)

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5345    Accepted Submission(s): 1189 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

HDU ACM 1044 Collect More Jewels BFS+DFS

题意:在一个迷宫中,有一些宝物,从起点走到终点,问在给定的时间内,到达终点后所能拾取珠宝的最大价值. 分析(BFS+DFS): 1.求入口到第一个取宝物的地方的最短距离 2.求第i个取宝物的地方到第i+1个取宝物的地方的最短距离 3.求第n个取宝物的地方到出口的最短距离 4.保证以上3点能在时间L内实现的情况下,取得的宝石价值最大. BFS特点:对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元来存储状态) DFS特点:对于解决遍历和求所有问题有效,对于问

1044 [Collect More Jewels] DFS+BFS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1044 题目大意:在地图中,有M个宝石,每个宝石有不同价值.问在时间限制L之内,从入口到出口这一路上获得的最大价值是多少.拿宝石不额外花时间,走一格用时为1. 关键思想:考虑到BFS和DFS的特点,BFS在解决最短(小)问题是很有效,但内存耗费巨大:DFS可以解决绝大多数搜索问题,但层数较深时,时间开销和栈的开销都很大. 这道题,只用DFS显然是不行的,地图比较大了.但是只用BFS也不行,因为取完之后

HDU Collect More Jewels 1044

BFS + 状态压缩 险过 这个并不是最好的算法 但是写起来比较简单 , 可以AC,但是耗时比较多 下面是代码 就不多说了 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std; #define Max(a,b) (a>b?a:b) #define Min(a,b) (a

hdu 1044(bfs+dfs+剪枝)

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6739    Accepted Submission(s): 1564 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

Collect More Jewels(hdu1044)(BFS+DFS)

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6857    Accepted Submission(s): 1592 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

hdu 1044(bfs+状压)

非常经典的一类题型 没有多个出口.这里题目没有说清楚 Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4684    Accepted Submission(s): 983 Problem Description It is written in the Book of The Lady: Afte