Collect More Jewels

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

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.
 
题意:@是起点,<是终点  .是路A到J是宝物每个宝物有不同的价值,要求你在t时间内走到出口,并带出最大价值的宝物
 
题解:结构体队列中jewel记录当前宝物是否遇见过,cost记录可获取宝物的价值,
 
#include<stdio.h>
#include<string.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 55
#define mod 10003
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
int head[MAX],ans;
int n,m,t,k;
char s[MAX][MAX];
int vis[MAX][MAX][1<<10];
int a[MAX];
struct node
{
int x,y,jewel,time,cost;
};
int Move[4][2]={1,0,-1,0,0,1,0,-1};
int judge(int a,int b)
{
if(a>=0&&a<n&&b>=0&&b<m&&s[a][b]!='*')
return 1;
return 0;
}
void bfs(int x1,int y1)
{
int i;
queue<node>q;
memset(vis,0,sizeof(vis));//刚开始忘记清空,一直错
while(!q.empty()) q.pop();
node beg,end;
beg.x=x1;
beg.y=y1;
beg.jewel=0;
beg.time=0;
beg.cost=0;
vis[beg.x][beg.y][beg.jewel]=1;
q.push(beg);
while(!q.empty())
{
beg=q.front();
q.pop();
if(s[beg.x][beg.y]=='<'&&beg.time<=t)
ans=max(ans,beg.cost);//找到最大的宝物价值
if(beg.time>t) continue;//超过时间
for(i=0;i<4;i++)
{
end.x=beg.x+Move[i][0];
end.y=beg.y+Move[i][1];
if(judge(end.x,end.y))
{
if(s[end.x][end.y]>='A'&&s[end.x][end.y]<='J')//找到宝物
{
end.jewel=beg.jewel;
end.time=beg.time+1;
int num=s[end.x][end.y]-'A';
if(end.jewel&(1<<num))//检查宝物是否已经拿走
end.cost=beg.cost;
else
{
end.cost=beg.cost+a[s[end.x][end.y]-'A'];
end.jewel=beg.jewel|(1<<num);
}
}
else//路
{
end.jewel=beg.jewel;
end.time=beg.time+1;
end.cost=beg.cost;
}
if(!vis[end.x][end.y][end.jewel])//标记当前位置
{
vis[end.x][end.y][end.jewel]=1;
q.push(end);
}
}
}
}
return ;
}
int main()
{
int l,op,x1,y1,i,j;
scanf("%d",&l);
op=1;
int ant=l;
while(l--)
{
scanf("%d%d%d%d",&m,&n,&t,&k);
memset(a,0,sizeof(a));
for(i=0;i<k;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%s",s[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(s[i][j]=='@')
{
x1=i;y1=j;
}
}
}
ans=-1;
printf("Case %d:\n",op++);
bfs(x1,y1);
if(ans>0) printf("The best score is %d.\n",ans);
else printf("Impossible\n");
if(l)
printf("\n");
}
return 0;
}

  

hdu 1044 Collect More Jewels(bfs+状态压缩)的更多相关文章

  1. hdu.1044.Collect More Jewels(bfs + 状态压缩)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. hdu 1044 Collect More Jewels

    题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...

  3. HDU 1044 Collect More Jewels(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. hdu 1885 Key Task(bfs+状态压缩)

    Problem Description The Czech Technical University years of its existence . Some of the university b ...

  5. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  6. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  7. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  8. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  9. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

随机推荐

  1. 为laravel5.1生产环境linux从源代码安装PHP

    laravel5.1正式发布,该版本号称是第一个LTS的版本,它对环境的要求也比较高,至少要PHP5.59以上. 现在网上找了很久,只能自己从头安装新版本的PHP yum install libmcr ...

  2. 使用public key来做SSH authentication

    public key authentication(公钥认证)是对通过敲用户名.密码方式登录服务器的一种替代办法.这种方法更加安全更具有适应性,但是更难以配置. 传统的密码认证方式中,你通过证明你你知 ...

  3. 【笨嘴拙舌WINDOWS】实践检验之GDI缩放

    SetMapmode(dc,MM_Text); //先继承MM_TEXT的属性 SetMapMode(dc,MM_ANISOTROPIC); SetWindowExtEx(dc,rect.Right, ...

  4. ZeptoLab Code Rush 2015

    A 题意:给出一串由.*组成的字符串,如果有等间距的五个及五个以上的*存在,则输出yes 直接枚举就可以了 看题一定要仔细啊,做的时候看成必须有五个等间距的".*"才可以跳跃= = ...

  5. POJ 3692 Kindergarten (补图是二分图的最大团问题)

    题意 幼稚园里有m个男孩和n个女孩(m.n范围都是[1,200]),男孩之间相互认识,女孩之间也相互认识,另外有部分男孩和女孩也认识.现在要举办一个活动,选取一些同学,要求所有选取的同学之间两两相互认 ...

  6. 20160131.CCPP体系详解(0010天)

    程序片段(01):Test.c+NewTest.c 内容概要:题目测试 ///Test.c #define _CRT_SECURE_NO_WARNINGS #include <stdio.h&g ...

  7. 物联网操作系统HelloX已成功移植到MinnowBoard MAX开发板上

    在HelloX开发团队的努力下,以及Winzent Tech公司(总部在瑞典斯德哥尔摩)的支持下,HelloX最新版本V1.78已成功移植到MinnowBoard MAX开发板上.相关源代码已经发布到 ...

  8. 五:分布式事务一致性协议paxos的应用场景

    1.应用场景 (1)分布式中的一致性 Paxos算法主要是解决一致性问题,关于“一致性”,在不同的场景有不同的解释: NoSQL领域:一致性更强调“能读到新写入的”,就是读写一致性数据库领域:一致性强 ...

  9. 【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型

    原文网址:http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/ #!/usr/bin/python 是用 ...

  10. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.5. 配置网络

    2.5. 配置网络 2.5.1. 配置网络 Oracle Rac数据库涉及到公用网络和私有网络,因此要做网络划分和IP地址规划,下表列出了要安装的RAC数据库对应的IP地址.主机名以及网络连接类型: ...