POJ 3026 Borg Maze

P - Borg Maze

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

Appoint description:



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
//我就不想说我这题错了几次了、其实这题也不难
BFS求出任意2点的最短距离 建立图 然后 最小生成树
//问题是我自己没考虑在墙外有'A'的情况 还有就是 50*50我等于 250了、真心二百五了、、、
//还有就是这题数据坑、还好discuss里面有好心人说了数字后面可能有好多空格、、
#include <iostream>

#include <stdio.h>

#include <queue>

#include <stack>

#include <set>

#include <vector>

#include <math.h>

#include <string.h>

#include <algorithm>

using namespace std;

#define N 150

int map[N][N];

bool b[N];

int ds[N];

int n,s;

int id[5000];//这里开始小了、、、

int Prim()

{

    int i,k;

    memset(b,0,sizeof(b));

    b[s]=1;

    int Min,sum=0;

    int t=n;

    while(--t)

    {

        Min=10000;

        for(i=1;i<=n;i++)

         if(!b[i]&&Min>ds[i])

         {

             Min=ds[i];

             k=i;

         }

         if(Min==10000) break;

        b[k]=1;

        sum+=Min;

        for(i=1;i<=n;i++)

        if(!b[i]&&map[k][i]<ds[i])

              ds[i]=map[k][i];

    }

    return sum;

}

int h,w;

char ch[55][55];

bool visit[55][55];

structnode

{

    int x,y;

    int step;

};

int dir[4][2]={-1,0,1,0,0,-1,0,1};

bool OK(node&b)

{

    if(b.x<0||b.y<0||b.x>=h||b.y>=w||ch[b.x][b.y]=='#'||visit[b.x][b.y])

       return false ;

    return true;

}

void bfs(int x,int y)

{

    memset(visit,0,sizeof(visit));

    visit[x][y]=1;

    nodea,b;

    a.x=x; a.y=y;

    a.step=0;

    queue<node>Q;

    int i,k1=x*w+y,k2;

    Q.push(a);

    while(!Q.empty())

    {

        a=Q.front();Q.pop();

        for(i=0;i<4;i++)

         {

             b.x=a.x+dir[i][0];

             b.y=a.y+dir[i][1];

             if(OK(b))

             {

                 visit[b.x][b.y]=1;

                 b.step=a.step+1;

                 if(ch[b.x][b.y]=='A'||ch[b.x][b.y]=='S')

                 {

                     k2=b.x*w+b.y;

                     map[id[k1]][id[k2]]=b.step;

                     map[id[k2]][id[k1]]=b.step;

                 }

                Q.push(b);

             }

         }

    }

}

int main()

{

    int T;

    char tem[55];

    int i,j,k;

    scanf("%d",&T);

    while(T--)

    {   n=0;

        scanf("%d%d",&w,&h);

        gets(tem);

        for(i=0;i<h;i++)

         {

             gets(ch[i]);

            for(j=0;j<w;j++)

             if(ch[i][j]=='A'||ch[i][j]=='S')

                 {

                     k=i*w+j;

                     id[k]=++n;

                     if(ch[i][j]=='S') s=n;

                 }

         }

        for(i=1;i<=n;i++)

         for(j=1;j<=n;j++)

           map[i][j]=1000000;

       for(i=0;i<h;i++)

        for(j=0;j<w;j++)

         {

             if(ch[i][j]=='A'||ch[i][j]=='S')

                   bfs(i,j);

         }

     for(i=1;i<=n;i++)

      ds[i]=map[s][i];

     printf("%d\n",Prim());

    }

    return 0;

}
 

你可能感兴趣的:(poj)