HDU1044 Collect More Jewels(BFS+DFS+地圖壓縮)

來源:互聯網
上載者:User
                                                  
Collect More Jewels

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

Problem DescriptionIt 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. 

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

OutputResults 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

34 4 2 2100 200*****@A**B<*****4 4 1 2100 200*****@A**B<*****12 5 13 2100 200*************B.........**.********.**@...A....<*************
 

Sample Output

Case 1:The best score is 200.Case 2:ImpossibleCase 3:The best score is 300.
 

SourceAsia 2005, Hangzhou (Mainland China), Preliminary
 

RecommendJGShining

解題思路:本題需要做地圖壓縮,要是直接(BFS+DFS)搜尋的話,思路會很混亂,還會逾時。
          咋地一看題目,第一感覺就是,BFS+DFS,也就是說BFS求出所有可以走得地方到終點的距離,然後再DFS全圖,果斷逾時了。後面想到地圖壓縮,直接BFS全圖,求出各寶石、起點、終點兩兩之間的距離,然後用數組儲存形成新的地圖。然後再用DFS搜尋新的地圖得出答案(相關基礎題目(連結):HDU2614 Beat)。
        主要問題有二:
        (一)寶石的定位與定價問題,該問題無法解決將導致BFS無法進行,無法形成新地圖。直接用結構體數組儲存(x,y,val)即可,用的時候,直接記錄下標即可找到寶石的地圖位置和價值(需要把起點和終點處理下,分別放到數組的第一個和最後一個下標的儲存內容中(相對於W))。
         (二)DFS搜尋序問題,該問題無法解決將導致答案錯誤。若再該題代碼中無法理解,請到基礎題連結,這裡不分析。
        剪枝:只要剪掉無法到達終點和已經遍曆過的的枝節即可(BFS中的inmap(next.x,next.y)&&!flag[next.x][next.y]&&map[next.x][next.y]!='*';DFS中的:寶石沒撿過(不會重撿),時間還可以走到終點(不會逾時),寶石撿完標記以及main函數中的,若起點就走不到終點,直接輸出Impossible)

#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;int n,m,l,w;char map[50][50];int flag[50][50];int time1[14][14];int flag_jews[14];int dir[4][2]={1,0,0,1,-1,0,0,-1};   //四個可以走的方向,用於bfsint f,max_valu;struct node    //用於bfs{    int x;    int y;    int t;};struct node2  //儲存寶石情況,用於dfs{    int x;    int y;    int val;}jews[12];int inmap(int x,int y)//判斷點(x,y)是否在地圖內,用於bfs{    if(x>=0&&x<n&&y>=0&&y<m)        return 1;    return 0;}int bfs(int x,int y,int x1,int y1) //求點(x,y)到點(x1,y1)之間的距離{    int i;    node first,next;    queue<node> q;    if(x==x1&&y==y1)        return 0;    memset(flag,0,sizeof(flag));    first.x=x;    first.y=y;    first.t=0;    flag[x][y]=1;    q.push(first);    while(!q.empty())    {        first=q.front();        q.pop();        for(i=0;i<4;i++)        {            next.x=first.x+dir[i][0];            next.y=first.y+dir[i][1];            next.t=first.t+1;            if(inmap(next.x,next.y)&&!flag[next.x][next.y]&&map[next.x][next.y]!='*')            {                if(next.x==x1&&next.y==y1)                    return next.t;                flag[next.x][next.y]=1;                q.push(next);            }        }    }    return 10000000;}void dfs(int r,int ld,int w1,int val)//第r個寶石,剩下ld時間,還有W個寶石沒有撿,當前已有寶石的價值{    if(w==0)  //寶石撿完了        return ;    for(int i=1;i<w+1;i++)    {        if(!flag_jews[i]&&time1[i][w+1]<=ld-time1[r][i]) //寶石沒撿過(不會重撿),時間還可以走到終點(不會逾時)        {            val+=jews[i].val;            flag_jews[i]=1;            max_valu=val>max_valu?val:max_valu;            dfs(i,ld-time1[r][i],w1-1,val);            val-=jews[i].val;            flag_jews[i]=0;        }    }}int main(){    int t;    int i,j,k;    scanf("%d",&t);    for(k=1;k<=t;k++)    {        f=0;        max_valu=0;        scanf("%d%d%d%d",&m,&n,&l,&w);        jews[0].val=0;        jews[w+1].val=0;        for(i=1;i<=w;i++)            scanf("%d",&jews[i].val);        for(i=0;i<n;i++)  //地圖輸入處理        {            scanf("%s",map[i]);            for(j=0;j<m;j++)            {                if(map[i][j]>='A'&&map[i][j]<='J')  //此處有寶石                {                    jews[map[i][j]-'A'+1].x=i;                    jews[map[i][j]-'A'+1].y=j;                }                else if(map[i][j]=='@')    //起點                {                    jews[0].x=i;                    jews[0].y=j;                    map[i][j]='.';                }                else if(map[i][j]=='<')    //終點                {                    jews[w+1].x=i;                    jews[w+1].y=j;                    map[i][j]='.';                }            }        }        for(i=0;i<w+2;i++)   //各寶石堆,起點,終點的距離            for(j=0;j<w+2;j++)                time1[i][j]=time1[j][i]=bfs(jews[i].x,jews[i].y,jews[j].x,jews[j].y);        if(k!=1)            printf("\n");        printf("Case %d:\n",k);        if(time1[0][w+1]>l)         printf("Impossible\n");  //起點直接走到終點,逾時        else        {            memset(flag_jews,0,sizeof(flag_jews));            flag_jews[0]=1;            dfs(0,l,w,0);            printf("The best score is %d.\n",max_valu);        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.