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): 5345 Accepted Submission(s): 1189
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.
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.
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.
4 4 2 2
100 200
****
*@A*
*B<*
****
4 4 1 2
100 200
****
*@A*
*B<*
****
12 5 13 2
100 200
************
*B.........*
*.********.*
*@...A....<*
************
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 + 状态压缩)的更多相关文章
- hdu 1044 Collect More Jewels
题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...
- hdu 1044 Collect More Jewels(bfs+状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1044 Collect More Jewels(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1885 Key Task(bfs+状态压缩)
Problem Description The Czech Technical University years of its existence . Some of the university b ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- 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 ...
随机推荐
- Beta版本冲刺第六天 12.12
一.站立式会议照片: 二.项目燃尽图: Android端 后台 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 邮箱验证和用户评价的填写 用户评价的查看以 ...
- Android系统手机端抓包方法
抓包准备 1. Android手机需要先获得root权限.一种是否获得root权限的检验方法:安装并打开终端模拟器(可通过安卓市场等渠道获得).在终端模拟器界面输入su并回车,若报错则说明未root, ...
- 使用LaTeX编辑数学公式
首先在博客园的页首html里添加以下代码: <script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex ...
- f
module.exports = util; }); 除了define之外,我们看到module.exports = util;这一句比较特殊.这句是在说,我util模块向外暴露的接口就这些,其他所 ...
- python 培训之HTTP
1. urllib #!/usr/env/python # -*- coding:UTF-8 -*- from __future__ import print_function import sys ...
- BZOJ4668: 冷战
并查集,按秩合并,树高log,暴力查询. 果然bzoj新挂的题中过的人多的全是sb题. 写了一发秒WA,发现姿势不对.(@_@) 然后过了50min,开始怀疑人生.(*_*) 这么长时间我lct都写完 ...
- 延时Led灯
1.代码:#include <reg52.h>typedef unsigned int u16;typedef unsigned char u8;sbit led = P1^0;void ...
- LDA(主题模型算法)
LDA整体流程 先定义一些字母的含义: 文档集合D,topic集合T D中每个文档d看作一个单词序列< w1,w2,...,wn >,wi表示第i个单词,设d有n个单词.(LDA里面称之为 ...
- 用js实现QQ自定义在线图片
用JS实现,具体步骤如下: 1) 申请代码 <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin ...
- Sql — CTE公用表表达式和With用法总结
CTE(Common Table Expression) 公用表表达式,它是在单个语句的执行范围内定义的临时结果集,只在查询期间有效.它可以自引用,也可在同一查询中多次引用,实现了代码段的重复利用. ...