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 god Moloch rebelled against the authority of Marduk the Creator.Moloch stole from Marduk the most powerful of all the artifacts of the gods, the Amulet of Yendor, and he hid it in the dark cavities of Gehennom, the Under World, where he now lurks, and bides his time.

Your goddess The Lady seeks to possess the Amulet, and with it to gain deserved ascendance over the other gods.

You, a newly trained Rambler, have been heralded from birth as the instrument of The Lady. You are destined to recover the Amulet for your deity, or die in the attempt. Your hour of destiny has come. For the sake of us all: Go bravely with The Lady!

If you have ever played the computer game NETHACK, you must be familiar with the quotes above. If you have never heard of it, do not worry. You will learn it (and love it) soon.

In this problem, you, the adventurer, are in a dangerous dungeon. You are informed that the dungeon is going to collapse. You must find the exit stairs within given time. However, you do not want to leave the dungeon empty handed. There are lots of rare jewels in the dungeon. Try collecting some of them before you leave. Some of the jewels are cheaper and some are more expensive. So you will try your best to maximize your collection, more importantly, leave the dungeon in time.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.

The first line of each test case contains four integers W (1 <= W <= 50), H (1 <= H <= 50), L (1 <= L <= 1,000,000) and M (1 <= M <= 10). The dungeon is a rectangle area W block wide and H block high. L is the time limit, by which you need to reach the exit. You can move to one of the adjacent blocks up, down, left and right in each time unit, as long as the target block is inside the dungeon and is not a wall. Time starts at 1 when the game begins. M is the number of jewels in the dungeon. Jewels will be collected once the adventurer is in that block. This does not cost extra time.

The next line contains M integers,which are the values of the jewels.

The next H lines will contain W characters each. They represent the dungeon map in the following notation:
> [*] marks a wall, into which you can not move;
> [.] marks an empty space, into which you can move;
> [@] marks the initial position of the adventurer;
> [<] marks the exit stairs;
> [A] - [J] marks the jewels.

Output

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.

If the adventurer can make it to the exit stairs in the time limit, print the sentence "The best score is S.", where S is the maximum value of the jewels he can collect along the way; otherwise print the word "Impossible" on a single line.

Sample Input

3

4 4 2 2

100 200

****

*@A*

*B<*

****

4 4 1 2

100 200

****

*@A*

*B<*

****

12 5 13 2

100 200

************

*B.........*

*.********.*

*@...A....<*

************

Sample Output

Case 1:

The best score is 200.

Case 2:

Impossible

Case 3:

The best score is 300.

//第一行是测试组数

然后每组第一行是 地图的宽,地图的长,时间,宝物数量

然后是宝物的价值

然后是地图

走一格算 1 的时间

要在规定的时间内,拿到最高的价值

这题有点难度,首先,用 bfs ,从所有的宝物,出口,入口 开始 bfs ,确定他们之间最短路径有多长,构成一个隐式图,用二维数组保存就行

然后就从入口开始 DFS,限制条件就是不超时,DFS到出口就更新 ans=max(ans,cnt)

