Borg Maze【POJ--3026】【并查集】

来源:互联网 发布:优化发现环境调研报告 编辑:程序博客网 时间:2024/05/18 06:05

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.
题意:有T组数据,每组数据输入m和n分别代表迷宫的宽和长,迷宫中的#代表墙,“A”代表外星人所在的位置,“S”代表搜索人员的入口,在搜索的过程中,搜索小队可以分身,(对于样例1来说,1小分队从入口向上走两步找到第一个外星人,2小分队从入口向右走在向上走找到第二个外星人再向下走向右走找到第三个外星人,即总共走了8步)求找到所有外星人所走的最少步数。
思路:先求出来任意两点间的步数,然后构建步数最少且能连通所有外星人位置及入口的通路。

Sample Input

26 5##### #A#A### # A##S  ####### 7 7#####  #AAA####    A## S ####     ##AAA########  

Sample Output

811

#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <map>#define INF 0x3f3f3f3fusing namespace std;typedef struct point{    int x,y;} point;typedef struct Edge{    int u,v,w;    bool operator < (const Edge aa)const    {        return w<aa.w;    }} Edge;Edge eg[10100];                     //边集合即记录任意两点间的通路point p[3000];char mmap[60][60];             //记录迷宫int id[60][60];                        //记录点的编号int dx[]= {-1,0,1,0};int dy[]= {0,1,0,-1};int tp,top,n,m;int dis[102][102];                //任意两点间的步数bool vs[102][102];               //标记该两点间是否已建成通路bool vis[102][102];           int pr[110];void BFS(int x,int y)              //计算任意两点间的步数{    int aa,a,bb,b;    memset(vis,0,sizeof(vis));    memset(dis,0,sizeof(dis));    queue<pair<int,int> >q;    q.push(pair<int,int>(x,y));    vis[x][y]=true;    dis[x][y]=0;    while(!q.empty())    {        pair<int,int> pt=q.front();        q.pop();        a=pt.first;        b=pt.second;        for(int i=0; i<4; i++)        {            aa=a+dx[i];            bb=b+dy[i];            if(aa>n||aa<1||bb>m||bb<1||vis[aa][bb]||mmap[aa][bb]=='#')continue;            dis[aa][bb]=dis[a][b]+1;            vis[aa][bb]=true;            q.push(pair<int,int> (aa,bb));            if(mmap[aa][bb]!=' '&&!vs[id[x][y]][id[aa][bb]])    //建边            {                vs[id[x][y]][id[aa][bb]]=vs[id[aa][bb]][id[x][y]]=true;                eg[top].u=id[x][y];                eg[top].v=id[aa][bb];                eg[top++].w=dis[aa][bb];            }        }    }}int Find(int x){   if(x!=pr[x])   pr[x]=Find(pr[x]);   return pr[x];}int main(){  //freopen("lalala.text","r",stdin);    int T,cnt,sum;    scanf("%d",&T);    while(T--)    {        memset(mmap,0,sizeof(mmap));           memset(vs,0,sizeof(vs));        memset(id,0,sizeof(id));        scanf("%d %d",&m,&n);        gets(mmap[0]);      //消除换行符        tp=top=0;        for(int i=1; i<=n; i++)        {            gets(mmap[i]+1);            for(int j=1; mmap[i][j]; j++)            {                if(mmap[i][j]=='A'||mmap[i][j]=='S')                {                    id[i][j]=tp;                    p[tp].x=i;                    p[tp++].y=j;                }            }        }       /*   for(int i=0;i<tp;i++)        {          printf("*****%d %d %d*****\n",i,p[i].x,p[i].y);        }*/        for(int i=0; i<tp; i++)            BFS(p[i].x,p[i].y);        for(int i=0;i<tp;i++)        pr[i]=i;        int fx,fy;        cnt=sum=0;        sort(eg,eg+top);                //记得排序        for(int i=0;i<top;i++)        {            fx=Find(eg[i].u);            fy=Find(eg[i].v);            if(fx!=fy)            {               pr[fx]=fy;               cnt++;               sum+=eg[i].w;            }            if(cnt==tp-1)            break;        }        printf("%d\n",sum);    }    return 0;}<strong></strong>


0 0
原创粉丝点击