poj 3026 Borg Maze

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11549   Accepted: 3797

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<cstdlib>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<queue>
#include<iomanip>
#include<map>
#include<set>
#define pi 3.14159265358979323846
using namespace std;
const int INF=0x3f3f3f3f;
struct Pos
{
    int x;
    int y;
    int len;
};
int x,y;
char maze[51][51];
int id[51][51];//储存结点的编号
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool vis[51][51];//广搜的标记数组
int dis[105][105];//字母之中的最短距离
void bfs(int a,int b)
{
    queue<Pos> q;
    memset(vis,0,sizeof(vis));
    Pos pos;
    pos.x=a;
    pos.y=b;
    pos.len=0;
    q.push(pos);
    vis[a][b]=1;
    while(!q.empty())
    {
        Pos cur=q.front();
        //cout<<cur.len<<endl;
        q.pop();
        if(id[cur.x][cur.y]!=-1)
            dis[id[pos.x][pos.y]][id[cur.x][cur.y]]=cur.len;
        for(int i=0;i<4;++i)
        {
            Pos temp;
            temp.x=cur.x+dx[i];
            temp.y=cur.y+dy[i];
            if(temp.x<0||temp.x>=y||temp.y<0||temp.y>=x)
                continue;
            if(vis[temp.x][temp.y]||maze[temp.x][temp.y]=='#')
                continue;
            vis[temp.x][temp.y]=1;
            temp.len=cur.len+1;
            //cout<<temp.len<<endl;
            q.push(temp);
        }
    }
}
int prim(int cnt)
{
    bool v[105];
    int d[105];
    int ans=0;
    memset(v,0,sizeof(v));
    for(int i=0;i<cnt;++i)
    {
        d[i]=dis[0][i];
        //cout<<d[i]<<endl;
    }
    v[0]=1;
    d[0]=0;
    for(int i=0;i<cnt-1;++i)
    {
        int flag,zmin=INF;
        for(int j=0;j<cnt;++j)
        {
            if(!v[j]&&d[j]<zmin)
            {
                flag=j;
                zmin=d[j];
                //cout<<zmin<<endl;
            }
        }
        v[flag]=1;
        ans+=zmin;
        //cout<<ans<<endl;
        for(int j=0;j<cnt;++j)
        {
            if(!v[j]&&d[j]>dis[flag][j]) d[j]=dis[flag][j];
        }
    }
    return ans;
}
int main()
{
    int N;
    char tmp[200];
    scanf("%d",&N);
    while(N--)
    {
        scanf("%d %d",&x,&y);
        //这里为了去空格,防坑
        gets(tmp);
        memset(id,-1,sizeof(id));
        int cnt=0;
        for(int i=0;i<y;++i)
        {
            gets(maze[i]);
            //编号
            for(int j=0;j<x;++j)
            {
                if(maze[i][j]=='A'||maze[i][j]=='S')
                id[i][j]=cnt++;
            }
        }
        for(int i=0;i<y;++i)
            for(int j=0;j<x;++j)
        {
            if(maze[i][j]=='A'||maze[i][j]=='S')
                bfs(i,j);
        }
        int ans=prim(cnt);
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(poj)