zoukankan      html  css  js  c++  java
  • HDU 1044 BFS

    Collect More Jewels

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7543    Accepted Submission(s): 1761


    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.
     
    Source
     

     题意:

    n*m的迷宫,@是出发点,*是墙,.是路,<是出口,大写字母A~J表示该点有价值,问在t时间之内能否走出迷宫,若能走出求经过的最大价值。

    输入T组数据

    输入列m,行n,时间t,有价值的点的个数p

    输入地图

    代码:

    //bfs,三维vis[i][j][k]标记数组,i,j表示点的位置,k表示到这个点时经过了多少有价值的点
    //因为最多只有10个所以可以用二进制表示,价值只能取一次。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    char mp[60][60];
    int T,n,m,t,p,val[15],ans;
    int dir[4][2]={1,0,-1,0,0,1,0,-1};
    bool vis[55][55][1<<10],cost[55][55];
    struct node{
        int x,y,cnt,sum,sta;
        node(){}
        node(int a,int b,int c,int d,int e):x(a),y(b),cnt(c),sum(d),sta(e){}
    }no1,no2;
    void bfs(int px,int py){
        memset(vis,0,sizeof(vis));
        memset(cost,0,sizeof(cost));
        no1=node(px,py,0,0,0);
        queue<node>q;
        q.push(no1);
        vis[px][py][0]=1;
        while(!q.empty()){
            no1=q.front();q.pop();
            if(no1.cnt>t) continue;
            if(mp[no1.x][no1.y]=='<'){
                if(ans==-1) ans=0;
                ans=max(ans,no1.sum);
            }
            for(int i=0;i<4;i++){
                int x=no1.x+dir[i][0],y=no1.y+dir[i][1];
                if(x<0||x>=n||y<0||y>=m) continue;
                if(mp[x][y]=='*') continue;
                if('A'<=mp[x][y]&&mp[x][y]<='J'){
                    if(no1.sta&(1<<(mp[x][y]-'A'))){
                        if(vis[x][y][no1.sta]) continue;
                        vis[x][y][no1.sta]=1;
                        no2=node(x,y,no1.cnt+1,no1.sum,no1.sta);
                        q.push(no2);continue;
                    }
                    int tmp=no1.sta^(1<<(mp[x][y]-'A'));
                    if(vis[x][y][tmp]) continue;
                    vis[x][y][tmp]=1;
                    no2=node(x,y,no1.cnt+1,no1.sum+val[mp[x][y]-'A'],tmp);
                    q.push(no2);
                }
                else{
                    if(vis[x][y][no1.sta]) continue;
                    vis[x][y][no1.sta]=1;
                    no2=node(x,y,no1.cnt+1,no1.sum,no1.sta);
                    q.push(no2);
                }
            }
        }
    }
    int main()
    {
        scanf("%d",&T);
        for(int cas=1;cas<=T;cas++){
            int px,py;
            scanf("%d%d%d%d",&m,&n,&t,&p);
            for(int i=0;i<p;i++) scanf("%d",&val[i]);
            for(int i=0;i<n;i++){
                scanf("%s",mp[i]);
                for(int j=0;j<m;j++)
                    if(mp[i][j]=='@') {px=i;py=j;}
            }
            ans=-1;
            bfs(px,py);
            printf("Case %d:
    ",cas);
            if(ans==-1) printf("Impossible
    ");
            else printf("The best score is %d.
    ",ans);
            if(cas!=T) printf("
    ");
        }
        return 0;
    }

    代码:

  • 相关阅读:
    Altium Designer如何从已有的PCB图中导出封装库
    获得内核函数地址的四种方法
    poj2976 Dropping tests
    poj3045 Cow Acrobats
    CF916C Jamie and Interesting Graph
    poj3104 Drying
    poj2455 Secret Milking Machine
    poj2289 Jamie's Contact Groups
    网络流最小路径覆盖
    CF897C Nephren gives a riddle
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6764765.html
Copyright © 2011-2022 走看看