Collect More Jewels

来源:互联网 发布:js取随机数 编辑:程序博客网 时间:2024/05/22 03:27

Collect More Jewels
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 19 Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.

数组 55

Source
//城堡寻宝,A~J分别代表宝物,给你每个的价值,@为起点,《为终点
怎么样才能在给定的时间范围l内走完并且获得宝物价值最大

bfs寻找最短路将宝物和起终点步数全部放进数组dis里面存,然后dfs寻找最大价值路线

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define N 55using namespace std;int w,h,l,m,ans,sum;int sx,sy,ex,ey;int a[15];int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}},vist[N][N];                          //|                          //|char s[N],map[N][N];      //vint dvis[15],vv[15],vs[15],dis[15][15]; //<----构造隐式图 用来dfs 0~9代表'A'~'J' 10~'@' 11~'<'struct node{    int x,y,step;};queue<node>q;void bfs(int sx,int sy,int k)     // bfs找最短路{    node st,ed;    int i,nx,ny,tx,ty,nstep,tmp;    while(!q.empty())    q.pop();    memset(vist,0,sizeof(vist));    st.x=sx;    st.y=sy;    st.step=0;    q.push(st);    vist[sx][sy]=1;    while(!q.empty())    {        st=q.front();        q.pop();        nx=st.x;        ny=st.y;        nstep=st.step;        if(nstep>l) break ;        if(nstep)        {            if(map[nx][ny]>='A'&&map[nx][ny]<='J')            {                tmp=map[nx][ny]-'A';                dis[k][tmp]=dis[tmp][k]=nstep;            }            else if(map[nx][ny]=='@')            {                tmp=10;                dis[k][tmp]=dis[tmp][k]=nstep;            }            else if(map[nx][ny]=='<')            {                tmp=11;                dis[k][tmp]=dis[tmp][k]=nstep;            }        }        for(i=0; i<4; i++)        {            tx=nx+dir[i][0];            ty=ny+dir[i][1];            if(map[tx][ty]!='*'&&!vist[tx][ty])            {                vist[tx][ty]=1;                ed.x=tx;                ed.y=ty;                ed.step=nstep+1;                q.push(ed);            }        }    }}int dfs(int k,int valu,int step)   // dfs搜最大值路径{    int i,flag=0;    if((step>l||vv[k]>=valu)&&(vs[k]<=step||ans==sum)) return 0;    if(k==11)    {        if(ans<valu) ans=valu;        return ans;    }    for(i=0;i<=11;i++)    {        if(dis[k][i]&&!dvis[i])        {            dvis[i]=1;            if(dfs(i,valu+a[i],step+dis[k][i]))            {                flag=1;                if(vv[k]<valu)                {                    vv[k]=valu;                    vs[k]=step;                }            }            dvis[i]=0;        }    }    if(flag) return 1 ;    return 0 ;}int main(){    int i,j,t,t1=0;    scanf("%d",&t);    while(t--)    {        t1++;        sum=0;        scanf("%d%d%d%d",&w,&h,&l,&m);        for(i=0; i<m; i++)        {            scanf("%d",&a[i]);            sum+=a[i];        }        a[10]=a[11]=0;        memset(map,'*',sizeof(map));        memset(dis,0,sizeof(dis));        for(i=1; i<=h; i++)        {            scanf("%s",s);            for(j=1; j<=w; j++)            {                map[i][j]=s[j-1];                if(s[j-1]=='@') sx=i,sy=j;                else if(s[j-1]=='<') ex=i,ey=j;            }        }        for(i=1;i<=h;i++)   // 从每个点出发 找到这个点到其他点的最短路        {            for(j=1;j<=w;j++)            {                if(map[i][j]=='@')                bfs(i,j,10);                else if(map[i][j]=='<')                bfs(i,j,11);                else if(map[i][j]>='A'&&map[i][j]<='J')                bfs(i,j,map[i][j]-'A');            }        }        if(t1>1) printf("\n");        printf("Case %d:\n",t1);        ans=-1;        memset(dvis,0,sizeof(dvis));        memset(vv,-1,sizeof(vv));        memset(vs,-1,sizeof(vs));        dvis[10]=1;        dfs(10,0,0);        if(ans!=-1) printf("The best score is %d.\n",ans);        else printf("Impossible\n");    }    return 0;}
0 0