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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sugarbliss/article/details/89388700

题目链接: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
*/

猜你喜欢

转载自blog.csdn.net/sugarbliss/article/details/89388700