搜索练习8//hdu/problem4771 /Stealing Harry Potter's Precious/bfs+dfs较难的搜索题

http://acm.hdu.edu.cn/showproblem.php?pid=4771

Stealing Harry Potter's Precious

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3715    Accepted Submission(s): 1641


Problem Description
  Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Vernon never allows such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:



  Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers' properties, so they live in the indestructible rooms and put customers' properties in vulnerable rooms. Harry Potter's precious are also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can't access the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry's precious are. He wants to collect all Harry's precious by as less steps as possible. Moving from one room to another adjacent room is called a 'step'. Dudely doesn't want to get out of the bank before he collects all Harry's things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.
 

Input
  There are several test cases.
  In each test cases:
  The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).
  Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, '.' means a vulnerable room, and the only '@' means the vulnerable room from which Dudely starts to move.
  The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter's precious in the bank.
  In next K lines, each line describes the position of a Harry Potter's precious by two integers X and Y, meaning that there is a precious in room (X,Y).
  The input ends with N = 0 and M = 0
 

Output
  For each test case, print the minimum number of steps Dudely must take. If Dudely can't get all Harry's things, print -1.
 

Sample Input
  
  
2 3 ##@ #.# 1 2 2 4 4 #@## .... #### .... 2 2 1 2 4 0 0
 

Sample Output
  
  
-1 5
题意:现在有一个@他要到k个点捡东西,求 (全部捡完的) 最短距离;如果捡不完输出-1。

思路:因为k很小(不要把bfs弄得太复杂))(这道题只是把最短路和搜索合到一块,把这两部分分开想),首先要用搜索把所有两个物品的距离求出来,然后就是最短路问题了。(没错就是这么简单,但是比赛的时候思路没想到,赛后,自己要认真补题了,认真思考到底哪里没想到,不要把问题复杂化)

自己打的AC代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int INF = 1e8;
const int inf = 0x3f3f3f3f;
const int maxx = 100100;
const int N = 105;
int n,m,k;
char ma[N][N];//存地图
int v[N][N];//bfs的标记
int mp[6][6];//存最短路距离
int dx[5]= {0,0,1,-1};
int dy[5]= {1,-1,0,0};
struct node
{
    int x,y,step;
} r[6];//r数组用来存每个点的位置。
int bfs(node star,node endd)//bfs计算两点间的距离。
{
    if(star.x==endd.x&&star.y==endd.y)//后台数据会有两点在同一位置的情况。
        return 0;
    node p1,p2;//下面就是bfs模板了
    queue<node>q;
    while(!q.empty())
        q.pop();
    star.step=0;
    v[star.x][star.y]=1;
    q.push(star);
    while(!q.empty())
    {
        p1=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            p2=p1;
            p2.x+=dx[i];
            p2.y+=dy[i];
            if(p2.x<0||p2.x>=n||p2.y<0||p2.y>=m)continue;
            if(ma[p2.x][p2.y]!='#'&&!v[p2.x][p2.y])
            {
                v[p2.x][p2.y]=1;
                p2.step++;
                if(p2.x==endd.x&&p2.y==endd.y)
                    return p2.step;
                q.push(p2);
            }
        }
    }
    return INF;
}
int vv[N],ans;
void dfs(int h,int num,int sum)
{
    if(num==k+1)
    {
        if(sum<ans)
            ans=sum;
        return ;
    }
    for(int i=1; i<=k; i++)//每次取都有k种情况,取过不能再取。从原点出发,所以i从1开始
    {
        if(!vv[i])
        {
            vv[i]=1;
            dfs(i,num+1,sum+mp[h][i]);
            vv[i]=0;
        }
    }
    return ;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
            break;
        for(int i=0; i<n; i++)
            scanf("%s",ma[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                if(ma[i][j]=='@')
                    r[0].x=i,r[0].y=j;
        scanf("%d",&k);
        int l,ll;
        for(int i=1; i<=k; i++)
        {
            scanf("%d%d",&l,&ll);
            r[i].x=l-1;
            r[i].y=ll-1;
        }
        for(int i=0; i<k; i++)
            for(int j=i+1; j<=k; j++)
            {
                memset(v,0,sizeof(v));
                mp[i][j]=mp[j][i]=bfs(r[i],r[j]);
            }
        memset(vv,0,sizeof(vv));
        int flag=0;
        for(int i=1; i<=k; i++)
            if(mp[0][i]==INF)//判断原点是否能走到其他点,若走不到输出-1;
                flag=1;
        if(flag)
            printf("-1\n");
        else
        {
            ans=inf;
            dfs(0,1,0);//用bfs求最短路
            printf("%d\n",ans);
        }
    }
}










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值