HDU1044 Collect More Jewels

Collect More Jewels


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


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

题意:在一定时间内从@走到<。。走一步需要一秒,有些格子有val,问走到<最多能获得多少val。。。

bfs+dfs。。。先用bfs把图给缩掉。。。用一个二维数组记录从起点到每个有val点的最小路径,然后从终点和每个val出发的也记录一下,然后从起点跑这个缩好的图,求出最大的val就是了。。。。这里要加个剪枝,就是如果已经找到的val等于所有val的和就直接return。。。不加这个剪枝就TLE。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
char mp[55][55];
int val[15],dis[15][15],step[55][55];
bool vis[55][55];
int n,m,L,k;
void bfs(int x,int y,int num)
{
    int temp;
    queue<int> q;
    memset(vis,0,sizeof(vis));
    memset(step,0,sizeof(step));
    temp=x*m+y;
    vis[x][y]=1;
    q.push(temp);
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        int sx=temp/m;
        int sy=temp%m;
        for(int i=0;i<4;i++)
        {
            int nx=sx+dir[i][0];
            int ny=sy+dir[i][1];
            if(nx<0||nx>=n||ny<0||ny>=m)
                continue;
            if(mp[nx][ny]=='*')
                continue;
            if(vis[nx][ny])
                continue;
            step[nx][ny]=step[sx][sy]+1;
            if(mp[nx][ny]=='@')
                dis[num][0]=step[nx][ny];
            if(mp[nx][ny]=='<')
                dis[num][k+1]=step[nx][ny];
            else if(mp[nx][ny]>='A'&&mp[nx][ny]<='J')
                dis[num][mp[nx][ny]-'A'+1]=step[nx][ny];
            temp=nx*m+ny;
            vis[nx][ny]=1;
            q.push(temp);
        }
    }
}
int mmax;
int sval;
bool vis1[60];
void dfs(int time,int u,int v)
{
    if(time>L)
        return;
    if(mmax==sval)  //剪枝。。不加就超时
        return;
    if(u>k)
    {
        if(mmax<v)
            mmax=v;
        return;
    }
    int i;
    for(i=0;i<=k+1;i++)
    {
        if(!dis[u][i]||vis1[i])
            continue;
        vis1[i]=1;
        dfs(time+dis[u][i],i,v+val[i]);
        vis1[i]=0;
    }
}
int main()
{
    int t,i,j,flag=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&m,&n,&L,&k);
        sval=0;
        memset(dis,0,sizeof(dis));
        for(i=1;i<=k;i++)
        {
            scanf("%d",&val[i]);
            sval+=val[i];
        }
        val[0]=val[k+1]=0;
        for(i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
        }
        memset(vis1,0,sizeof(vis1));
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(mp[i][j]=='@')
                    bfs(i,j,0);
                else if(mp[i][j]=='<')
                    bfs(i,j,k+1);
                else if(mp[i][j]>='A'&&mp[i][j]<='J')
                    bfs(i,j,mp[i][j]-'A'+1);
            }
        }
        mmax=-1;
        vis1[0]=1;
        dfs(0,0,0);
        printf("Case %d:\n",flag++);
        if(mmax==-1)
            printf("Impossible\n");
        else
            printf("The best score is %d.\n",mmax);
        if(t>0)
            printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值