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

Collect More Jewels

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5345    Accepted Submission(s): 1189

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 #include<stdio.h>
 2 #include<queue>
 3 #include<string.h>
 4 #include<ctype.h>
 5 #include<algorithm>
 6 #include<math.h>
 7 int T , maxn ;
 8 int m , n , l , p ;
 9 int val[15] ;
10 char map[55][55] ;
11 bool vis[55][55][4000] ;
12 int move[][2] = {{1,0} ,{-1,0} , {0,1} , {0,-1}} ;
13 struct node
14 {
15     int x , y , step ;
16     int jew , v;
17 };
18
19 void bfs (int sx , int sy)
20 {
21     std::queue <node> q ;
22     while ( !q.empty ()) q.pop () ;
23     q.push ((node) {sx , sy , 0 , 0 , 0}) ;
24     node ans , tmp ;
25     vis[sx][sy][0] = 1 ;
26     while (!q.empty ()) {
27         ans = q.front () ; q.pop () ;
28       //  printf ("S---(%d,%d) %d = %d\n" , ans.x , ans.y , ans.step , ans.v) ;
29         if (map[ans.x][ans.y] == ‘<‘) maxn = std::max (maxn , ans.v) ;
30         for (int i = 0 ; i < 4 ; i ++) {
31             tmp.x = ans.x + move[i][0] ; tmp.y = ans.y + move[i][1] ;
32             if (tmp.x < 0 || tmp.y < 0 || tmp.x >= n || tmp.y >= m) continue ;
33             if (map[tmp.x][tmp.y] == ‘*‘) continue ;
34             tmp.jew = ans.jew ;
35             tmp.step = ans.step +1 ;
36             if (tmp.step > l) continue ;
37             int k = -1 ;
38             tmp.jew = ans.jew ; tmp.v = ans.v ;
39             if ( isalpha (map[tmp.x][tmp.y]) ) {
40                     k = map[tmp.x][tmp.y] - ‘A‘ ;
41                     if (! (tmp.jew & (1 << k))) tmp.v += val[k] ;
42                     tmp.jew = tmp.jew | (1 << k) ;
43             }
44             if ( vis[tmp.x][tmp.y][tmp.jew] ) continue ;
45           //  printf ("(%d,%d) %d = %d\n" , tmp.x , tmp.y , tmp.step , tmp.v) ;
46             vis[tmp.x][tmp.y][tmp.jew] = 1 ;
47             q.push (tmp ) ;
48         }
49     }
50 }
51
52 int main ()
53 {
54   // freopen ("a.txt" , "r" , stdin ) ;
55     int T , cas = 1 ;
56     scanf ("%d" , &T ) ;
57     while (T --) {
58         printf ("Case %d:\n" , cas ++) ;
59         int ex , ey , sx , sy ;
60         memset (vis , 0 , sizeof(vis)) ;
61         scanf ("%d%d%d%d" , &m , &n , &l , &p) ;
62         for (int i = 0 ; i < p ; i ++) scanf ("%d" , &val[i]) ;
63         for (int i = 0 ; i < n; i ++) scanf ("%s" , map[i]) ; //, puts (map[i]);
64         maxn = -1 ;
65         for (int i = 0 ; i < n; i ++) {
66             for (int j = 0 ; j < m ; j ++) {
67                 if (map[i][j] == ‘@‘)
68                     sx = i , sy = j ;
69                 else if (map[i][j] == ‘<‘)
70                     ex = i , ey = j ;
71             }
72         }
73         if (fabs (sx - ex ) + fabs (sy - ey) > l) {
74             printf ("Impossible\n") ;
75             if (T != 0) puts ("") ;
76             continue ;
77         }
78         bfs (sx , sy) ;
79         if (maxn != - 1) printf ("The best score is %d.\n" , maxn ) ;
80         else puts ("Impossible") ;
81         if (T != 0) puts ("") ;
82     }
83     return 0 ;
84 }

逗比的我把状压state | (1 << k) 一直写成 state | k ,然后....
有逗比的折腾出一种   + 宝藏  的方法,以后决定就先判定它有没有加入过,然后直接 + . (明明觉得很水的题orz)

时间: 2024-08-02 23:16:51

hdu.1044.Collect More Jewels(bfs + 状态压缩)的相关文章

HDU ACM 1044 Collect More Jewels BFS+DFS

题意:在一个迷宫中,有一些宝物,从起点走到终点,问在给定的时间内,到达终点后所能拾取珠宝的最大价值. 分析(BFS+DFS): 1.求入口到第一个取宝物的地方的最短距离 2.求第i个取宝物的地方到第i+1个取宝物的地方的最短距离 3.求第n个取宝物的地方到出口的最短距离 4.保证以上3点能在时间L内实现的情况下,取得的宝石价值最大. 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/Others) Total Submission(s): 6597    Accepted Submission(s): 1527 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

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): 6707    Accepted Submission(s): 1556 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

hdu 1044 Collect More Jewels

并没有做过这种类型的题目,不太会做...看了一下大神的代码,然后自己敲...额..思路一样了,代码也差不多.. http://www.cnblogs.com/kuangbin/archive/2012/08/14/2637512.html 先通过BFS预处理任意两点之间的距离,然后通过DFS暴力模拟路径,算出最优解. 感觉自己可能对BFS理解比DFS更深一点,或者说,BFS比较简单一点吧... 这题还有一种解法是状态压缩+BFS...通过开设一个int型变量记录是否拿到宝物,然后暴力..不过这种

hdu 5025 Saving Tang Monk (bfs+状态压缩)

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 41    Accepted Submission(s): 10 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi

hdu 1885 Key Task(bfs+状态压缩)

Problem Description The Czech Technical University is rather old — you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little

hdu 4856 Tunnels(bfs+状态压缩)

题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,现在有人想要体验下这M个管道,问最短需要移动的距离,起点未定. 解题思路:首先用bfs处理出两两管道之间移动的距离,然后后用状态压缩求出最短代价,dp[i][j],i表示的已经走过的管道,j是当前所在的管道. #include <cstdio> #include <cstring> #include <queue> #include <algorithm&g

HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP 用最短路预处理出状态的转移.能够优化非常多 AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <queue> us

HDU 3247 Resource Archiver (AC自动机 + BFS + 状态压缩DP)

题目链接:Resource Archiver 解析:n个正常的串,m个病毒串,问包含所有正常串(可重叠)且不包含任何病毒串的字符串的最小长度为多少. AC自动机 + bfs + 状态压缩DP 用最短路预处理出状态的转移.可以优化很多 AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <queue> using n