POJ 3026 Borg Maze(BFS+Prim)

来源:互联网 发布:郑州中学编程培训班 编辑:程序博客网 时间:2024/05/16 01:55
Borg Maze
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8347 Accepted: 2798

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
题意为找到各个点之间的距离,计算连接各个点需要的最短距离。。BFS+Prim就可解决,题意一开始理解偏了,各个点之间的距离保存错了,调了整整一个下午才发现,,哎、、、、、、啥都不说了,上代码
<pre name="code" class="html">#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#define MAX 999999999using namespace std;struct node{    int c1,c2;    int sum;} q[10001];int w=0;int n,m,k;int c[101],d[101];char map[101][101];int b[10001];int v[10001];int a[101][101];int xx[4]= {0,0,-1,1};int yy[4]= {1,-1,0,0};int b1[101][101];void Prim(){    int i,j,e=0;    memset(b,0,sizeof(b));    for(i=0; i<k; i++)        v[i]=MAX;    for(i=0; i<k; i++)      v[i]=a[0][i];    v[0]=0;    b[0]=1;    int min=MAX;    int sum=0;    for(i=0; i<k; i++)    {        min=MAX;        for(j=0; j<k; j++)        {            if(!b[j]&&v[j]<min)            {                min=v[j];                e=j;            }        }        if(min!=MAX)            sum+=min;        b[e]=1;        for(j=0; j<k; j++)        {            if(!b[j]&&v[j]>a[e][j])                v[j]=a[e][j];        }    }    printf("%d\n",sum);}int bfs(int s,int e,int x,int y){    int k=0,l=0;    memset(b1,0,sizeof(b1));    struct node t,r;    t.c1=s;    t.c2=e;    t.sum=0;    b1[s][e]=1;    q[l++]=t;    int i;    while(k<l)    {        t=q[k++];        if(t.c1==x && t.c2==y)            return t.sum;        for(i=0; i<4; i++)        {            r.c1=t.c1+xx[i];            r.c2=t.c2+yy[i];            if(r.c1>=0 && r.c1<n&& r.c2>=0 && r.c2<m&&!b1[r.c1][r.c2] && map[r.c1][r.c2]!='#')            {                r.sum=t.sum+1;                q[l++]=r;                b1[r.c1][r.c2]=1;            }        }    }    return 0;}int main(){    int s,i,j;    scanf("%d",&s);    char b[300];    while(s--)    {        k=0;        memset(a,0,sizeof(a));        scanf("%d%d",&n,&m);        gets(b);        for(i=0; i<m; i++)            gets(map[i]);        for(i=0; i<m; i++)            for(j=0; j<n; j++)            {                if(map[i][j]=='S'||map[i][j]=='A')                {                    c[k]=i;                    d[k++]=j;                }            }        for(i=0; i<k-1; i++)            for(j=i+1; j<k; j++)            {                if(i!=j)                    a[i][j]=a[j][i]=bfs(c[i],d[i],c[j],d[j]);            }        Prim();    }    return 0;



                                             
0 0