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. 

解题思路: 

题目大意:在一个迷宫中,从起点走到终点,还有几个珠宝,问在给定的时间内,到达终点后所能获取珠宝的最大价值。我们可以先用bfs求出入口,宝物,出口,两两之间的最短距离。 再用dfs搜索所有情况,求出从入口走到出口能获得珠宝的最大价值。

程序代码: 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 55
int w,h,l,m,ans,sum;
int dis[N][N];//起点,珠宝,出口之间的距离 
int step[N][N];
int val[N];//珠宝价值 
bool vis[N][N];//bfs标记数组 
bool mark[N];//dfs标记数组 
char a[N][N];//存储地图 
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int ok(int x,int y)
{
	if(x<0||x>=h||y<0||y>=w)
		return 0;
	return 1;
}
//从点(x,y)到其他点的距离,z(0到m+1)是该点的编号,0是起点,m+1是出口 
void bfs(int x,int y,int z)
{
	queue<int> q;
	while(!q.empty())
		q.pop();
	memset(step,0,sizeof(step));
	memset(vis,false,sizeof(vis));
	step[x][y]=0;
	vis[x][y]=true;
	int p=x*w+y;
	q.push(p);
	while(!q.empty())
	{
		p=q.front();
		q.pop();
		int x=p/w;
		int y=p%w;
		for(int i=0;i<4;i++)
		{
			int tx=x+dir[i][0];
			int ty=y+dir[i][1];
			if(ok(tx,ty)&&vis[tx][ty]==false&&a[tx][ty]!='*')
			{
				vis[tx][ty]=true;
				step[tx][ty]=step[x][y]+1;
				if(a[tx][ty]=='@')
					dis[z][0]=step[tx][ty];
				else if(a[tx][ty]=='<')
					dis[z][m+1]=step[tx][ty];
				else if(a[tx][ty]>='A'&&a[tx][ty]<='J')
					dis[z][a[tx][ty]-'A'+1]=step[tx][ty];
				q.push(tx*w+ty);
			}
		}
	}
}
//当前收集珠宝价值,当前珠宝价值,消耗的时间 
void dfs(int s,int value,int time)
{
	if(time>l)//时间超出,直接返回 
		return ;
	if(ans==sum)//已经得到最大珠宝价值,返回 
		return ;
	if(s>m)
	{
		if(value>ans)
			ans=value;
		return ;
	}
	for(int i=0;i<=m+1;i++)
	{
		if(mark[i]||dis[s][i]==0)
			continue;
		mark[i]=true;
		dfs(i,value+val[i],time+dis[s][i]);
		mark[i]=false;
	}
}
int main()
{
	int t,Case=1;
	cin>>t;
	while(t--)
	{
		cin>>w>>h>>l>>m;
		sum=0;
		for(int i=1;i<=m;i++)
		{
			cin>>val[i];
			sum+=val[i];
		}
		val[0]=val[m+1]=0;//把起点和终点当做第一个和最后一个价值为0的珠宝 
		for(int i=0;i<h;i++)
			cin>>a[i];
		memset(dis,0,sizeof(dis));
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
			{
				if(a[i][j]=='@')
					bfs(i,j,0);
				else if(a[i][j]=='<')
					bfs(i,j,m+1);
				else if(a[i][j]>='A'&&a[i][j]<='J')
					bfs(i,j,a[i][j]-'A'+1);
			}
		}
		memset(mark,false,sizeof(mark));
		mark[0]=true;
		ans=-1;
		dfs(0,0,0);
		cout<<"Case "<<Case++<<":"<<endl;
		if(ans>=0)
			cout<<"The best score is "<<ans<<"."<<endl;
		else
			cout<<"Impossible"<<endl;
		if(t)
			cout<<endl;
	}
	return 0;
}