100ms

  1 //BFS+DFS so good!
  2 //BFS 从每个点出发,找到这个点到其他点的最短路
  3 //用一个二维数组保留宝藏,入口,出口之间的最短距离
  4 //DFS 从起点出发,去创造最大价值,然后去出口
  5
  6 #include <iostream>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <queue>
 10 #define maxn 52
 11 using namespace std;
 12
 13
 14 int n,m,l,cnt,ans,sum;
 15 int sx,sy,ex,ey;
 16
 17 int dx[4]= {-1,1,0,0};
 18 int dy[4]= {0,0,-1,1};
 19
 20 char mp[maxn][maxn];
 21 int jewel[12];//珠宝价值
 22
 23 struct Node
 24 {
 25     int x,y;
 26     int step;
 27 };
 28 int bvis[maxn][maxn];//bfs的时候vis
 29 int min_d[12][12];   //宝藏,出入口之间最小距离
 30
 31 int b_check(int x,int y)
 32 {
 33     if (x<0||x>=n||y<0||y>=m)
 34         return 0;
 35     if (mp[x][y]==‘*‘||bvis[x][y]==1)
 36         return 0;
 37     return 1;
 38 }
 39
 40
 41 int bfs(int x,int y,int k)
 42 {
 43     Node now,next;
 44     now.x=x;
 45     now.y=y;
 46     now.step=0;
 47     queue<Node> Q;
 48     Q.push(now);
 49     memset(bvis,0,sizeof(bvis));//每次BFS都可以继续走
 50     while (!Q.empty())
 51     {
 52         now=Q.front();
 53         Q.pop();
 54         if (now.step>l) break;
 55         bvis[now.x][now.y]=1;
 56         for (int i=0;i<4;i++)
 57         {
 58             next.x=now.x+dx[i];
 59             next.y=now.y+dy[i];
 60             next.step=now.step+1;
 61
 62             if (b_check(next.x,next.y))//如果可以走
 63             {
 64                 if (mp[next.x][next.y]>=‘A‘&&mp[next.x][next.y]<=‘J‘)
 65                 {
 66                     min_d[k][mp[next.x][next.y]-‘A‘]=next.step;
 67                     min_d[mp[next.x][next.y]-‘A‘][k]=next.step;
 68                 }
 69                 else if(mp[next.x][next.y]==‘@‘)
 70                 {
 71                     min_d[k][10]=next.step;
 72                     min_d[10][k]=next.step;
 73                 }
 74                 else if(mp[next.x][next.y]==‘<‘)
 75                 {
 76                     min_d[k][11]=next.step;
 77                     min_d[11][k]=next.step;
 78                 }
 79                 bvis[next.x][next.y]=1;
 80                 Q.push(next);
 81             }
 82         }
 83     }
 84     while (!Q.empty()) Q.pop();
 85     return 0;
 86 }
 87
 88 int dvis[12];//dfs的vis
 89
 90 int dfs(int now,int sc,int step)
 91 {
 92     if (now<10) sc+=jewel[now];
 93
 94     if (step>l||ans==sum) return 0;
 95
 96     if (now==11&&sc>ans)
 97     {
 98         ans=sc;
 99         return 0;
100     }
101
102     for (int i=0;i<12;i++)
103     {
104         if (min_d[now][i]!=-1&&dvis[i]==0)//如果可以去,并且没被拿过
105         {
106             dvis[i]=1;
107             dfs(i,sc,step+min_d[now][i]);
108             dvis[i]=0;
109         }
110     }
111 }
112
113 int main()
114 {
115     int t,t1=0;
116     scanf("%d",&t);
117     while(t--)
118     {
119         t1++;
120
121         sum=0;//珠宝总价值
122         int i,j;
123
124         scanf("%d%d%d%d",&m,&n,&l,&cnt);
125         for (i=0;i<cnt;i++)
126         {
127             scanf("%d",&jewel[i]);
128             sum+=jewel[i];
129         }
130         getchar();
131         for (i=0;i<n;i++)
132         {
133             for (j=0;j<m;j++)
134             {
135                 scanf("%c",&mp[i][j]);
136                 if (mp[i][j]==‘@‘) sx=i,sy=j;
137                 if (mp[i][j]==‘<‘) ex=i,ey=j;
138             }
139             getchar();
140         }
141
142         memset(min_d,-1,sizeof(min_d));//隐式图初始化
143         for (i=0;i<n;i++)
144         {
145             for (j=0;j<m;j++)
146             {
147                 if (mp[i][j]>=‘A‘&&mp[i][j]<=‘J‘) bfs(i,j,mp[i][j]-‘A‘);
148                 else if (mp[i][j]==‘@‘) bfs(i,j,10);
149             }
150         }
151
152         /*
153         //输出隐式图
154         for (i=0;i<12;i++)
155         {
156             for (j=0;j<12;j++)
157                 printf("%d ",min_d[i][j]);
158             printf("\n");
159         }
160         */
161
162
163         ans=-1;
164         memset(dvis,0,sizeof(dvis));
165         dvis[10]=1;
166         dfs(10,0,0);//从入口开始,初始分数为0,步数0
167         printf("Case %d:\n",t1);
168         if(ans!=-1) printf("The best score is %d.\n",ans);
169         else printf("Impossible\n");
170         if(t) printf("\n");
171     }
172     return 0;
173 }

另外,还可以剪下枝

在DFS的时候可以,如果到拿这个宝物的时候,步数比以前的更大,并且价值更少,就不要去DFS了

可以优化到31ms

时间: 2024-08-05 23:39:56

Collect More Jewels(hdu1044)(BFS+DFS)的相关文章

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+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 ACM 1044 Collect More Jewels BFS+DFS

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

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 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

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

邻结矩阵的建立和 BFS,DFS;;

邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!---------------------------------------------------------------------------------------------------------------------------------------//邻接矩阵的建立和 其BFS, DFS, 遍历 #include <

建图方式一 之 ”前向星“ BFS&amp;&amp;DFS 简单应用

三种建图方式,邻接矩阵.前向星(边表集).邻接链表! 耗时一晚上 ,好好研究了一下 前向星,因为我的指针用的实在是很烂,所以还是 入赘 前向星吧. 问了学长,看了大牛们的博客,终于有点收获了,个人认为 前向星Very Well. 前向星 建图方法: 以储存边的方式来储存图.在构造图时,把边存放在数组里,不需使用指针,只需一个 next  即可是整个图构建完成 . 适用条件: 点集特别多的稀疏图,边数多且繁杂,开邻接矩阵会浪费大量内存. 时间复杂度: O(m),并不比邻接链表差. #include

UVALive - 6455 Stealing Harry Potter&#39;s Precious (bfs+dfs)

https://cn.vjudge.net/problem/UVALive-6455 题目大意:题目上给出一个地图,其中@是人在地图上的出发点,#是墙,' . '是路.然后给出几个点,这些点表示财宝的所在地.问人是否能够得到所有的宝藏,如果能够的话给出所有的宝藏的最短的路径. 解题思路:由于只有最多4个宝藏所在地,所以只要用bfs找出出发点和财宝所在地距离到达其他点的步数.因为最多只有4个宝藏,所以可以暴力找出出发点到所有宝藏的最短距离. 暴力代码: 1 #include<stdio.h> 2