HDU 1044 BFS
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
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.
题意:
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的更多相关文章
- hdu 1044(bfs+状压)
非常经典的一类题型 没有多个出口.这里题目没有说清楚 Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- hdu 1044(bfs+dfs+剪枝)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1044 BFS(压缩图)+DFS
题意: 给你起点,终点,图上有墙有路还有宝物,问你在规定时间内能否能到终点,如果能问最多能捡到多少宝物. 思路: 看完这个题目果断 BFS+三维的mark ...
- 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 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- 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+状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1044
http://acm.hdu.edu.cn/showproblem.php?pid=1044 代码题,没什么好说的,先预处理出两点间距离,然后dfs搜一下找最大值 #include <iostr ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
随机推荐
- Java学习笔记-13.创建窗口和程序片
1.init()方法:程序片第一次被创建,初次运行初始化程序片时调用. start()方法:每当程序片进入web浏览器中,并且允许程序片启动他的常规操作时调用(特殊的程序片被stop()关闭):同样在 ...
- mysql 按日期统计
按年汇总,统计: select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col ...
- CTC (Connectionist Temporal Classification) 算法原理
(原创文章,转载请注明出处哦~) 简单介绍CTC算法 CTC是序列标注问题中的一种损失函数. 传统序列标注算法需要每一时刻输入与输出符号完全对齐.而CTC扩展了标签集合,添加空元素. 在使用扩展标签集 ...
- Leetcode - 557. Reverse Words in a String III (C++) stringstream
1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是 ...
- 创新手机游戏《3L》开发点滴(1)——道具、物品、装备表设计
一.游戏物品/道具系统数据模型设计特点 为了让游戏更加的丰富,我们1201团队的新手机游戏设计了道具系统.于是丰富了游戏.取悦了玩家,哭了开发——道具/物品数据子系统是简单的.复杂的.不确定的: 简单 ...
- linux下的常用技巧。
xargs linux下的多行合并~ [root@]# yum list installed|grep php|awk -F ' ' '{print $1}' php-channel-nrk.noa ...
- jQuery实现仿京东商城图片放大镜
效果图: 不废话直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 第二次作业(1001.A+B Format (20))
代码文件及题目描写已放至此 一开始看题目的时候有点没看懂,要求把数分组是什么意思.如果只是单纯的a+b的话是不可能的,所以关于这一点犹豫了很久.本来以为是指把a,b,以及它们的和c各建一个数组,但只输 ...
- .getClass()和.class的区别
一直在想.class和.getClass()的区别,思索良久,有点思绪,然后有网上搜了搜,找到了如下的一篇文章,与大家分享. 原来为就是涉及到java的反射----- Java反射学习 所谓反射,可以 ...
- error LNK2019: 无法解析的外部符号 该符号在函数 中被引用 解决方案
需要添加lib或者dll库.项目-属性-配置属性-链接器-输入-附件依赖项,添加需要的lib. 例如我在运行OSG程序的时候,忘记添加了附件依赖项就会报这个错. 解决方案如图.