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

题目大意:

在一个迷宫中,从起点走到终点,还有几个宝物,问在给定的时间内,到达终点后所能获取的最大价值。

思路:

先用bfs求出入口,宝物,出口,两两之间的最短距离。

在用dfs搜索所有情况,求出从入口走到出口能获得的最大价值。

熟悉两种搜索的优缺点:

BFS: 对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元用来存储状态)。

DFS:对于解决遍历和求所有问题有效,对于问题搜索深度小的时候处理速度迅速,然而在深度很大的情况下效率不高

#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
    int x,y,step;
};
int maxx,n,m,many,limit_time,value_sum,val[15];
bool vis2[15],vis[55][55];
int step[4][2]= {1,0,-1,0,0,1,0,-1};
char map[55][55];//原始地图
int newmap[15][15];//新的最短距离标记  但是不是按照这个地图行走
int check(int x,int y)
{
    if(x>=0&&y>=0&&x<n&&y<m)
        return 1;
    return 0;
}
void BFS(int x,int y,int num)//num指的是:  [email protected]出发点  |  many+1=< 出口点   |  (0,many)  是标记‘A’到‘J’
{
    queue<node>q;
    while(!q.empty())
        q.pop();
    node a,n;
    memset(vis,false,sizeof(vis));

    a.x=x;
    a.y=y;
    a.step=0;
    vis[x][y]=true;////不加的话 newmap[][]是错误的 newmap中的0会变成2
    q.push(a);
    int nx,ny;
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            nx=a.x+step[i][0];
            ny=a.y+step[i][1];
            // if(nx>=0&&ny>=0&&nx<=n&&ny<=m)//不能白为什么 code block 不能这样编译 会出错,必须用check才行
            if(check(nx,ny))
            {
                if(map[nx][ny]!='*'&&vis[nx][ny]==false)
                {
                    vis[nx][ny]=true;
                    n.x=nx;
                    n.y=ny;
                    n.step=a.step+1;
                    q.push(n);
                    //找出num点到各个点的最短距离 标记给newmap
                    if(map[nx][ny]=='@') newmap[num][0]=n.step;
                    else if(map[nx][ny]=='<') newmap[num][many+1]=n.step;
                    else if(isalpha(map[nx][ny])) newmap[num][map[nx][ny]-64]=n.step;
                }
            }
        }
    }
}
void DFS(int hang,int value,int step)//hang是指hang这个点 | value 总价值
{
    if(step>limit_time||maxx==value_sum) return ;////一个是> 不是>= && 另一个是maxx== 不是value==
    if(hang>many&&value>maxx)//已经找完最后一个宝珠 并且最后一个宝珠也被加到总价值里面了
        maxx=value;    //选出每个深搜后的最大值
    for(int i=0; i<=many+1; i++)////是<=不是<
    {
        if(vis2[i]==true||newmap[hang][i]==0) continue;
        vis2[i]=true;
        DFS(i,value+val[i],newmap[hang][i]+step);
        vis2[i]=false;
    }
}
int main(void)
{
    int c,xxx=1;
    scanf("%d",&c);////
    while(c--)
    {
        scanf("%d%d%d%d",&m,&n,&limit_time,&many);
        memset(vis2,false,sizeof(vis2));
        memset(newmap,0,sizeof(newmap));
        maxx=-1;
        value_sum=0;////之前少输入了这个 一直过不了
        for(int i=1; i<=many; i++) //宝珠的数量
        {
            scanf("%d",&val[i]);
            value_sum+=val[i];
        }
        val[0]=val[many+1]=0;
        for(int i=0; i<n; i++)
        {
            scanf("%s",map[i]);////不能吧下一个for放到这里面,因为下一个for里面进行了 搜索
        }

        //广搜BFS找出各个点之间的最短距离
        for(int i=0; i<n; i++)
        {
            for(int ii=0; ii<m; ii++)
            {
                if(map[i][ii]=='@') BFS(i,ii,0);
                else if(map[i][ii]=='<') BFS(i,ii,many+1);
                else if(isalpha(map[i][ii])) BFS(i,ii,map[i][ii]-64);//@在ascll码里刚好是A前面一个编号64 这里也可以 减-64
            }
        }
        /*
        for(int i=0; i<=many+3; i++)
         {
             for(int ii=0; ii<=many+3; ii++)
             {
                 printf("%3d",newmap[i][ii]);
             }
             printf("\n");
         }
         */

         //深搜 找出符合条件最大价值的路径
        vis2[0]=true;
        DFS(0,0,0);

        //输出部分
        printf("Case %d:\n",xxx++);
        if(maxx==-1)
            printf("Impossible\n");
        else
            printf("The best score is %d.\n",maxx);
        if(c) printf("\n");
    }
    return 0;
}
时间: 2024-08-09 19:53:49

HDU 1044 Collect More Jewels【BFS+DFS+建立距离图】的相关文章

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

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

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

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

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