POJ 3026 Borg Maze【BFS+最小生成树MST】

Borg Maze

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 12014

 

Accepted: 3925

Description

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


题目大意:一个图,里边有A有S。在图里边连接所有的S和A需要走的步数。这里口述这个问题可能是有点过于浅略,接下来分析一个样例就能明白了:


分析样例:比如第二个样例连接完上边三个A需要两步,连接完下边三个A也需要两步,S连接上边三个A中间的A和下边三个A中间的A都需要两步,然后最后连接S和右边的A需要3步一共是:2+2+2+2+3=11、


思路:BFS+Kruskal、用BFS的方法把所有A/S都遍历一遍,建边,然后对这些边排序,进行最小生成树算法即可。


坑点:输入完n和m之后呢,后台测试数据里边有一堆空格,所以导致很多人没有办法1A。。。。。。。


因为我的代码比较挫,所以我们分步来解这个问题:

1、主函数内容

        memset(g,0,sizeof(g));
        scanf("%d%d",&m,&n);
        gets(fuck);
        for(int i=0;i<n;i++)
        {
            gets(a[i]);
        }
        cont=0;
        u=1;
        for(int i=0;i<n;i++)//给A编号,给节点编号
        {
            for(int j=0;j<m;j++)
            {
                if(a[i][j]=='#')g[i][j]=-1;
                if(a[i][j]==' ')g[i][j]=0;
                if(a[i][j]=='S'||a[i][j]=='A')
                {
                    g[i][j]=u;
                    u++;
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(g[i][j]>0)
                bfs(i,j,g[i][j]);//BFS建边
            }
        }
        MST();//最后进行最小生成树算法
2、BFS:
void bfs(int x,int y,int from)
{
    memset(vis,0,sizeof(vis));
    queue<zuobiao >s;
    now.x=x;
    now.y=y;
    now.output=0;
    vis[x][y]=1;
    s.push(now);
    while(!s.empty())
    {
        now=s.front();
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            nex.output=now.output+1;
            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&&g[nex.x][nex.y]!=-1)//如果能走
            {
                vis[nex.x][nex.y]=1;
                s.push(nex);
                if(g[nex.x][nex.y]>0)//如果走到的点也是个节点,建边
                {
                    e[cont].x=from;
                    e[cont].y=g[nex.x][nex.y];
                    e[cont++].w=nex.output;
                }
            }
        }
    }
}
3、最小生成树+并查集部分:
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int cmp(path a,path b)
{
    return a.w<b.w;
}
void MST()
{
    int output=0;
    sort(e,e+cont,cmp);
    for(int i=1;i<=u;i++)
    {
        f[i]=i;
    }
    for(int i=0;i<cont;i++)
    {
        if(find(e[i].x)!=find(e[i].y))
        {
            merge(e[i].x,e[i].y);
            output+=e[i].w;
        }
    }
    printf("%d\n",output);
}

完整AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct path
{
    int x,y,w;
}e[500*500];
struct zuobiao
{
    int x,y,output;
}now,nex;
int n,m;
int cont,u;
int f[500*500];
char a[60][60];
int g[60][60];
int vis[60][60];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
void bfs(int x,int y,int from)
{
    memset(vis,0,sizeof(vis));
    queue<zuobiao >s;
    now.x=x;
    now.y=y;
    now.output=0;
    vis[x][y]=1;
    s.push(now);
    while(!s.empty())
    {
        now=s.front();
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            nex.output=now.output+1;
            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&&g[nex.x][nex.y]!=-1)
            {
                vis[nex.x][nex.y]=1;
                s.push(nex);
                if(g[nex.x][nex.y]>0)
                {
                    e[cont].x=from;
                    e[cont].y=g[nex.x][nex.y];
                    e[cont++].w=nex.output;
                }
            }
        }
    }
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int cmp(path a,path b)
{
    return a.w<b.w;
}
void MST()
{
    int output=0;
    sort(e,e+cont,cmp);
    for(int i=1;i<=u;i++)
    {
        f[i]=i;
    }
    for(int i=0;i<cont;i++)
    {
        if(find(e[i].x)!=find(e[i].y))
        {
            merge(e[i].x,e[i].y);
            output+=e[i].w;
        }
    }
    printf("%d\n",output);
}
char fuck[1000];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(g,0,sizeof(g));
        scanf("%d%d",&m,&n);
        gets(fuck);
        for(int i=0;i<n;i++)
        {
            gets(a[i]);
        }
        cont=0;
        u=1;
        for(int i=0;i<n;i++)//给A编号
        {
            for(int j=0;j<m;j++)
            {
                if(a[i][j]=='#')g[i][j]=-1;
                if(a[i][j]==' ')g[i][j]=0;
                if(a[i][j]=='S'||a[i][j]=='A')
                {
                    g[i][j]=u;
                    u++;
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(g[i][j]>0)
                bfs(i,j,g[i][j]);
            }
        }
        MST();
    }
}







你可能感兴趣的:(poj,pku,3026,3026)