Collect More Jewels
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6356 Accepted Submission(s): 1455
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.
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.
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....<* ************
Case 1: The best score is 200. Case 2: Impossible Case 3: The best score is 300.
题目大意:
有n*m这样的一个图,你初始的时候位于@处,终点位于<处,有时间限制的条件下,已知图里边有多少种宝石以及其每块宝石的价值,问最多能够拿出多少价值的宝石。
思路:
暴力Bfs+状态压缩。
1、对于每个点都会可能有重复行走的可能性,所以我们开判重数组要开三维的vis【x】【y】【tmp】,表示在x,y这个点上,身上有tmp这种情况的宝石是否走过。
2、对于身上有tmp这种情况的宝石是如何确定的呢?我们要枚举所有tmp的情况然然一一枚举?那我们会累死的.我们这里可以用到状态压缩的方法来解决,假如我们身上有了A和E宝石,那么我们可以将二进制01串的形式来表示现在这种tmp:10001。同理,假如我们身上有了A和和B和E宝石,那么我们可以将二进制01串的形式来表示现在这种tmp:10011。也就是说,我们用1代表有这个宝石,用0表示没有这个宝石。
3、对于宝石拿取的细节处理:因为每个点都有可能重复走,所以以前有宝石的地方也可以重复走,那么如何保证这种情况下不重复拿取宝石呢?我们可以用位运算中的与运算来解决这个问题,与运算的规则:同位都是1出1,否则都出0,我们可以根据这个特点来判断当前这个宝石是否已经拿取过了,只要判断一下now.tmp&(1<<宝石编号)是否为0即可,如果为0,表示没有拿过,如果为1,表示有拿过,如果没有拿过,我们nex.tmp=now.tmp+(1<<宝石编号),在二进制上操作将这个宝石放带在身上。
我们举例说明:
假如我们现在有A,E,now.tmp=17=10001;
如果我们遇到了A宝石:
显然17&1!=0。
如果我们遇到了B宝石:
显然17&2==0.
那么这个时候nex.tmp=17+2=19=10011;那么接下来如果再遇到B宝石我们也不用再担心重复拿取的问题辣!!
4、对于每种到终点的情况,都不一定是最优的,而且如果加入了优先队列的话,因为优先队列不仅仅优先了价值,还优先着先走出去是不行的,所以我们不能两个同时优先,所以我们只能放弃优先队列的使用,直接暴力搜、
思路构建完毕,剩下的就是代码实现了。
注意的点:如果操作系统不开心,会让我TLE,如果开心了,会让我AC,大概时间850ms+AC。所以如果大家照我的代码敲TLE了,没事,多交两发会AC的0.0
同一份代码TLE和AC图:
AC代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
struct zuobiao
{
int x,y,tmp,output,ti;
}now,nex;
int val[50];
char a[55][55];
int vis[55][55][1<<10];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int n,m,time,zhonglei,ok,ans;
void Bfs(int x,int y)
{
memset(vis,0,sizeof(vis));
now.x=x;
now.y=y;
now.output=0;
now.ti=0;
now.tmp=0;
queue<zuobiao >s;
s.push(now);
vis[x][y][0]=1;
while(!s.empty())
{
now=s.front();
if(a[now.x][now.y]=='<')
{
ok=1;
if(now.output>ans)ans=now.output;
}
s.pop();
for(int i=0;i<4;i++)
{
nex.x=now.x+fx[i];
nex.y=now.y+fy[i];
nex.output=now.output;
nex.ti=now.ti+1;
nex.tmp=now.tmp;
if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&a[nex.x][nex.y]!='*')
{
if(a[nex.x][nex.y]>='A'&&a[nex.x][nex.y]<='J')
{
int id=a[nex.x][nex.y]-'A';
if((nex.tmp&(1<<id))==0)
{
nex.output+=val[id];
nex.tmp+=(1<<id);
}
if(vis[nex.x][nex.y][nex.tmp]==0&&nex.ti<=time)
{
vis[nex.x][nex.y][nex.tmp]=1;
s.push(nex);
}
}
else
{
if(vis[nex.x][nex.y][nex.tmp]==0&&nex.ti<=time)
{
vis[nex.x][nex.y][nex.tmp]=1;
s.push(nex);
}
}
}
}
}
return ;
}
int main()
{
int t;
int kase=0;
int f=0;
scanf("%d",&t);
while(t--)
{
if(f!=0)
printf("\n");
f++;
int sx,sy;
scanf("%d%d%d%d",&m,&n,&time,&zhonglei);
for(int i=0;i<zhonglei;i++)
{
scanf("%d",&val[i]);
}
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
for(int j=0;j<m;j++)
{
if(a[i][j]=='@')
{
sx=i;
sy=j;
}
}
}
ans=0;
printf("Case %d:\n",++kase);
ok=0;
Bfs(sx,sy);
if(ok==1)
{
printf("The best score is %d.\n",ans);
}
else printf("Impossible\n");
}
}
/*
10
5 5 6 2
100 200
*****
*@A.*
*B<A*
*.A.*
*****
*/