HDU - 4771 - Stealing Harry Potter's Precious(BFS + DFS)

本文详细解析了ACM竞赛中HDU 4771题目,该题要求在一个n*m的矩阵中,计算小偷从起点出发,经过所有珠宝点的最短路径总步数。文章提供了两种解题思路,一是使用bfs+dfs算法,二是使用bfs结合next_permutation函数,通过全排列找到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771

题意:给一个n*m的矩阵,小偷从@出发可以走“.”,墙是“#”不可走,之后又K个珠宝,并且给你这K个珠宝的坐标,问小偷最少要走多少步可以吧珠宝偷完,如果不能偷完输出“-1”。

思路:算上起点"@"一共k+1个点,最终步数的多少一定跟取宝物的顺序有关,我们可以求出k+1个点任意两点的距离,然后dfs搜索一遍即可,也可以用next_permutation全排列函数,其实都一样,下面给出两个版本的代码。

bfs + dfs版本:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS cin.tie(0); cout.tie(0); ios::sync_with_stdio(0);
const int inf = 0x3f3f3f3f;
const int N = 3e5+7;
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
struct node
{
    int x, y, step;
}Now, Next, p;
int n, m, k, din[5][505], vis[505][505];
char mp[505][505];
int bfs(int s, int t)
{
    memset(vis, 0, sizeof(vis));
    p.x = din[s][0]; p.y = din[s][1];
    p.step = 0; vis[p.x][p.y] = 1;

    queue <node> Q;
    Q.push(p);
    while(!Q.empty())
    {
        Now = Q.front(); Q.pop();
        if(Now.x == din[t][0] && Now.y == din[t][1]) return Now.step;
        for(int i = 0; i < 4; i++)
        {
            int tx = Now.x + dir[i][0];
            int ty = Now.y + dir[i][1];
            if(!vis[tx][ty] && tx >= 0 && tx < n && ty >= 0 && ty < m && mp[tx][ty] != '#')
            {
                Next.x = tx; Next.y = ty;
                Next.step = Now.step + 1;
                vis[tx][ty] = 1;
                Q.push(Next);
            }
        }
    }
    return inf;

}
int ans, book[10], dis[10][10];
void dfs(int u, int lev, int sum)
{
    if(lev == k)
    {
        ans = min(ans, sum);
        return ;
    }
    for(int i = 1; i <= k; i++)
    {
        if(!book[i])
        {
            book[i] = 1;
            dfs(i, lev + 1, sum + dis[u][i]);
            book[i] = 0;
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m) && n+m)
    {
        ans = inf;
        memset(din, 0, sizeof(din));
        memset(dis, 0x3f, sizeof(dis));
        for(int i = 0; i < n; i++)
            scanf("%s",mp[i]);

        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(mp[i][j] == '@')
                {
                    din[0][0] = i, din[0][1] = j;
                    break;
                }
        scanf("%d",&k);
        int a[5] = {0}; int x, y;
        for(int i = 1; i <= k; i++)
        {
            scanf("%d%d",&x, &y);
            din[i][0] = --x, din[i][1] = --y;
            a[i] = i;
        }
        for(int i = 0; i < k; i++)
            for(int j = i + 1; j <= k; j++)
            dis[i][j] = dis[j][i] = bfs(i, j);
        memset(book, 0, sizeof(book));
        dfs(0, 0, 0);
        if(ans == inf) puts("-1");
        else printf("%d\n",ans);
    }
}
/*
2 3
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
*/

bfs + next_permutation版本:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS cin.tie(0); cout.tie(0); ios::sync_with_stdio(0);
const int inf = 0x3f3f3f3f;
const int N = 3e5+7;
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
struct node
{
    int x, y, step;
}Now, Next, p;
int n, m, k, din[5][505], vis[505][505];
char mp[505][505];
int bfs(int s, int t)
{
    memset(vis, 0, sizeof(vis));
    p.x = din[s][0]; p.y = din[s][1];
    p.step = 0; vis[p.x][p.y] = 1;

    queue <node> Q;
    Q.push(p);
    while(!Q.empty())
    {
        Now = Q.front(); Q.pop();
        if(Now.x == din[t][0] && Now.y == din[t][1]) return Now.step;
        for(int i = 0; i < 4; i++)
        {
            int tx = Now.x + dir[i][0];
            int ty = Now.y + dir[i][1];
            if(!vis[tx][ty] && tx >= 0 && tx < n && ty >= 0 && ty < m && mp[tx][ty] != '#')
            {
                Next.x = tx; Next.y = ty;
                Next.step = Now.step + 1;
                vis[tx][ty] = 1;
                Q.push(Next);
            }
        }
    }
    return inf;

}
int main()
{
    while(~scanf("%d%d",&n,&m) && n+m)
    {
        memset(din, 0, sizeof(din));
        for(int i = 0; i < n; i++)
            scanf("%s",mp[i]);

        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(mp[i][j] == '@')
                {
                    din[0][0] = i, din[0][1] = j;
                    break;
                }
        scanf("%d",&k);
        int a[5] = {0}; int x, y;
        for(int i = 1; i <= k; i++)
        {
            scanf("%d%d",&x, &y);
            din[i][0] = --x, din[i][1] = --y;
            a[i] = i;
        }
        int ans = inf, sum;
        do
        {
            sum = 0;
            for(int i = 1; i <= k; i++)
            {
                int t = bfs(a[i-1], a[i]);
                sum += t;
            }
            ans = min(ans, sum);

        }while(next_permutation(a+1, a+1+k));
        if(ans == inf) puts("-1");
        else printf("%d\n",ans);
    }

}
/*
2 3
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值