poj 3026 Borg Maze (bfs+最小生成树)

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6885   Accepted: 2306

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

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001


思路:bfs+克鲁斯卡尔算法 

先对每个点(A、S)进行bfs建边,边全部建好之后就是克鲁斯卡尔算法 了。

ps:这题超坑,出题人的素质问题,估计不看discuss一般人过不了的。因为数据上面x、y后面还可以有n个空格。

如果你只用一个getchar()的话就会WA了。


代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 105
#define maxx 55
using namespace std;

int n,m,num,cnt,edge,ans;
int set[maxn],rank[maxn];
int xx[maxn],yy[maxn];
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
int map[maxn*maxn];
char mp[maxx][maxx];
bool vis[maxx][maxx];
char s[maxx];
struct Tnode
{
    int v1,v2;
    int cost;
} node[maxn*maxn];
struct Node
{
    int x,y;
    int step;
} cur,now,q[maxx*maxx];

void init(int nn)
{
    int i;
    for(i=1; i<=nn; i++)
    {
        set[i]=i;
        rank[i]=1;
    }
    num=nn;
}
int find(int x)        // 查找时优化了路径
{
    int r=x,nn;
    while(set[r]!=r) r=set[r];
    while(x!=r)
    {
        nn=set[x];
        set[x]=r;
        x=nn;
    }
    return r;
}
void merge(int a,int b)    // 合并时将深度小的集合合并到大的里面
{
    int x,y;
    x=find(a);
    y=find(b);
    if(x!=y)
    {
        if(rank[x]<rank[y])  set[x]=y;
        else
        {
            set[y]=x;
            if(rank[x]==rank[y])  rank[x]++;
        }
        num--;
    }
}
bool cmp(const Tnode &xx1, const Tnode &xx2)
{
    return  xx1.cost<xx2.cost;
}
void bfs(int sx,int sy)
{
    int i,j,nx,ny,tx,ty,nstep,u,v,tmp;
    int head=0,tail=-1;
    tmp=(sx-1)*m+sy;
    u=map[tmp];
    memset(vis,0,sizeof(vis));
    cur.x=sx;
    cur.y=sy;
    cur.step=0;
    vis[sx][sy]=1;
    q[++tail]=cur;
    while(head<=tail)
    {
        now=q[head];
        head++;
        nx=now.x;
        ny=now.y;
        nstep=now.step;
        if(nstep&&(mp[nx][ny]=='A'||mp[nx][ny]=='S'))
        {
            edge++;
            tmp=(nx-1)*m+ny;
            v=map[tmp];
            node[edge].v1=u;
            node[edge].v2=v;
            node[edge].cost=nstep;
        }
        for(i=0; i<4; i++)
        {
            tx=nx+dx[i];
            ty=ny+dy[i];
            if(mp[tx][ty]!='#'&&!vis[tx][ty])
            {
                cur.x=tx;
                cur.y=ty;
                cur.step=nstep+1;
                vis[tx][ty]=1;
                q[++tail]=cur;
            }
        }
    }
}
int main()
{
    int i,j,x1,x2,t,tmp,len;
    char c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        gets(s);
        cnt=0;
        memset(mp,'#',sizeof(mp));
        for(i=1; i<=n; i++)
        {
            gets(s);
            len=strlen(s);
            for(j=1; j<=len; j++)
            {
                mp[i][j]=s[j-1];
                if(mp[i][j]=='A'||mp[i][j]=='S')
                {
                    cnt++;
                    xx[cnt]=i;
                    yy[cnt]=j;
                    tmp=(i-1)*m+j;
                    map[tmp]=cnt;   // 建立映射
                }
            }
        }
        edge=0;
        for(i=1; i<=cnt; i++)
        {
            bfs(xx[i],yy[i]);
        }
        sort(node+1,node+edge+1,cmp);
        init(cnt);
        ans=0;
        for(i=1; num>1&&i<=edge; i++)
        {
            x1=find(node[i].v1);
            x2=find(node[i].v2);
            if(x1!=x2)
            {
                merge(node[i].v1,node[i].v2);
                ans+=node[i].cost;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
2
7 7           
#####
#AAA###
#    A#
# S ###
#    A#
#AAA###
#####
ans:14
*/


你可能感兴趣的:(poj 3026 Borg Maze (bfs+最小生成树))