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 ...
随机推荐
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- UVa 156 Ananagrams(STL,map)
Ananagrams Most crossword puzzle fans are used to anagrams--groups of words with the same letters ...
- 错误:“The requested resource () is not available.”的处置
做网页过程中,某页需要以新窗方式打开另一个网页,于是url是这样写: pages/test/transw/claimer.html 但是,点链接后网页提示 The requested resource ...
- Win7提示1970-01-01 000000 is not a valid data怎么办.
1 单击屏幕右下角的时间按钮 2 选个更改日期和时间,更改日历设置 3 把短日期改成"yyyy-m-d" 4 确定即可.发现日期的表示形式变了.
- Unity里面的自动寻路(一)
来自:http://www.narkii.com/club/forum.php?mod=viewthread&tid=269146&highlight=Unity%E9%87%8C%E ...
- Python——os.path.dirname(__file__) 与 os.path.join(str,str)
Python os.path.dirname(__file__) Python os.path.join(str,str) (1).当"print os.path.dirname(__f ...
- React Native 爬坑之路
1.react 基础 (创建组件及在浏览器上渲染组件) <!DOCTYPE html> <html lang="en"> <head> < ...
- windows超过最大连接数解决命令
query user /server:218.57.146.175 logoff 1 /server:218.57.146.175
- 匿名函数 invoke
delegate string MyDele(string str); string MyFun(string str) { return str; } private void Form1_Load ...
- GDBus
1. https://en.wikipedia.org/wiki/D-Bus In computing, D-Bus (for "Desktop Bus"[4]), a softw ...