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 ...
随机推荐
- python核心编程学习记录之Web编程
cgi未完待续
- JS方面重点摘要(二)
1.函数声明与函数表达式 (1)变量声明会置顶提前,但赋值仍在原地方(2)函数声明同变量声明一样会提前:但是,函数表达式没有提前,就相当于平时的变量赋值(3)函数声明会覆盖变量声明,但不会覆盖变量赋值 ...
- ES6里关于数字的拓展
一.指数运算符 ES6引入的唯一一个JS语法变化是求幂运算符,它是一种将指数应用于基数的数学运算.JS已有的Math.pow()方法可以执行求幂运算,但它也是为数不多的需要通过方法而不是正式的运算符来 ...
- eclipse自动添加作者、日期等注释
使用eclipse的时候一般会添加自己的注释,标注日期作者等内容,我总结的添加注释的方式有两种:一.在新建class时自动添加注释:二.通过快捷键自动添加注释.下面分别描述一下添加方式. 一.新建cl ...
- JavaSE入门学习18:Java面向对象之多态
一Java多态 多态是同一个行为具有多个不同表现形式或形态的能力. 多态性是对象多种表现形式的体现.比方我们说"宠 物"这个对象.它就有非常多不同的表达或实现,比方有小猫.小狗.蜥 ...
- 【Android工具类】怎样保证Android与server的DES加密保持一致
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 在我们的应用程序涉及到比較敏感的数据的时候,我们一般会对数据进行简单的加密.在与server之间的数据交互中 ...
- python sqlite3 MySQLdb
SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. Python就 ...
- Oracle基础 索引
一.索引 索引是一种快速访问数据的途径,可提高数据库性能.索引使数据库程序无须对整个表进行扫描,就可以在其中找到所需的数据,就像书的目录,可以快速查找所需的信息,无须阅读整本书. (一)索引的分类 逻 ...
- python数据类型整理
Python中常见的数据结构可以统称为容器(container).序列(如列表和元组).映射(如字典)以及集合(set)是三类主要的容器. 一.序列(列表.元组和字符串) 序列中的每个元素都有自己的编 ...
- ES6 动态计算属性名
在ES5之前,如果属性名是个变量或者需要动态计算,则只能通过 对象.[变量名] 的方式去访问. <script type="text/javascript"> var ...