码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-29 15:46:52      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:


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指的是:  0=@出发点  |  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;
}


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

标签:

原文地址:http://blog.csdn.net/qq_24653023/article/details/52056908

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!