POJ 3026-Borg Maze【最小生成树+bfs】

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

写在前面

本来觉得这道题是一道挺好的最小生成树的题,但是当我提交后,一直WA,我开始怀疑自己,知道原因后我自闭了,最后原因竟是!!    (ノ=Д=)ノ┻━┻

题目描述

The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated subspace network that insures each member is given constant supervision and guidance. 

Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over 100 individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11=5+3+3.

Input

On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that 1 <= x,y <= 50. After this, y lines follow, each which x characters. For each character, a space `` '' stands for an open space, a hash mark ``#'' stands for an obstructing wall, the capital letter ``A'' stand for an alien, and the capital letter ``S'' stands for the start of the search. The perimeter of the maze is always closed, i.e., there is no way to get out from the coordinate of the ``S''. At most 100 aliens are present in the maze, and everyone is reachable.

Output

For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.

Sample Input

2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  

Sample Output

8
11

题意

题意还挺难理解的,我读了好几遍才看懂 。

题意是给你一个y*x的迷宫,‘#’代表墙壁,‘A’代表外星人,‘S’是出发点,‘ ’(空格)是开放区域,这个人可以在‘S’‘A’处分身成多个人,问你找到所有的外星人,最少要走几步。

例如第一个样例,这个人在‘S’处分身成两个,一个到达(2,2)这个‘A’点要走2步,一个到达(3,5)这个‘A’点要走4步,再从(3,5)这个‘A’点到达(2,4)这个‘A’点需要2步,一共8 = 2+4+2 步。

其实题意就是把‘S’‘A’点看成一样的点,计算每两个点之间要走的最短步数,便形成了一个图,然后在图中找最小生成树就可以了。

思路

一般迷宫的题,都会想到深搜(dfs)和宽搜(bfs),在这我们用bfs,对每一个‘A’‘S’点跑一遍bfs,这样就可以得到每两个点之间的最短距离,然后再跑一遍Kruskal就可以了。

最后别以为这样写完真的可以了!!这道题的数据在输入完xy后,有莫名其妙的空格,所以,在输入完xy后,需要用gets()把多余的空格去掉后,再输入迷宫,不然会一直WA!!!!       (ノ=Д=)ノ┻━┻

代码

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

const int Max = 55;
char maze[Max][Max];
bool flag[Max][Max];
int po[Max][Max];


const int Maxe = 100005;
const int Maxn = 105;
struct edge{
    int from,to,val;
};
edge e[Maxe];
int par[Maxn];
int ct;

bool cmp(edge e1, edge e2)
{
    return e1.val<e2.val;
}
void Init()
{
    for(int i = 0; i < Maxn; i++)
        par[i] = i;
}
int Find(int x)
{
    if(x==par[x])
        return par[x];
    else
        return par[x] = Find(par[x]);
}
bool Union(int x, int y)
{
    x = Find(x);
    y = Find(y);
    if(x!=y)
    {
        par[y] = x;
        return true;
    }
    else
        return false;
}
int Kruskal()
{
    int res = 0;
    sort(e,e+ct,cmp);
    Init();
    for(int i = 0; i < ct; i++)
    {
        if(Union(e[i].from, e[i].to))
            res+=e[i].val;
    }
    return res;
}

struct node{
    int x,y,step;
    friend bool operator < (node n1, node n2)
    {
        return n1.step>n2.step;
    }
};

int n,m;
int cnt;
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};
priority_queue<node> q;

bool inmaze(int nx, int ny)
{
    return nx>=0&&nx<n&&ny>=0&&ny<m;
}
void bfs(int sx, int sy)
{
    node n1;
    n1.x = sx;
    n1.y = sy;
    n1.step = 0;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
            flag[i][j] = false;
    }
    flag[sx][sy] = true;
    q.push(n1);
    while(!q.empty())
    {
        n1 = q.top();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            int nx = n1.x+dx[i];
            int ny = n1.y+dy[i];
            if(inmaze(nx,ny)&&!flag[nx][ny])
            {
                node n2;
                n2.x = nx;
                n2.y = ny;
                n2.step = n1.step+1;
                if(maze[nx][ny]==' ')
                {
                    q.push(n2);
                    flag[nx][ny] = true;
                }
                else if(maze[nx][ny]=='A'||maze[nx][ny]=='S')
                {
                    q.push(n2);
                    flag[nx][ny] = true;
                    if(po[sx][sy]<po[nx][ny])
                    {
                        e[ct].from = po[sx][sy];
                        e[ct].to = po[nx][ny];
                        e[ct++].val = n2.step;
                    }
                }
            }
        }
    }
}
int main()
{
    int t;
    char s[100];
    scanf("%d", &t);
    while(t--)
    {
        cnt = 0;
        ct = 0;
        scanf("%d %d", &m, &n);
        gets(s);//吸收莫名其妙的空格
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                scanf("%c", &maze[i][j]);
                if(maze[i][j]=='S'||maze[i][j]=='A')
                {
                    po[i][j] = cnt++;
                }
            }
            getchar();
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(maze[i][j]=='S'||maze[i][j]=='A')
                {
                    bfs(i,j);
                }
            }
        }
        printf("%d\n", Kruskal());
    }
    return 0;
}

如有错误请指明~   ฅ●ω●ฅ

猜你喜欢

转载自blog.csdn.net/include_peng/article/details/86656957