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

 

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

  1 //BFS+DFS so good!
  2 //BFS 从每个点出发,找到这个点到其他点的最短路
  3 //用一个二维数组保留宝藏,入口,出口之间的最短距离
  4 //DFS 从起点出发,去创造最大价值,然后去出口
  5 
  6 #include <iostream>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <queue>
 10 #define maxn 52
 11 using namespace std;
 12 
 13 
 14 int n,m,l,cnt,ans,sum;
 15 int sx,sy,ex,ey;
 16 
 17 int dx[4]= {-1,1,0,0};
 18 int dy[4]= {0,0,-1,1};
 19 
 20 char mp[maxn][maxn];
 21 int jewel[12];//珠宝价值
 22 
 23 struct Node
 24 {
 25     int x,y;
 26     int step;
 27 };
 28 int bvis[maxn][maxn];//bfs的时候vis
 29 int min_d[12][12];   //宝藏,出入口之间最小距离
 30 
 31 int b_check(int x,int y)
 32 {
 33     if (x<0||x>=n||y<0||y>=m)
 34         return 0;
 35     if (mp[x][y]=='*'||bvis[x][y]==1)
 36         return 0;
 37     return 1;
 38 }
 39 
 40 
 41 int bfs(int x,int y,int k)
 42 {
 43     Node now,next;
 44     now.x=x;
 45     now.y=y;
 46     now.step=0;
 47     queue<Node> Q;
 48     Q.push(now);
 49     memset(bvis,0,sizeof(bvis));//每次BFS都可以继续走
 50     while (!Q.empty())
 51     {
 52         now=Q.front();
 53         Q.pop();
 54         if (now.step>l) break;
 55         bvis[now.x][now.y]=1;
 56         for (int i=0;i<4;i++)
 57         {
 58             next.x=now.x+dx[i];
 59             next.y=now.y+dy[i];
 60             next.step=now.step+1;
 61 
 62             if (b_check(next.x,next.y))//如果可以走
 63             {
 64                 if (mp[next.x][next.y]>='A'&&mp[next.x][next.y]<='J')
 65                 {
 66                     min_d[k][mp[next.x][next.y]-'A']=next.step;
 67                     min_d[mp[next.x][next.y]-'A'][k]=next.step;
 68                 }
 69                 else if(mp[next.x][next.y]=='@')
 70                 {
 71                     min_d[k][10]=next.step;
 72                     min_d[10][k]=next.step;
 73                 }
 74                 else if(mp[next.x][next.y]=='<')
 75                 {
 76                     min_d[k][11]=next.step;
 77                     min_d[11][k]=next.step;
 78                 }
 79                 bvis[next.x][next.y]=1;
 80                 Q.push(next);
 81             }
 82         }
 83     }
 84     while (!Q.empty()) Q.pop();
 85     return 0;
 86 }
 87 
 88 int dvis[12];//dfs的vis
 89 
 90 int dfs(int now,int sc,int step)
 91 {
 92     if (now<10) sc+=jewel[now];
 93 
 94     if (step>l||ans==sum) return 0;
 95 
 96     if (now==11&&sc>ans)
 97     {
 98         ans=sc;
 99         return 0;
100     }
101 
102     for (int i=0;i<12;i++)
103     {
104         if (min_d[now][i]!=-1&&dvis[i]==0)//如果可以去,并且没被拿过
105         {
106             dvis[i]=1;
107             dfs(i,sc,step+min_d[now][i]);
108             dvis[i]=0;
109         }
110     }
111 }
112 
113 int main()
114 {
115     int t,t1=0;
116     scanf("%d",&t);
117     while(t--)
118     {
119         t1++;
120 
121         sum=0;//珠宝总价值
122         int i,j;
123 
124         scanf("%d%d%d%d",&m,&n,&l,&cnt);
125         for (i=0;i<cnt;i++)
126         {
127             scanf("%d",&jewel[i]);
128             sum+=jewel[i];
129         }
130         getchar();
131         for (i=0;i<n;i++)
132         {
133             for (j=0;j<m;j++)
134             {
135                 scanf("%c",&mp[i][j]);
136                 if (mp[i][j]=='@') sx=i,sy=j;
137                 if (mp[i][j]=='<') ex=i,ey=j;
138             }
139             getchar();
140         }
141 
142         memset(min_d,-1,sizeof(min_d));//隐式图初始化
143         for (i=0;i<n;i++)
144         {
145             for (j=0;j<m;j++)
146             {
147                 if (mp[i][j]>='A'&&mp[i][j]<='J') bfs(i,j,mp[i][j]-'A');
148                 else if (mp[i][j]=='@') bfs(i,j,10);
149             }
150         }
151 
152         /*
153         //输出隐式图
154         for (i=0;i<12;i++)
155         {
156             for (j=0;j<12;j++)
157                 printf("%d ",min_d[i][j]);
158             printf("\n");
159         }
160         */
161         
162 
163         ans=-1;
164         memset(dvis,0,sizeof(dvis));
165         dvis[10]=1;
166         dfs(10,0,0);//从入口开始,初始分数为0,步数0
167         printf("Case %d:\n",t1);
168         if(ans!=-1) printf("The best score is %d.\n",ans);
169         else printf("Impossible\n");
170         if(t) printf("\n");
171     }
172     return 0;
173 }
View Code

 另外,还可以剪下枝

在DFS的时候可以,如果到拿这个宝物的时候,步数比以前的更大,并且价值更少,就不要去DFS了

可以优化到31ms

 

 

 

 

 

posted @ 2016-10-15 17:51  happy_codes  阅读(200)  评论(0编辑  收藏  举报