HDU 4771 Stealing Harry Potter's Precious(bfs+dfs)

本博客介绍了一道经典的路径寻找问题,通过使用BFS和DFS算法解决如何以最少步数收集所有宝藏的问题。需要处理地图网格上的障碍物和目标点,最终实现对最短路径的有效计算。

Stealing Harry Potter's Precious

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


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
 
题目大意:#表示不能走,@起点, · 可以走,然后给出几个宝物的位置,坐标,求拿到全部宝物要走的路程,不能走完输出-1

先用bfs求出没两个宝物见的最短路,然后在最短路这个图上用dfs找到最短要走的路程即可,也可以用状压dp来写,这里给出dfs代码
ps:队友想到用kruscal,并查集来写这个问题,但这样只能求出所有宝物连起来的最短路,但是这里求得是走的路程,可能重复走一条路,所以此法不可行

题目链接:点击打开链接

代码注释:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define N 105
#define inf 0x7ffffff
int visit[N][N],vis[N],dis[N][N],n,m,Min,cc;//visit用作bfs的标记,vis用作dfs的标记,dis存两点间最短路,cc宝物总个数
char map[N][N];
int dir[2][4]= {1,-1,0,0,0,0,1,-1};

struct node
{
    int x,y,step;//step记载最短路
} good[N],cur,tem;

bool ok(int a,int b)
{
    if(a<1||a>n||b<1||b>m)
        return false;
    return true;
}
void dfs(int index,int sum,int c)
{
    if(c==cc)
    {
        Min=min(Min,sum);
        return ;
    }
    for(int i=1;i<=cc;i++)
    {
        if(!vis[i])
        {
            vis[i]=1;
            dfs(i,dis[index][i]+sum,c+1);
            vis[i]=0;
        }
    }
}

int bfs(node s,node e)
{
    memset(visit,0,sizeof(visit));
    queue<node>q;
    while(!q.empty())
        q.pop();
    if(s.x==e.x&&s.y==e.y)//同一个点,返回距离0
        return 0;
    visit[s.x][s.y]=1;
    q.push(s);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        for(int k=0; k<4; k++)
        {
            int X=tem.x=cur.x+dir[0][k];
            int Y=tem.y=cur.y+dir[1][k];
            if(map[X][Y]!='#'&&!visit[X][Y]&&ok(X,Y))
            {
                visit[X][Y]=1;
                tem.step=cur.step+1;
                if(X==e.x&&Y==e.y)
                    return tem.step;
                q.push(tem);
            }
        }
    }
    return -1;//没有路返回-1
}
int main()
{
    int i,j;
    while(cin>>n>>m,m+n)
    {
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=m; j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='@')
                {
                    good[0].x=i;//起点记为第一个宝物
                    good[0].y=j;
                }
            }
        }
        cin>>cc;
        for(i=1; i<=cc; i++)
            cin>>good[i].x>>good[i].y;
        int flag=0;
        for(i=0; i<=cc; i++)
            for(j=i+1; j<=cc; j++)
            {
                dis[i][j]=dis[j][i]=bfs(good[i],good[j]);//用最短路建图
                if(dis[i][j]==-1)
                {
                    flag=1;
                    break;
                }
            }
        if(flag)
            cout<<-1<<endl;
        else
        {
            Min=inf;
            memset(vis,0,sizeof(vis));
            vis[0]=1;
            dfs(0,0,0);
            cout<<Min<<endl;
        }
    }
}


下载前必看:https://pan.quark.cn/s/a4b39357ea24 在本资料中,将阐述如何运用JavaScript达成单击下拉列表框选定选项后即时转向对应页面的功能。 此种技术适用于网页布局中用户需迅速选取并转向不同页面的情形,诸如网站导航栏或内容目录等场景。 达成此功能,能够显著改善用户交互体验,精简用户的操作流程。 我们须熟悉HTML里的`<select>`组件,该组件用于构建一个选择列表。 用户可从中选定一项,并可引发一个事件来响应用户的这一选择动作。 在本次实例中,我们借助`onchange`事件监听器来实现当用户在下拉列表框中选定某个选项时,页面能自动转向该选项关联的链接地址。 JavaScript里的`window.location`属性旨在获取或设定浏览器当前载入页面的网址,通过变更该属性的值,能够实现页面的转向。 在本次实例的实现方案里,运用了`eval()`函数来动态执行字符串表达式,这在现代的JavaScript开发实践中通常不被推荐使用,因为它可能诱发安全问题及难以排错的错误。 然而,为了本例的简化展示,我们暂时搁置这一问题,因为在更复杂的实际应用中,可选用其他方法,例如ES6中的模板字符串或其他函数来安全地构建和执行字符串。 具体到本例的代码实现,`MM_jumpMenu`函数负责处理转向逻辑。 它接收三个参数:`targ`、`selObj`和`restore`。 其中`targ`代表要转向的页面,`selObj`是触发事件的下拉列表框对象,`restore`是标志位,用以指示是否需在转向后将下拉列表框的选项恢复至默认的提示项。 函数的实现通过获取`selObj`中当前选定的`selectedIndex`对应的`value`属性值,并将其赋予`...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值