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): 6356    Accepted Submission(s): 1455


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.
 

 题目大意:

有n*m这样的一个图,你初始的时候位于@处,终点位于<处,有时间限制的条件下,已知图里边有多少种宝石以及其每块宝石的价值,问最多能够拿出多少价值的宝石。


思路:

暴力Bfs+状态压缩。

1、对于每个点都会可能有重复行走的可能性,所以我们开判重数组要开三维的vis【x】【y】【tmp】,表示在x,y这个点上,身上有tmp这种情况的宝石是否走过。

2、对于身上有tmp这种情况的宝石是如何确定的呢?我们要枚举所有tmp的情况然然一一枚举?那我们会累死的.我们这里可以用到状态压缩的方法来解决,假如我们身上有了A和E宝石,那么我们可以将二进制01串的形式来表示现在这种tmp:10001。同理,假如我们身上有了A和和B和E宝石,那么我们可以将二进制01串的形式来表示现在这种tmp:10011。也就是说,我们用1代表有这个宝石,用0表示没有这个宝石。

3、对于宝石拿取的细节处理:因为每个点都有可能重复走,所以以前有宝石的地方也可以重复走,那么如何保证这种情况下不重复拿取宝石呢?我们可以用位运算中的与运算来解决这个问题,与运算的规则:同位都是1出1,否则都出0,我们可以根据这个特点来判断当前这个宝石是否已经拿取过了,只要判断一下now.tmp&(1<<宝石编号)是否为0即可,如果为0,表示没有拿过,如果为1,表示有拿过,如果没有拿过,我们nex.tmp=now.tmp+(1<<宝石编号),在二进制上操作将这个宝石放带在身上。

我们举例说明:
假如我们现在有A,E,now.tmp=17=10001;

如果我们遇到了A宝石:

显然17&1!=0。

如果我们遇到了B宝石:

显然17&2==0.

那么这个时候nex.tmp=17+2=19=10011;那么接下来如果再遇到B宝石我们也不用再担心重复拿取的问题辣!!

4、对于每种到终点的情况,都不一定是最优的,而且如果加入了优先队列的话,因为优先队列不仅仅优先了价值,还优先着先走出去是不行的,所以我们不能两个同时优先,所以我们只能放弃优先队列的使用,直接暴力搜、


思路构建完毕,剩下的就是代码实现了。


注意的点:如果操作系统不开心,会让我TLE,如果开心了,会让我AC,大概时间850ms+AC。所以如果大家照我的代码敲TLE了,没事,多交两发会AC的0.0

同一份代码TLE和AC图:



AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
struct zuobiao
{
    int x,y,tmp,output,ti;
}now,nex;
int val[50];
char a[55][55];
int vis[55][55][1<<10];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int n,m,time,zhonglei,ok,ans;
void Bfs(int x,int y)
{
    memset(vis,0,sizeof(vis));
    now.x=x;
    now.y=y;
    now.output=0;
    now.ti=0;
    now.tmp=0;
    queue<zuobiao >s;
    s.push(now);
    vis[x][y][0]=1;
    while(!s.empty())
    {
        now=s.front();
        if(a[now.x][now.y]=='<')
        {
            ok=1;
            if(now.output>ans)ans=now.output;
        }
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            nex.output=now.output;
            nex.ti=now.ti+1;
            nex.tmp=now.tmp;
            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&a[nex.x][nex.y]!='*')
            {
                if(a[nex.x][nex.y]>='A'&&a[nex.x][nex.y]<='J')
                {
                    int id=a[nex.x][nex.y]-'A';
                    if((nex.tmp&(1<<id))==0)
                    {
                        nex.output+=val[id];
                        nex.tmp+=(1<<id);
                    }
                    if(vis[nex.x][nex.y][nex.tmp]==0&&nex.ti<=time)
                    {
                        vis[nex.x][nex.y][nex.tmp]=1;
                        s.push(nex);
                    }
                }
                else
                {
                    if(vis[nex.x][nex.y][nex.tmp]==0&&nex.ti<=time)
                    {
                        vis[nex.x][nex.y][nex.tmp]=1;
                        s.push(nex);
                    }
                }
            }
        }
    }
    return ;
}
int main()
{
    int t;
    int kase=0;
    int f=0;
    scanf("%d",&t);
    while(t--)
    {
        if(f!=0)
        printf("\n");
        f++;
        int sx,sy;
        scanf("%d%d%d%d",&m,&n,&time,&zhonglei);
        for(int i=0;i<zhonglei;i++)
        {
            scanf("%d",&val[i]);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            for(int j=0;j<m;j++)
            {
                if(a[i][j]=='@')
                {
                    sx=i;
                    sy=j;
                }
            }
        }
        ans=0;
        printf("Case %d:\n",++kase);
        ok=0;
        Bfs(sx,sy);
        if(ok==1)
        {
            printf("The best score is %d.\n",ans);
        }
        else     printf("Impossible\n");
    }
}
/*
10
5 5 6 2
100 200
*****
*@A.*
*B<A*
*.A.*
*****
*/




Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值