Collect More Jewels(hdu1044)(BFS+DFS)
Collect More Jewels
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6857 Accepted Submission(s): 1592
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
Sample Output
Case 1:
The best score is 200.
Case 2:
Impossible
Case 3:
The best score is 300.
//第一行是测试组数
然后每组第一行是 地图的宽,地图的长,时间,宝物数量
然后是宝物的价值
然后是地图
走一格算 1 的时间
要在规定的时间内,拿到最高的价值
这题有点难度,首先,用 bfs ,从所有的宝物,出口,入口 开始 bfs ,确定他们之间最短路径有多长,构成一个隐式图,用二维数组保存就行
然后就从入口开始 DFS,限制条件就是不超时,DFS到出口就更新 ans=max(ans,cnt)
100ms
//BFS+DFS so good!
//BFS 从每个点出发,找到这个点到其他点的最短路
//用一个二维数组保留宝藏,入口,出口之间的最短距离
//DFS 从起点出发,去创造最大价值,然后去出口 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 52
using namespace std; int n,m,l,cnt,ans,sum;
int sx,sy,ex,ey; int dx[]= {-,,,};
int dy[]= {,,-,}; char mp[maxn][maxn];
int jewel[];//珠宝价值 struct Node
{
int x,y;
int step;
};
int bvis[maxn][maxn];//bfs的时候vis
int min_d[][]; //宝藏,出入口之间最小距离 int b_check(int x,int y)
{
if (x<||x>=n||y<||y>=m)
return ;
if (mp[x][y]=='*'||bvis[x][y]==)
return ;
return ;
} int bfs(int x,int y,int k)
{
Node now,next;
now.x=x;
now.y=y;
now.step=;
queue<Node> Q;
Q.push(now);
memset(bvis,,sizeof(bvis));//每次BFS都可以继续走
while (!Q.empty())
{
now=Q.front();
Q.pop();
if (now.step>l) break;
bvis[now.x][now.y]=;
for (int i=;i<;i++)
{
next.x=now.x+dx[i];
next.y=now.y+dy[i];
next.step=now.step+; if (b_check(next.x,next.y))//如果可以走
{
if (mp[next.x][next.y]>='A'&&mp[next.x][next.y]<='J')
{
min_d[k][mp[next.x][next.y]-'A']=next.step;
min_d[mp[next.x][next.y]-'A'][k]=next.step;
}
else if(mp[next.x][next.y]=='@')
{
min_d[k][]=next.step;
min_d[][k]=next.step;
}
else if(mp[next.x][next.y]=='<')
{
min_d[k][]=next.step;
min_d[][k]=next.step;
}
bvis[next.x][next.y]=;
Q.push(next);
}
}
}
while (!Q.empty()) Q.pop();
return ;
} int dvis[];//dfs的vis int dfs(int now,int sc,int step)
{
if (now<) sc+=jewel[now]; if (step>l||ans==sum) return ; if (now==&&sc>ans)
{
ans=sc;
return ;
} for (int i=;i<;i++)
{
if (min_d[now][i]!=-&&dvis[i]==)//如果可以去,并且没被拿过
{
dvis[i]=;
dfs(i,sc,step+min_d[now][i]);
dvis[i]=;
}
}
} int main()
{
int t,t1=;
scanf("%d",&t);
while(t--)
{
t1++; sum=;//珠宝总价值
int i,j; scanf("%d%d%d%d",&m,&n,&l,&cnt);
for (i=;i<cnt;i++)
{
scanf("%d",&jewel[i]);
sum+=jewel[i];
}
getchar();
for (i=;i<n;i++)
{
for (j=;j<m;j++)
{
scanf("%c",&mp[i][j]);
if (mp[i][j]=='@') sx=i,sy=j;
if (mp[i][j]=='<') ex=i,ey=j;
}
getchar();
} memset(min_d,-,sizeof(min_d));//隐式图初始化
for (i=;i<n;i++)
{
for (j=;j<m;j++)
{
if (mp[i][j]>='A'&&mp[i][j]<='J') bfs(i,j,mp[i][j]-'A');
else if (mp[i][j]=='@') bfs(i,j,);
}
} /*
//输出隐式图
for (i=0;i<12;i++)
{
for (j=0;j<12;j++)
printf("%d ",min_d[i][j]);
printf("\n");
}
*/ ans=-;
memset(dvis,,sizeof(dvis));
dvis[]=;
dfs(,,);//从入口开始,初始分数为0,步数0
printf("Case %d:\n",t1);
if(ans!=-) printf("The best score is %d.\n",ans);
else printf("Impossible\n");
if(t) printf("\n");
}
return ;
}
另外,还可以剪下枝
在DFS的时候可以,如果到拿这个宝物的时候,步数比以前的更大,并且价值更少,就不要去DFS了
可以优化到31ms
Collect More Jewels(hdu1044)(BFS+DFS)的更多相关文章
- 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 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(bfs+dfs+剪枝)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...
- hdu 1044 Collect More Jewels
题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- LeetCode:BFS/DFS
BFS/DFS 在树专题和回溯算法中其实已经涉及到了BFS和DFS算法,这里单独提出再进一步学习一下 BFS 广度优先遍历 Breadth-First-Search 这部分的内容也主要是学习了labu ...
随机推荐
- 项目笔记:导出Excel功能设置导出数据样式
/** * 导出-新导出 * * @return * @throws IOException */ @OperateLogAnn(type = OperateEnum.EXPORT, hibInter ...
- 对tensorflow 中的attention encoder-decoder模型调试分析
#-*-coding:utf8-*- __author = "buyizhiyou" __date = "2017-11-21" import random, ...
- go语言的一些特性
go语言中如何判断一个方法是私有的还是公有的?说出来你可能不信,通过首字母的大小写. 不管是一个变量还是一个函数,如果它的首字母是大写的,那么它就是包外可见的,也就是说可以 从这个包的外面访问这个资源 ...
- 配置Linux系统实现dhcp功能
配置Linux系统实现dhcp功能 1.背景及原理 DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用 ...
- C++003基础
1.C++对C的扩展 1简单的C++程序 1.1求圆的周长和面积 数据描写叙述: 半径.周长,面积均用实型数表示 数据处理: 输入半径 r. 计算周长 = 2*π*r : 计算面积 = π* r2 . ...
- UltraISO 9.6.5.3237
注册信息: 用户名:Guanjiu 注册码:A06C-83A7-701D-6CFC
- select、poll、epoll之间的区别总结[整理](转)
select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作.但select ...
- Nginx:管理HTTP模块的配置项
参考资料<深入理解Nginx> 一个nginx.conf的例子 http { mytest_num ; server { server_name A; listen ; mytest_nu ...
- 【BIEE】09_BIEE控制台乱码问题解决
BIEE安装完成后,点击[启动BI服务] 接着从弹出窗口可以发现,全部汉字都是乱码 出现这种情况,想看一下BIEE启动情况是很费劲的,接着我们处理一下这个问题 1.从路径D:\obiee\user_p ...
- SearchView的全面解析
代码地址如下:http://www.demodashi.com/demo/12535.html 前言 今天来讲讲searchView的使用,这里讲的searchView是引用android.suppo ...