POJ 3026 bfs+最小生成树

来源:互联网 发布:数据运营是干什么的 编辑:程序博客网 时间:2024/06/03 05:31

Borg Maze
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8445 Accepted: 2833

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

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

Sample Output

811

Source




题目就是求一个图中 s,A的 最小生成树,不过有点变态,将出现的s和A按出现的顺序依次递增赋值,用bfs求出这个点和别的S或A的值,然后最小生成树

具体看代码,坑点在代码中解释


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define maxe 100000#define N 1005  //一定要开大,刚开始105一直wa%>_<%int a[N][N];int father[N];struct stud{int a,b;int len;}f[maxe];struct stud1{int x,y;int time;};int step[4][2]={1,0,-1,0,0,1,0,-1};int n,m;int vis[N][N];int k;int judge(int x,int y){    if(x>=0&&x<n&&y>=0&&y<m)        return 1;    return 0;}int cha(int x){    if(x!=father[x])        father[x]=cha(father[x]);    return father[x];}int cmp(stud a,stud b){    return a.len<b.len;}void bfs(int x,int y){    int i,j;    queue<stud1>q;    while(!q.empty())        q.pop();    struct stud1 cur,next;    cur.x=x;    cur.y=y;    cur.time=0;    q.push(cur);    memset(vis,0,sizeof(vis));    vis[x][y]=1;    while(!q.empty())    {        cur=q.front();        q.pop();        for(i=0;i<4;i++)        {            int xx=cur.x+step[i][0];            int yy=cur.y+step[i][1];            if(!judge(xx,yy)||a[xx][yy]<0||vis[xx][yy])                continue;           next.x=xx;           next.y=yy;           next.time=cur.time+1;           vis[xx][yy]=1;                      if(a[xx][yy]>=1)           {               f[k].a=a[x][y];               f[k].b=a[xx][yy];               f[k++].len=next.time;           }           q.push(next);        }    }}int main(){    int t,i,j;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&m,&n);        char s[1000];        gets(s); //小心是一组空格,必须用gets        int num=0;        k=0;        char c;        for(i=0;i<n;i++)        {            for(j=0;j<m;j++)            {                scanf("%c",&c);                                if(c=='#')                    a[i][j]=-1;                else                    if(c==' ')                    a[i][j]=0;                else                    a[i][j]=++num; //出现的S或A依次递增赋值            }            getchar();        }        for(i=0;i<n;i++)            for(j=0;j<m;j++)            if(a[i][j]>0)               bfs(i,j);        for(i=0;i<=num;i++)            father[i]=i;        sort(f,f+k,cmp);        j=1;        int ans=0;        for(i=0;i<k;i++)        {            int aa=cha(f[i].a);            int bb=cha(f[i].b);            if(aa!=bb)            {                j++;                ans+=f[i].len;                father[aa]=bb;                if(j==num)                    break;            }        }        printf("%d\n",ans);    }    return 0;}


代码不怎么好读,sorry,


1 0
原创粉丝点击