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

Source

Asia 2005, Hangzhou (Mainland China), Preliminary

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1044

题意:输入一个T,代表测试数量,再输入W,H代表地图大小,L代表时间,M代表珠宝的数量,‘*’代表墙,‘.’代表路,‘@’代表起点,‘<’代表出口,‘A’-‘J’代表珠宝,下面一行依次表示珠宝的价值,问你在规定的时间最多能拿价值为多少的珠宝出去。

PS:刚开始理解错了,用了优先队列+BFS发现WA了,后来才发现可以走重复的路,去拿更多的珠宝,一时没有了思路,后来参考了邝斌的博客,才德AC。

思路:先用BFS求出每个珠宝到起点和终点的最短距离,再用DFS搜索取哪些珠宝的到最大价值。

此题还可以用状态压缩。

AC代码:

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
char a[55][55];//地图
int dis[55][55];//起点,珠宝,出口之间的距离
int step[55][55];//
bool vis[55][55];//BFS标记
bool mark[55];//DFS标记
int val[55];//珠宝价值
int w,h,t,n;
int sum,ans;
int dir[4][2]={1,0,0,1,-1,0,0,-1};
bool OK(int x,int y)
{
    if(x<0||x>=h||y<0||y>=w)
        return false;
    return true;
}
void BFS(int x,int y,int idx)
{
    queue<int>q;
    while(!q.empty())
        q.pop();
    memset(step,0,sizeof(step));
    memset(vis,false,sizeof(vis));
    step[x][y]=0;
    vis[x][y]=true;
    int p=x*w+y;
    q.push(p);
    while(!q.empty())
    {
        p=q.front();
        q.pop();
        int x=p/w;
        int y=p%w;
        for(int i=0;i<4;i++)
        {
            int xx=x+dir[i][0];
            int yy=y+dir[i][1];
            if(OK(xx,yy)&&!vis[xx][yy]&&a[xx][yy]!='*')
            {
                vis[xx][yy]=true;
                step[xx][yy]=step[x][y]+1;
                if(a[xx][yy]=='@')
                    dis[idx][0]=step[xx][yy];
                else if(a[xx][yy]=='<')
                    dis[idx][n+1]=step[xx][yy];
                else if(a[xx][yy]>='A'&&a[xx][yy]<='J')
                    dis[idx][a[xx][yy]-'A'+1]=step[xx][yy];
                q.push(xx*w+yy);
            }
        }
    }
}
//当前收集珠宝价值,当前珠宝价值,消耗时间
void DFS(int s,int value,int time)
{
    //注意下面三条语句的顺序
    if(time>t)
        return;
    if(ans==sum)
        return;
    if(s>n)
    {
        if(value>ans)
            ans=value;
        return;
    }
    for(int i=0;i<=n+1;i++)
    {
        if(mark[i]||dis[s][i]==0)
            continue;
        mark[i]=true;
        DFS(i,value+val[i],time+dis[s][i]);
        mark[i]=false;
    }
}

int main()
{
    int T,kase=0;
    cin>>T;
    while(T--)
    {
        cin>>w>>h>>t>>n;
        sum=0;
        for(int i=1; i<=n; i++)
        {
            cin>>val[i];
            sum+=val[i];
        }
        val[0]=val[n+1]=0;
        //把起点和终点当做第一个和最后一个价值为0珠宝
        for(int i=0; i<h; i++)
            cin>>a[i];
        memset(dis,0,sizeof(dis));
        for(int i=0; i<h; i++)
        {
            for(int j=0; j<w; j++)
            {
                if(a[i][j]=='@')
                    BFS(i,j,0);
                else if(a[i][j]=='<')
                    BFS(i,j,n+1);
                else if(a[i][j]>='A'&&a[i][j]<='J')
                    BFS(i,j,a[i][j]-'A'+1);
            }
        }
        memset(mark,false,sizeof(mark));
        mark[0]=true;
        ans=-1;
        DFS(0,0,0);
        cout<<"Case "<<++kase<<":"<<endl;
        if(ans>=0)
            cout<<"The best score is "<<ans<<"."<<endl;
        else
            cout<<"Impossible"<<endl;
        if(T)
            cout<<endl;
    }
    return 0;
}
时间: 2024-08-02 23:16:53

HDU 1044 Collect More Jewels 【经典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 + 状态压缩)

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

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 Collect More Jewels

并没有做过这种类型的题目,不太会做...看了一下大神的代码,然后自己敲...额..思路一样了,代码也差不多.. http://www.cnblogs.com/kuangbin/archive/2012/08/14/2637512.html 先通过BFS预处理任意两点之间的距离,然后通过DFS暴力模拟路径,算出最优解. 感觉自己可能对BFS理解比DFS更深一点,或者说,BFS比较简单一点吧... 这题还有一种解法是状态压缩+BFS...通过开设一个int型变量记录是否拿到宝物,然后暴力..不过这种

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

HDU 1254 (经典游戏)推箱子 BFS+dfs

Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动. 现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格. Input 输入数据的第一行是一个整数T(1<=T<=20),代表测试