关闭

[置顶] HDU 1044 Collect More Jewels(BFS+DFS)

标签: HDUBFSDFS
215人阅读 评论(0) 收藏 举报
分类:

Collect More Jewels

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5499    Accepted Submission(s): 1239


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.


题意:

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


思路:

  参考Kuangbin的文章:

  BFS+DFS
  我们要解决几个问题:1、求入口到第一个取宝物的地方的最短距离
2、求第i个取宝物的地方到第i+1个取宝物的地方的最短距离
3、求第n个取宝物的地方到出口的最短距离
4、保证以上3点能在时间L内实现的情况下,取得的宝石价值最大(DFS)


BFS特点:对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元来存储状态)
DFS特点:对于解决遍历和求所有问题有效,对于问题搜索深度小的时候处理速度迅速,然而在深度很大的情况下效率不高
所以,我们先用BFS求出入口、各宝物堆、出口两两间的最短距离,然后用DFS求出在时间L内到达出口且取得最大价值宝物堆的路径。


//46MS	1736K
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int W,H,L,M;
int val[60];//宝石价值1-M
char map[60][60];
bool used[60][60];//bfs访问标记
bool vis[60];//dfs访问标记
int step[60][60];//两两间的所需时间
int ans;//最大的价值
int sum;//所有宝石价值总和
int mov[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int dis[60][60];//
queue<int> q;

//从(x1,y1)到其余各点的距离,s为该店的编号,0为初始位置
//M+1为终点,1-M为宝石编号
void bfs(int x1,int y1,int s)
{
    while(!q.empty())
        q.pop();
    memset(used,false,sizeof(used));
    memset(step,0,sizeof(step));
    int u = x1*W + y1;
    q.push(u);
    used[x1][y1] = true;
    step[x1][y1] = 0;
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        int x = u/W;
        int y = u%W;
        for(int i=0; i<4; i++)
        {
            int xx = x + mov[i][0];
            int yy = y + mov[i][1];
            if(xx<0 || xx>=H || yy<0 || yy>=W)continue;
            if(used[xx][yy] ||map[xx][yy]=='*')continue;
            used[xx][yy] = true;
            step[xx][yy] = step[x][y] + 1;
            if(map[xx][yy]=='@') dis[s][0] = step[xx][yy];
            else if(map[xx][yy]=='<') dis[s][M+1] = step[xx][yy];
            else if(map[xx][yy]>='A' && map[xx][yy]<='J')
                dis[s][map[xx][yy]-'A'+1] = step[xx][yy];
            q.push(xx*W+yy);
        }
    }
}

//s为编号,value为价值,time为花费的时间
void dfs(int s,int value,int time)
{
    if(time>L) return;
    if(ans==sum)return;
    if(s>M)
    {
        if(value>ans) ans=value;
        return;
    }
    for(int i=0;i<=M+1;i++)
    {
        if(dis[s][i]==0||vis[i])continue;
        vis[i] = true;
        dfs(i,value+val[i],time+dis[s][i]);
        vis[i] = false;
    }
}

int main()
{
    int T;
    cin>>T;
    int iCase = 0;
    while(T--)
    {
        memset(dis,0,sizeof(dis));
        iCase++;
        cin>>W>>H>>L>>M;
        sum = 0;
        ans = -1;
        for(int i=1; i<=M; i++)//宝石编号是1-M
        {
            cin>>val[i];
            sum+=val[i];
        }
        val[0] = val[M+1] = 0;
        for(int i=0; i<H; i++)
            scanf("%s",&map[i]);
        for(int i = 0; i<H; i++)
            for(int j = 0; j<W; j++)
            {
                if(map[i][j]=='@') bfs(i,j,0);
                else if(map[i][j]=='<') bfs(i,j,M+1);
                else if(map[i][j]>='A' && map[i][j]<='J')
                    bfs(i,j,map[i][j]-'A'+1);
            }
        memset(vis,0,sizeof(vis));
        vis[0] = true;
        dfs(0,0,0);
        cout<<"Case "<<iCase<<":"<<endl;
        if(ans>=0) cout<<"The best score is "<<ans<<"."<<endl;
        else cout<<"Impossible"<<endl;
        if(T > 0) cout<<endl;
    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:28513次
    • 积分:922
    • 等级:
    • 排名:千里之外
    • 原创:64篇
    • 转载:9篇
    • 译文:0篇
    • 评论:5条
    最新评论