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

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.

//第一行是测试组数

然后每组第一行是 地图的宽,地图的长,时间,宝物数量

然后是宝物的价值

然后是地图

走一格算 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)的更多相关文章

  1. HDU 1044 Collect More Jewels(BFS+DFS)

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

  2. hdu 1044 Collect More Jewels(bfs+状态压缩)

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

  3. hdu.1044.Collect More Jewels(bfs + 状态压缩)

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

  4. hdu 1044(bfs+dfs+剪枝)

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

  5. POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)

    思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...

  6. 邻结矩阵的建立和 BFS,DFS;;

    邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...

  7. hdu 1044 Collect More Jewels

    题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...

  8. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  9. LeetCode:BFS/DFS

    BFS/DFS 在树专题和回溯算法中其实已经涉及到了BFS和DFS算法,这里单独提出再进一步学习一下 BFS 广度优先遍历 Breadth-First-Search 这部分的内容也主要是学习了labu ...

随机推荐

  1. 装饰者模式对HttpServletRequest进行增强

    package cn.web.servlet; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpS ...

  2. BP神经网络(手写数字识别)

    1实验环境 实验环境:CPU i7-3770@3.40GHz,内存8G,windows10 64位操作系统 实现语言:python 实验数据:Mnist数据集 程序使用的数据库是mnist手写数字数据 ...

  3. JS或jQuery获取当前屏幕宽度

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...

  4. 【HTML 元素】标记文字

    1.用基本的文字元素标记内容 先看显示效果: 对应HTML代码: <!DOCTYPE html> <html lang="en"> <head> ...

  5. Import CSV into DB using SSIS

    Step 1: create a table CREATE TABLE [EmployeeImported]( ,) NOT NULL, [ContactID] [int] NOT NULL, [Ma ...

  6. hdu - 4974 - A simple water problem(贪心 + 反证)

    题意:N个队(N <= 100000),每一个队有个总分ai(ai <= 1000000),每场比赛比赛两方最多各可获得1分,问最少经过了多少场比赛. 题目链接:http://acm.hd ...

  7. react-native 项目实战 -- 新闻客户端(1) -- 初始化项目结构

    1.在项目根目录新建Componet文件夹(专门用来放我们的自定义组件),里面新建Main.js.Home.js.Message.js.Mine.js.Find.js 2.修改 index.andro ...

  8. d3.js封装文本实现自动换行和旋转平移等功能

    我们下面话不多说,本文主要介绍的是利用D3.js封装文本实现自动换行功能的步骤,下面来一起看看吧. 一.引用 multext.js 文件 multext.js function appendMulti ...

  9. Solution to Triangle by Codility

    question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one ...

  10. const readonly

    静态常量(compile-time constants)静态常量是指编译器在编译时候会对常量进行解析,并将常量的值替换成初始化的那个值. 动态常量(runtime constants)而动态常量的值则 ...