Collect More Jewels

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

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.

 #include<stdio.h>
#include<queue>
#include<string.h>
#include<ctype.h>
#include<algorithm>
#include<math.h>
int T , maxn ;
int m , n , l , p ;
int val[] ;
char map[][] ;
bool vis[][][] ;
int move[][] = {{,} ,{-,} , {,} , {,-}} ;
struct node
{
int x , y , step ;
int jew , v;
}; void bfs (int sx , int sy)
{
std::queue <node> q ;
while ( !q.empty ()) q.pop () ;
q.push ((node) {sx , sy , , , }) ;
node ans , tmp ;
vis[sx][sy][] = ;
while (!q.empty ()) {
ans = q.front () ; q.pop () ;
// printf ("S---(%d,%d) %d = %d\n" , ans.x , ans.y , ans.step , ans.v) ;
if (map[ans.x][ans.y] == '<') maxn = std::max (maxn , ans.v) ;
for (int i = ; i < ; i ++) {
tmp.x = ans.x + move[i][] ; tmp.y = ans.y + move[i][] ;
if (tmp.x < || tmp.y < || tmp.x >= n || tmp.y >= m) continue ;
if (map[tmp.x][tmp.y] == '*') continue ;
tmp.jew = ans.jew ;
tmp.step = ans.step + ;
if (tmp.step > l) continue ;
int k = - ;
tmp.jew = ans.jew ; tmp.v = ans.v ;
if ( isalpha (map[tmp.x][tmp.y]) ) {
k = map[tmp.x][tmp.y] - 'A' ;
if (! (tmp.jew & ( << k))) tmp.v += val[k] ;
tmp.jew = tmp.jew | ( << k) ;
}
if ( vis[tmp.x][tmp.y][tmp.jew] ) continue ;
// printf ("(%d,%d) %d = %d\n" , tmp.x , tmp.y , tmp.step , tmp.v) ;
vis[tmp.x][tmp.y][tmp.jew] = ;
q.push (tmp ) ;
}
}
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
int T , cas = ;
scanf ("%d" , &T ) ;
while (T --) {
printf ("Case %d:\n" , cas ++) ;
int ex , ey , sx , sy ;
memset (vis , , sizeof(vis)) ;
scanf ("%d%d%d%d" , &m , &n , &l , &p) ;
for (int i = ; i < p ; i ++) scanf ("%d" , &val[i]) ;
for (int i = ; i < n; i ++) scanf ("%s" , map[i]) ; //, puts (map[i]);
maxn = - ;
for (int i = ; i < n; i ++) {
for (int j = ; j < m ; j ++) {
if (map[i][j] == '@')
sx = i , sy = j ;
else if (map[i][j] == '<')
ex = i , ey = j ;
}
}
if (fabs (sx - ex ) + fabs (sy - ey) > l) {
printf ("Impossible\n") ;
if (T != ) puts ("") ;
continue ;
}
bfs (sx , sy) ;
if (maxn != - ) printf ("The best score is %d.\n" , maxn ) ;
else puts ("Impossible") ;
if (T != ) puts ("") ;
}
return ;
}

逗比的我把状压state | (1 << k) 一直写成 state | k ,然后....
有逗比的折腾出一种   + 宝藏  的方法,以后决定就先判定它有没有加入过,然后直接 + . (明明觉得很水的题orz)

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

  1. hdu 1044 Collect More Jewels

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

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

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

  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. 【问题】R文件报错原因及解决办法 (转)

    错误如图.下面是几种解决方法(网上搜集的). 1.如果是导入项目出现这个问题,一般是R文件没有更新造成(据说导入项目,R不会自动更新).可以Project——clean以下,R文件会重新生成. 2.选 ...

  2. python使用cookielib库示例分享

    Python中cookielib库(python3中为http.cookiejar)为存储和管理cookie提供客户端支持,下面是使用示例 该模块主要功能是提供可存储cookie的对象.使用此模块捕获 ...

  3. zabbix监控系列(4)之zabbix报警邮件无法发送

    情况介绍 首先确保邮箱规则没有把报警邮件作为垃圾邮件拉黑了. 服务器断电重启后,发现zabbix报警邮件无法发送,断电之前是好好的,但是重启后不行了,于是查看maillog日志,发现这个错误: Hos ...

  4. C++ STL中vector的内存机制和性能分析

    vecotr是动态数组,顾名思义他可以动态的增加自己的长度. 内存机制: 但是怎样的增加自己的长度? vector有两个函数一个是capacity()返回内存空间即缓冲区的大小,另一个是size()返 ...

  5. JS-鼠标经过显示二级菜单

    在css处添加了border样式为了看得更清楚——源代码有一个程序漏洞,存在一个很烦人的大bug. <ul class="nav"> <li class=&quo ...

  6. Jquery 基本知识(一)

    1. DOM对象:通过例如getElementById方法获取到DOM树中的元素就是DOM对象 jQuery对象:通过jQuery包装DOM对象后产生的对象 --- 注意:jQuery对象和DOM对象 ...

  7. 比较oracle数据的表结构

    对比不同用户对象的异同,同时生成sql语句或直接提交到数据库,powerdesinger的比较实在是麻烦. pl/sql为我们提供了很好的工具 在pl/sql中的工具下“比较用户对象”,下即可实现:

  8. Java包装类

    自动装箱:指开发人员可以把一个基本数据类型直接赋给对应的包装类 自动拆箱:指开发人员可以把一个包装类对象直接赋给对应的基本数据类型 要把基本数据类型称为对象的时候,需要把基本数据类型进行包装, 运用: ...

  9. Linux学习笔记(一)2015.4.13

    研究生由单片机转Linux学习 首先安装VMware虚拟机,用的是VMware 10.0 在VMware 10.0上安装视频上推荐的Red Hat Linux 5 安装后正式进入Linux学习 笔记1 ...

  10. header的安全配置指南

    0x00 背景 在统计了Alexa top 100万网站的header安全分析之后(2012年11月 - 2013年3月 - 2013年11月),我们发现其实如何正确的设置一个header并不是一件容 ...