poj 3026 Borg Maze

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6176   Accepted: 2087

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求出每个点之间的距离,然后求最小生成树。

#include <iostream>
#include <string>
#include <cstring>
#include <queue>
#define OO 9999999

using namespace std;

typedef struct{
    int x;
    int y;
    int step;
}POINT;

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

POINT pot[500];//储存S与A
char chr[500][500];//储存asc码地图
int map[500][500];//储存数字地图,0代表通路,OO代表墙,1~100代表S与A
bool bm[500][500];
int a[500][500];//邻接矩阵存图
int d[500];//最小生成树
bool v[500];//集合标记

queue <POINT> que;//bfs用队列
POINT tmp,p;//bfs用变量

int main()
{
    int T;//样例数
    int A,B;//储存坐标边界
    int n;//表示A点个数
    int find;//表示bfs时找到几个点
    int t,mind;//prim用变量
    int ans;
    cin>>T;
    while (T--)
    {
        n=1;
        cin>>B>>A;
        for (int i=0;i<=A;i++)
        {
            cin.getline(chr[i],100,'\n');
        }
        for (int i=1;i<=A;i++)
        {
            for (int j=1;j<=B;j++)
            {
                if (chr[i][j-1]==' ')
                {
                    map[i][j]=0;
                }
                else if (chr[i][j-1]=='#')
                {
                    map[i][j]=OO;
                }
                else if (chr[i][j-1]=='S')
                {
                    map[i][j]=1;
                    pot[1].x=i;
                    pot[1].y=j;
                }
                else if (chr[i][j-1]=='A')
                {
                    n++;
                    map[i][j]=n;
                    pot[n].x=i;
                    pot[n].y=j;
                }
            }
        }
        //读入地图
        /*
        for (int i=1;i<=A;i++)
        {
            for (int j=1;j<=B;j++)
            {
                cout<<map[i][j]<<" ";
            }
            cout<<endl;
        }
        */
        //验证完毕
        memset(a,0,sizeof(a));
        for (int pl=1;pl<=n;pl++)
        {
            memset(bm,0,sizeof(bm));
            while (!que.empty()) que.pop();
            p=pot[pl];
            p.step=0;
            que.push(p);
            bm[p.x][p.y]=true;
            find=n;
            while (!que.empty()&&find>0)
            {
                tmp=que.front();
                que.pop();
                if ( map[tmp.x][tmp.y]!=0 )
                {
                    a[ pl ][ map[tmp.x][tmp.y] ]=tmp.step;
                    a[ map[tmp.x][tmp.y] ][ pl ]=tmp.step;
                    find--;
                }
                for (int i=0;i<4;i++)
                {
                    p=tmp;
                    p.x=p.x+direct[i][0];
                    p.y=p.y+direct[i][1];
                    p.step=p.step+1;
                    if ( p.x>=1&&p.x<=A&&p.y>=1&&p.y<=B&&map[p.x][p.y]!=OO&&bm[p.x][p.y]!=true )
                    {
                        bm[p.x][p.y]=true;
                        que.push(p);
                    }
                }
            }
        }
        //bfs求矩阵
        /*
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=n;j++)
            {
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
        */
        //检验完毕
        ans=0;
        memset(v,0,sizeof(v));
        for (int i=0;i<=n;i++) d[i]=OO;
        d[1]=0;
        for (int loop=1;loop<=n;loop++)
        {
            mind=OO;
            for (int i=1;i<=n;i++)
            {
                if (!v[i]&&d[i]<mind)
                {
                    mind=d[i];
                    t=i;
                }
            }
            v[t]=true;
            ans+=mind;
            for (int i=1;i<=n;i++)
            {
                if (a[t][i]<d[i])
                {
                    d[i]=a[t][i];
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}



posted on 2013-02-02 22:57  电子幼体  阅读(119)  评论(0编辑  收藏  举报

导航