HDU 1044 Collect More Jewels(BFS+DFS)
Collect More Jewels
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6713 Accepted Submission(s): 1558
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.
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.
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.
4 4 2 2
100 200
****
*@A*
*B<*
****
4 4 1 2
100 200
****
*@A*
*B<*
****
12 5 13 2
100 200
************
*B.........*
*.********.*
*@...A....<*
************
The best score is 200.
Case 2:
Impossible
Case 3:
The best score is 300.
链接:HDU 1044
题目需要转换一下,先用bfs求每一个点到另外其他点的单源最短路dis[now][to],再用dfs进行剪枝搜索……,PE两发……
代码:
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=55;
struct info
{
int x;
int y;
int step;
info operator +(info b)
{
b.x+=x;
b.y+=y;
b.step+=step;
return b;
}
};
info direct[4]={{1,0,1},{-1,0,1},{0,1,1},{0,-1,1}};
int n,m,L,M,maxm,ans; char pos[N][N];
int vis[N][N];
int dis[N][N];
int value[15];
int dvis[15];
info S;
void init()
{
MM(pos,0);
MM(dis,INF);
MM(value,0);
maxm=ans=0;
}
bool check(const info &a)
{
return (a.x>=0&&a.x<n&&a.y>=0&&a.y<m&&!vis[a.x][a.y]&&pos[a.x][a.y]!='*'&&a.step<=L);
}
int getnum(const info &s)
{
if(pos[s.x][s.y]=='@')
return 0;
else if(pos[s.x][s.y]=='<')
return M+1;
else if(pos[s.x][s.y]>='A'&&pos[s.x][s.y]<='J')
return pos[s.x][s.y]-'A'+1;
}
void bfs(const info &s)
{
MM(vis,0);
queue<info>Q;
int ins,inr;
ins=getnum(s);
vis[s.x][s.y]=1;
Q.push(s);
while (!Q.empty())
{
info now=Q.front();
Q.pop();
for (int i=0; i<4; ++i)
{
info v=now+direct[i];
if(check(v))
{
vis[v.x][v.y]=1;
if(pos[v.x][v.y]!='.')
{
inr=getnum(v);
dis[ins][inr]=v.step;
}
Q.push(v);
}
}
}
}
void dfs(int now,int t,int v)
{
if(now==M+1)
ans=max(ans,v);
if(ans==maxm)
return ;
for (int i=0; i<=M+1; ++i)
{
if(t+dis[now][i]<=L&&!dvis[i])
{
dvis[i]=1;
dfs(i,t+dis[now][i],v+value[i]);
dvis[i]=0;
}
}
}
int main(void)
{
int tcase,i,j;
scanf("%d",&tcase);
for (int q=1; q<=tcase; ++q)
{
init();
scanf("%d%d%d%d",&m,&n,&L,&M);
for (i=1; i<=M; ++i)
{
scanf("%d",&value[i]);
maxm+=value[i];
}
for (i=0; i<n; ++i)
scanf("%s",pos[i]);
for (i=0; i<n; ++i)
{
for (j=0; j<m; ++j)
{
if(pos[i][j]!='*'&&pos[i][j]!='.')
{
S.x=i;
S.y=j;
bfs(S);
}
}
}
printf("Case %d:\n",q);
if(dis[0][M+1]==INF)
puts("Impossible");
else
{
dvis[0]=1;
dfs(0,0,0);
dvis[0]=0;
printf("The best score is %d.\n",ans);
}
if(q!=tcase)
putchar('\n');
}
return 0;
}
HDU 1044 Collect More Jewels(BFS+DFS)的更多相关文章
- hdu 1044 Collect More Jewels(bfs+状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- hdu.1044.Collect More Jewels(bfs + 状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Collect More Jewels(hdu1044)(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1044 Collect More Jewels
题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...
- hdu 4771 求一点遍历全部给定点的最短路(bfs+dfs)
题目如题.题解如题. 因为目标点最多仅仅有4个,先bfs出俩俩最短路(包含起点).再dfs最短路.)0s1A;(当年弱跪杭州之题,现看如此简单) #include<iostream> #i ...
- hdu1254(bfs+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 分析: 真正移动的是箱子,但是要移动箱子需要满足几个条件. 1.移动方向上没有障碍. 2.箱子后 ...
- HDU1254--推箱子(BFS+DFS)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
- 图的基本遍历算法的实现(BFS & DFS)复习
#include <stdio.h> #define INF 32767 typedef struct MGraph{ ]; ][]; int ver_num, edge_num; }MG ...
随机推荐
- WIN7实现多人远程一台电脑
今天查了查网,发现有人说,WIN7可以实现多人远程一台电脑,于是乎我就试了试, 在工作办公室里的局域网里试了试,嘿,成功了,愿与大家分享一下,呵呵! 方法一: 多用户早就能破解了 方法如下:用UE打开 ...
- javaScript中利用ActiveXObject来创建FileSystemObject操作文件
注:如果用javascript读本地文件,遇到安全问题. 需在浏览器中进行设置,如下: 工具—> Internet选项->安全->自定义级别->启用“没有标识为安全的A ...
- Android实现支持缩放平移图片
本文主要用到了以下知识点 Matrix GestureDetector 能够捕捉到长按.双击 ScaleGestureDetector 用于检测缩放的手势 自由的缩放 需求:当图片加载时,将图片在屏幕 ...
- 关于Android中RemoveView的错误理解
我以前一直以为,一个View被removeView了之后,就会被回收.其实不是这样的.如果有人引用它. 它还是会存在的.removeView和View被回收没有必然的关系.一个View被removeV ...
- 2016国产开源软件TOP100(Q1)
随着互联网的发展.开放标准的普及和虚拟化技术的应用等诸多IT新领域的创新及拓展,开源技术凭借其开放性.低成本.稳定性.灵活性.安全性和技术创新性等特点迅速走向成熟,逐步发展成为一种主流模式,日益改变着 ...
- Android权限设置android.permission
android.permission.ACCESS_NETWORK_STATE: 允许程序访问有关GSM网络信息(Allows applications to access information a ...
- HDU 4343 贪心
D - Interval queryTime Limit: 1.5 Sec Memory Limit: 256 MB Description This is a very simple questio ...
- 【JUnit 报错】java.lang.IncompatibleClassChangeError
使用Junit 测试spring时候报错: java.lang.IncompatibleClassChangeError: class org.springframework.core.LocalVa ...
- Android 编程下的四大组件之服务(Service)
服务(Service) 是一种在后台运行,没有界面的组件,由其他组件调用开始.Android 中的服务和 Windows 中的服务是类似的东西,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类 ...
- Collection总结一览
Java中集合大家族的成员实在是太丰富了,有常用的ArrayList.HashMap.HashSet,也有不常用的Stack.Queue,有线程安全的Vector.HashTable,也有线程不安全的 ...