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.
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<ctype.h>
using namespace std;
const int maxn=50+10;
int dr[]={-1,1,0,0};
int dc[]={0,0,-1,1};
int R,C,t,M;
int sum,ans;//sum是所有珠宝价值和,ans是解
char map[maxn][maxn];
int abc[maxn][maxn];//abc[i][j]=x表第i个有效点到第j个有效点之间的距离,如果为0表示不存在路径
int dist[maxn][maxn], vis1[maxn][maxn];//BFS时使用
//注意dist[r][c]表示BFS中的id点到(r,c)的最短距离,其中id为0到M+1.而vis1[r][c]类似
int vis2[maxn]; //DFS时使用
int val[maxn];//宝珠的价值,宝珠从1到M编号
void BFS(int r,int c,int id)
{
queue<int> Q;
vis1[r][c]=1;
dist[r][c]=0;
Q.push(r*C+c); //错误1,这里及后面写成了r*R+c了
while(!Q.empty())
{
int z=Q.front(); Q.pop();
int x=z/C, y=z%C;
for(int d=0;d<4;d++)
{
int xx=x+dr[d], yy=y+dc[d];
if(xx<0||xx>=R||yy<0||yy>=C||map[xx][yy]=='*') continue;
if(vis1[xx][yy]) continue;
vis1[xx][yy]=1;
dist[xx][yy]= dist[x][y]+1;
if(map[xx][yy]=='@') abc[id][0] = dist[xx][yy];
else if(isalpha(map[xx][yy])) abc[id][map[xx][yy]-'A'+1] = dist[xx][yy];
else if(map[xx][yy]=='<') abc[id][M+1]=dist[xx][yy];
Q.push(xx*C+yy);
}
}
}
void dfs(int p,int s,int time)//当前在第p个有效点,拥有价值s的珠宝,花了time时间
{
if(time>t || ans==sum) return; //剪枝,朝时了或已经得到最优解了
if(p==M+1 && s>ans) ans=s; //到达了终点且总价值s>ans,值得更新
else for(int i=1;i<=M+1;i++)
{
if(vis2[i]==1 || abc[p][i]==0) continue; //第i个有效点已经被走过或不存在从p到i的路径
vis2[i]=1;
dfs(i,s+val[i],time+abc[p][i]);
vis2[i]=0;
}
}
int main()
{
int T; scanf("%d",&T);
for(int kase=1;kase<=T;kase++)
{
sum=0;
memset(abc,0,sizeof(abc)); //错误2,忘记了对abs初始化
memset(vis2,0,sizeof(vis2));
scanf("%d%d%d%d",&C,&R,&t,&M);
for(int i=1;i<=M;i++) scanf("%d",&val[i]), sum+=val[i];
val[0]=val[M+1]=0;
for(int i=0;i<R;i++) scanf("%s",map[i]);
for(int i=0;i<R;i++)
for(int j=0;j<C;j++)
{
if(map[i][j]!='@'&&map[i][j]!='<'&&!isalpha(map[i][j])) continue;//非有效点
memset(vis1,0,sizeof(vis1));
memset(dist,0,sizeof(dist));
if(map[i][j]=='@') BFS(i,j,0);
else if(isalpha(map[i][j])) BFS(i,j,map[i][j]-'A'+1);
else if(map[i][j]=='<') BFS(i,j,M+1);
}
vis2[0]=1;
ans=-1;
dfs(0,0,0);//当前在0号点,处于0秒时间且已经获得价值0的珠宝
if(ans>=0) printf("Case %d:\nThe best score is %d.\n",kase,ans);
else printf("Case %d:\nImpossible\n",kase);
if(kase<T) puts("");
}
return 0;
}