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

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

时间:2016-07-19 11:04:06      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

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


题意:输入一个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;
}


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

标签:

原文地址:http://blog.csdn.net/hurmishine/article/details/51933805

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