Collect More Jewels

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

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.

TLE 无数发,QAQ,能够想到的剪枝都想了,终于AC..
题意:某人要从'@'走到'<' ,途中经过一些有宝石的点,这些点的宝石分别有自己的价值,'A'对应的是第一个宝石...依次类推,最多10个,现在某人要保证能够走到终点的同时尽量多收集宝石,问他最多能够收集多少宝石,不能走到终点输出Impossible.
题解:对每个点进行bfs,求出其到每个点的距离,接下来就做一次DFS即可得到答案,这题坑点在DFS时候的剪枝,当答案已经为sum(jewel[i])时就不用继续搜索了,不然的话会将全排列都搜索一遍,会超时。
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int INF = ;
int n,m,limit,num;
int dis[][]; ///记录两点之间的距离
int jw[];
struct Node{
int x,y,step,geshu;
}s;
char graph[][];
bool vis[][];
int dir[][]={{,},{-,},{,},{,-}};
bool check(int x,int y){
if(x<||x>n||y<||y>m||vis[x][y]||graph[x][y]=='*') return false;
return true;
}
void bfs(Node s,int k){
queue<Node> q;
vis[s.x][s.y] = true;
s.geshu = ;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.geshu==num+) return;
for(int i=;i<;i++){
Node next;
next.x = now.x+dir[i][];
next.y = now.y+dir[i][];
if(!check(next.x,next.y)) continue;
char c = graph[next.x][next.y];
next.step = now.step+;
next.geshu = now.geshu+;
if(c=='.') next.geshu-=;
if(c=='@'){
dis[k][] = next.step;
}
else if(c>='A'&&c<='J') {
dis[k][c-'A'+] = next.step;
}else if(c=='<'){
dis[k][num+] = next.step;
}
vis[next.x][next.y] = true;
q.push(next);
}
}
}
bool vis1[];
int MAX = ,sum;
void dfs(int u,int step,int ans){
if(step>limit || MAX == sum) return ; ///必须要加剪枝
if(u==num+){
MAX = max(MAX,ans);
return;
}
for(int i=;i<=num+;i++){
if(!vis1[i]){
vis1[i] = true;
dfs(i,step+dis[u][i],ans+jw[i]);
vis1[i] = false;
}
}
}
int main(){
int tcase;
scanf("%d",&tcase);
int kk = ;
while(tcase--){
for(int i=;i<;i++){
for(int j=;j<;j++){
dis[i][j] = (i==j)?:INF;
}
}
scanf("%d%d%d%d",&m,&n,&limit,&num);
sum = ;
for(int i=;i<=num;i++){
scanf("%d",&jw[i]);
sum+=jw[i];
}
jw[num+] = jw[] = ;
for(int i=;i<=n;i++){
scanf("%s",graph[i]+);
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(graph[i][j]=='@') {
memset(vis,false,sizeof(vis));
s.x = i,s.y = j,s.step=;
bfs(s,);
}
if(graph[i][j]=='<'){
memset(vis,false,sizeof(vis));
s.x = i,s.y = j,s.step=;
bfs(s,num+);
}
if(graph[i][j]>='A'&&graph[i][j]<='J'){
memset(vis,false,sizeof(vis));
s.x = i,s.y = j,s.step=;
bfs(s,graph[i][j]-'A'+);
}
}
}
printf("Case %d:\n",kk++);
if(dis[][num+]>limit){
printf("Impossible\n");
if(tcase) printf("\n");
continue;
}
memset(vis1,false,sizeof(vis1));
MAX = ;
vis1[] = true;
dfs(,,);
printf("The best score is %d.\n",MAX);
if(tcase) printf("\n");
}
}

hdu 1044(bfs+dfs+剪枝)的更多相关文章

  1. hdu - 1072(dfs剪枝或bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...

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

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

  3. hdu 1983(BFS+DFS) 怪盗Kid

    http://acm.hdu.edu.cn/showproblem.php?pid=1983 首先,题目要求出口和入口不能封闭,那么,只要把出口或入口的周围全给封闭了那盗贼肯定无法成功偷盗,出口或入口 ...

  4. hdu 1175(BFS&DFS) 连连看

    题目在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大家都很熟悉的连连看,原理基本就是这个,典型的搜索.这里用的是广搜.深搜的在下面 与普通的搜索 ...

  5. UVA-11882 bfs + dfs + 剪枝

    假设当前已经到达(x,y),用bfs判断一下还可以到达的点有maxd个,如果maxd加上当前已经经过的长度小于当前答案的长度就退出,如果相同,就将bfs搜索到的点从大到小排序,如果连最大序列都无法大于 ...

  6. HDU 1175 连连看 (DFS+剪枝)

    <题目链接> 题目大意:在一个棋盘上给定一个起点和终点,判断这两点是否能通过连线连起来,规定这个连线不能穿过其它的棋子,并且连线转弯不能超过2次. 解题分析:就是DFS从起点开始搜索,只不 ...

  7. hdu 1044(bfs+状压)

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

  8. HDU 1044 BFS

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

  9. hdu 1145(Sticks) DFS剪枝

    Sticks Problem Description George took sticks of the same length and cut them randomly until all par ...

随机推荐

  1. js判断设备类型

    1. 判断微信 function is_weixin() { var ua = window.navigator.userAgent.toLowerCase(); if (ua.match(/Micr ...

  2. X day1

    题目pdf 官方题解 T1: 我们可以发现此题若要求$[L,R]$区间的答案,其实就是再求前缀和,我们设$b$为当前出现次数最多的字符,$c$为最小,所以答案为$s[b]_r-s[c]_r-(s[b] ...

  3. mapper的前后缀

    1.<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=&quo ...

  4. c++模板类编译错误

    最近想写一个c++模板类,实现一个有向图.依据惯例,类在头文件中声明,类的实现写在源文件中.可是编译的时候出现了如下错误: undefined reference to 通过谷歌发现,这是一个很常见的 ...

  5. windows修改文件的修改或者创建时间

    https://www.online-tech-tips.com/computer-tips/how-to-change-the-last-modified-date-creation-date-an ...

  6. 省队集训 Day3 吴清华

    [题目大意] 给网格图,共有$n * n$个关键节点,横向.纵向距离均为$d$,那么网格总长度和宽度均为$(n+1) * d + 1$,最外围一圈除了四角是终止节点.要求每个关键节点都要通过线连向终止 ...

  7. [BZOJ2440]完全平方数解题报告|莫比乌斯函数的应用

    完全平方数 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱.  这天是小X的生日 ...

  8. 【转载】Lua中实现类的原理

    原文地址 http://wuzhiwei.net/lua_make_class/ 不错,将metatable讲的很透彻,我终于懂了. --------------------------------- ...

  9. pythonTensorFlow实现yolov3训练自己的目标检测探测自定义数据集

    1.数据集准备,使用label标注好自己的数据集. https://github.com/tzutalin/labelImg 打开连接直接下载数据标注工具, 2.具体的大师代码见下链接 https:/ ...

  10. bzoj 1084 DP

    首先对于m==1的情况非常容易处理(其实这儿因为边界我错了好久...),直接DP就好了,设f[i][k]为这个矩阵前i个选k个矩阵的最大和,那么f[i][k]=max(f[j][k-1]+sum[j+ ...