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 ...
随机推荐
- Mac安装pstree
brew install pstree pstree(选项) -a:显示每个程序的完整指令,包含路径,参数或是常驻服务的标示: -c:不使用精简标示法: -G:使用VT100终端机的列绘图字符: -h ...
- UVa 10192 - Vacation & UVa 10066 The Twin Towers ( LCS 最长公共子串)
链接:UVa 10192 题意:给定两个字符串.求最长公共子串的长度 思路:这个是最长公共子串的直接应用 #include<stdio.h> #include<string.h> ...
- EffectiveJava(7)避免使用终结方法
避免使用终结方法 1.使用终结方法会导致行为不稳定,性能降低,以及可移植性的问题.(终结线程的优先级过低) 终结方法不能保证被及时的执行(从一个对象变得不可到达开始,到中介方法被执行,所花费的时间是任 ...
- pip 安装自己开发模块 边调试边修改
pip install -e /path/to/mypackage
- Android学习(一) 按钮的事件
用户登录 1.内部匿名类方式实现 layout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...
- 扩充STL-编写自己的迭代器
这里的迭代器能够与STL组件共同工作,是对STL的一种扩充. 自定义迭代器必须提供iterator_traits的五种特性,分别是迭代器类型.元素类型.距离类型.指针类型与reference类型. ...
- ionic2项目创建回顾 及 react-native 报错处理
ionic2: 1.创建项目: ionic start MyIonic2Project tutorial --v2 (下载 tutorial 模板来初始化项目) ionic start MyIonic ...
- javascript 冒泡排序算法
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- Websphere: Stop Server and Uninstall Application
In WAS, stopping server and uninstalling application are important steps to re-deploy. SET ProfileLo ...
- jenkins调用shell脚本 输出带颜色字体
jenkins需要安装AnsiColor插件在构建环境项选择“color ansi console output” 安装插件AnsiColor shell 脚本相关颜色设置 echo -e " ...