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.*
*****
*/




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值