Collect More Jewels

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

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.
 
Source
 

题意:

n*m的迷宫,@是出发点,*是墙,.是路,<是出口,大写字母A~J表示该点有价值,问在t时间之内能否走出迷宫,若能走出求经过的最大价值。

输入T组数据

输入列m,行n,时间t,有价值的点的个数p

输入地图

代码:

//bfs,三维vis[i][j][k]标记数组,i,j表示点的位置,k表示到这个点时经过了多少有价值的点
//因为最多只有10个所以可以用二进制表示,价值只能取一次。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char mp[][];
int T,n,m,t,p,val[],ans;
int dir[][]={,,-,,,,,-};
bool vis[][][<<],cost[][];
struct node{
int x,y,cnt,sum,sta;
node(){}
node(int a,int b,int c,int d,int e):x(a),y(b),cnt(c),sum(d),sta(e){}
}no1,no2;
void bfs(int px,int py){
memset(vis,,sizeof(vis));
memset(cost,,sizeof(cost));
no1=node(px,py,,,);
queue<node>q;
q.push(no1);
vis[px][py][]=;
while(!q.empty()){
no1=q.front();q.pop();
if(no1.cnt>t) continue;
if(mp[no1.x][no1.y]=='<'){
if(ans==-) ans=;
ans=max(ans,no1.sum);
}
for(int i=;i<;i++){
int x=no1.x+dir[i][],y=no1.y+dir[i][];
if(x<||x>=n||y<||y>=m) continue;
if(mp[x][y]=='*') continue;
if('A'<=mp[x][y]&&mp[x][y]<='J'){
if(no1.sta&(<<(mp[x][y]-'A'))){
if(vis[x][y][no1.sta]) continue;
vis[x][y][no1.sta]=;
no2=node(x,y,no1.cnt+,no1.sum,no1.sta);
q.push(no2);continue;
}
int tmp=no1.sta^(<<(mp[x][y]-'A'));
if(vis[x][y][tmp]) continue;
vis[x][y][tmp]=;
no2=node(x,y,no1.cnt+,no1.sum+val[mp[x][y]-'A'],tmp);
q.push(no2);
}
else{
if(vis[x][y][no1.sta]) continue;
vis[x][y][no1.sta]=;
no2=node(x,y,no1.cnt+,no1.sum,no1.sta);
q.push(no2);
}
}
}
}
int main()
{
scanf("%d",&T);
for(int cas=;cas<=T;cas++){
int px,py;
scanf("%d%d%d%d",&m,&n,&t,&p);
for(int i=;i<p;i++) scanf("%d",&val[i]);
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++)
if(mp[i][j]=='@') {px=i;py=j;}
}
ans=-;
bfs(px,py);
printf("Case %d:\n",cas);
if(ans==-) printf("Impossible\n");
else printf("The best score is %d.\n",ans);
if(cas!=T) printf("\n");
}
return ;
}

代码:

HDU 1044 BFS的更多相关文章

  1. hdu 1044(bfs+状压)

    非常经典的一类题型 没有多个出口.这里题目没有说清楚 Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  2. hdu 1044(bfs+dfs+剪枝)

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

  3. hdu 1044 BFS(压缩图)+DFS

    题意:              给你起点,终点,图上有墙有路还有宝物,问你在规定时间内能否能到终点,如果能问最多能捡到多少宝物. 思路:           看完这个题目果断 BFS+三维的mark ...

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

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

  5. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

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

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

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

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

  8. HDU 1044

    http://acm.hdu.edu.cn/showproblem.php?pid=1044 代码题,没什么好说的,先预处理出两点间距离,然后dfs搜一下找最大值 #include <iostr ...

  9. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

随机推荐

  1. 深入理解 Vuejs 组件

    本文主要归纳在 Vuejs 学习过程中对于 Vuejs 组件的各个相关要点.由于本人水平有限,如文中出现错误请多多包涵并指正,感谢.如果需要看更清晰的代码高亮,请跳转至我的个人站点的 深入理解 Vue ...

  2. 开关灯问题(C++)

    [问题描述] 假设有 N 盏灯(N 为不大于 5000 的正整数),从 1 到 N 按顺序依次编号,初始时全部处于开启状态:有 M 个人(M 为不大于 N 的正整数)也从 1 到 M 依次编号.第一个 ...

  3. UVa 1225 - Digit Counting - ACM/ICPC Danang 2007 解题报告 - C语言

    1.题目大意 把前n$(n\le 10000)$个整数顺次写在一起:12345678910111213……计算0~9各出现了多少次. 2.思路 第一想法是打表,然而觉得稍微有点暴力.不过暂时没有想到更 ...

  4. 共识算法 pos,Dpos

    在之前讲解了比特币中的共识算法pow(proot of work),我们先来简单的回顾一下. 新的交易将会广播给所有节点. 每个节点将都会讲新的交易收集到一个区块中. 每个节点都在为其区块收集困难的工 ...

  5. HDU 1042 N!(高精度乘)

    Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!   Input One N in ...

  6. Python中函数的参数-arguments

    归纳起来,Python中函数的定义形式和调用形式主要有如下几种形式: # 函数的定义形式 def func(name) # 匹配positional参数或者keyword参数 def func(nam ...

  7. ACM 第十二天

    博弈论(巴什博奕,威佐夫博弈,尼姆博弈,斐波那契博弈,SG函数,SG定理) 一.  巴什博奕(Bash Game): A和B一块报数,每人每次报最少1个,最多报4个,看谁先报到30.这应该是最古老的关 ...

  8. 第二章 shell的语法

    变量:字符串.数字.环境和参数 获取变量内容可以在变量前使用$字符,使用echo指令可以将变量内容输出到终端. wuchao@wuchao-Lenovo:~$ var=hello wuchao@wuc ...

  9. python学习第二天-基本数据类型常用方法

    1.直入主题 python中基本的数据类型有 数字(整形,长整形,浮点型,复数) 字符串 字节串:在介绍字符编码时介绍字节bytes类型 列表 元组 字典 集合 下面我们直接将以下面几个点进行学习 # ...

  10. <Effective C++>读书摘要--Implementations<二>

    <Item29> Strive for exception-safe code. 1.如下面的代码 class PrettyMenu { public: ... void changeBa ...