HDU--1044--深搜+广搜



Collect More Jewels

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


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.
对于这个WA了N遍的搜索题我也是不想再吐槽自己了,代码什么的都不重要。。主要是理解思想。。
首先利用广度优先搜索,分别列出由起始点到各个宝物之间的距离,起始点到达出口的距离,各个宝物到达出口的距离。
得出距离之后再利用深度优先搜索,去搜索这个所求的最大值。
宝物有M个,则可以设定,0为起始点,而M+1为出口。dyx
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int W,H,L,M;//区域是H行W列的,L是时间限制,M是宝石的数量
int val[60];//宝石的价值,1-M
char map[60][60];
bool used[60][60];//BFS时访问标记
bool vis[60];//dfs时访问标记
int step[60][60];
int ans;//结果
int sum;//所有的宝石的价值总和
int move[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int dis[60][60];//记录初始位置、各宝石和出口两两间的距离
queue<int>q;

//0表示初始位置
//M+1表示出口,1-M表示第i个宝物堆
void bfs(int x1,int y1,int s)
{
    while(!q.empty())
    q.pop();
    memset(used,false,sizeof(used));
    memset(step,0,sizeof(step));
    int u=x1*W+y1;
    //因为y1<=W,所以,u/W=x1,也有可能u/W=x1+1,但因为数组由0开始,所以+1不影响,后面会有判断。
    //u%w,为y1.
    q.push(u);
    used[x1][y1]=true;
    //注意每次搜索的初始距离都必须要初始化为0;
    step[x1][y1]=0;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        int x=u/W;
        int y=u%W;
        for(int i=0;i<4;i++)
        {
            //在广度优先搜索中,按照据开始状态由近及远的顺序进行搜索。
            //在每一次的搜索中,初始值x是不会变化的,知道下一次搜索将状态传入队列中,才会变化
            int xx=x+move[i][0];
            int yy=y+move[i][1];
            if(xx<0||xx>=H||yy<0||yy>=W)continue;
            if(used[xx][yy]||map[xx][yy]=='*')continue;

            used[xx][yy]=true;
            step[xx][yy]=step[x][y]+1;
            if(map[xx][yy]=='@')dis[s][0]=step[xx][yy];
            else if(map[xx][yy]=='<') dis[s][M+1]=step[xx][yy];
            else if(map[xx][yy]>='A'&&map[xx][yy]<='J')
                dis[s][map[xx][yy]-'A'+1]=step[xx][yy];
            q.push(xx*W+yy);//将新的状态加入队列。
        }
    }
}

//dfs,深度优先搜索当中,如果进入某个条件不符合,则retrun 会返回上一个状态
 //s表示当前点,value表示获得的价值,time表示花费的时间
void dfs(int s,int value,int time)
{
    if(time>L)return;//超出限制时间
    if(ans==sum) return;//已经得到了最大的价值,剪枝
    if(s>M)
    {
        if(value>ans)ans=value;
        return;
    }
    for(int i=0;i<=M+1;i++)
    {
        if(dis[s][i]==0||vis[i])continue;
        vis[i]=true;
        dfs(i,value+val[i],time+dis[s][i]);
        vis[i]=false;
    }
}
int main()
{
    
    int T;
    scanf("%d",&T);
    int iCase=0;
    while(T--)
    {
        // freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
        memset(dis,0,sizeof(dis));
        iCase++;
        scanf("%d%d%d%d",&W,&H,&L,&M);
        sum=0;
        ans=-1;
        for(int i=1;i<=M;i++)
        {
            scanf("%d",&val[i]);
            sum+=val[i];
        }
        val[0]=val[M+1]=0;//这个很重要啊,WR到死啊
        for(int i=0;i<H;i++)
          scanf("%s",&map[i]);
        for(int i=0;i<H;i++)
          for(int j=0;j<W;j++)
          {
              if(map[i][j]=='@') bfs(i,j,0);
              else if(map[i][j]=='<') bfs(i,j,M+1);
              else if(map[i][j]>='A'&&map[i][j]<='J')
                 bfs(i,j,map[i][j]-'A'+1);

          }
        memset(vis,false,sizeof(vis));
        vis[0]=true;
        dfs(0,0,0);

        printf("Case %d:\n",iCase);
        if(ans>=0)printf("The best score is %d.\n",ans);
        else printf("Impossible\n");

        if(T>0)printf("\n");

    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值