hdu1044 Collect More Jewels

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 advs 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.
一開始是用DFS寫的果斷超時了,打算去看大神的博客,但是在最後關頭忍住了沒點進去,不過眼睛一瞥看到了BFS+DFS,然後我就靈光一閃啊!!最後努力一個上午終於把這道題寫出來了。(事實證明不要看別人代碼也是能順利AC的)
下面是AC代碼.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<queue>
using namespace std;
int n,m,l,jew,v[11],p[51][51],ans,sum,mark[12];
char a[51][51];
int d[4][2]={{0,1},{0,-1},{1,0},{-1,0}},a2[12][12];
struct node
{
    int x,y,step;
};
int jud(int x,int y)
{
    if(x>0&&y>0&&x<=n&&y<=m&&!p[x][y]&&a[x][y]!='*')
        return 1;
    return 0;
}
void bfs(int x,int y,int temp)//壓縮狀態
{
    memset(p,0,sizeof(p));
    p[x][y]=1;
    queue<node> q;
    node cur,next;
    cur.x=x,cur.y=y,cur.step=0;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            next.x=cur.x+d[i][0];
            next.y=cur.y+d[i][1];
            if(pd(next.x,next.y))
            {
                next.step=cur.step+1;
                p[next.x][next.y]=1;
                q.push(next);
                if(a[next.x][next.y]=='@')
                    a2[temp][0]=next.step;
                if(a[next.x][next.y]=='<')
                    a2[temp][jew+1]=next.step;
                if(isalpha(a[next.x][next.y]))
                    a2[temp][a[next.x][next.y]-'A'+1]=next.step;
            }
        }
    }
}
void dfs(int now,int step,int maxx)
{
    if(step>l)
        return ;
    if(ans==sum)//最大的答案了,沒必要繼續搜下去
        return ;
    if(maxx>ans&&now==jew+1)
        ans=maxx;
    for(int i=0;i<=jew+1;i++)
    {
        if(a2[now][i]&&!mark[i])
        {
            mark[i]=1;
            if(i!=0&&i!=jew+1)
                dfs(i,step+a2[now][i],maxx+v[i]);
            else
                dfs(i,step+a2[now][i],maxx);
            mark[i]=0;
        }
    }
}
int main()
{
    int t,i,j,cas;
    scanf("%d",&t);
    for(cas=1;cas<=t;cas++)
    {
        scanf("%d%d%d%d",&m,&n,&l,&jew);
        sum=0;
        for(i=1;i<=jew;i++)
        {
            scanf("%d",&v[i]);
            sum+=v[i];
        }
        getchar();
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
                scanf("%c",&a[i][j]);
            getchar();
        }
        memset(a2,0,sizeof(a2));
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
            {
                if(a[i][j]=='@')
                    bfs(i,j,0);
                if(a[i][j]=='<')
                    bfs(i,j,jew+1);
                if(isalpha(a[i][j]))
                    bfs(i,j,a[i][j]-'A'+1);
            }
        ans=-1;
        memset(mark,0,sizeof(mark));
        mark[0]=1;
        dfs(0,0,0);
        if(cas!=1)
            printf("\n");
        printf("Case %d:\n",cas);
        if(ans!=-1)
            printf("The best score is %d.\n",ans);
        else
            printf("Impossible\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章