poj 3026 Borg Maze bfs+最小生成树

46 篇文章 0 订阅
Language:
Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8829 Accepted: 2954

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

Source

题意:在N*M的迷宫上,从S点出发找一条最短的路径走遍所有的A,且在S和A处可以分成多组同时走,可以分叉,这样就先求出所有点两两之间的距离,找到一棵最小生成树,树上所有边的和就是要求的最小值。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define Maxn 1005
using namespace std;

struct Node
{
    int x,y,step;
    Node(){}
    Node(int a,int b,int c)
    {
        x=a,y=b,step=c;
    }
};

struct Edge
{
    int a,b;
    int len;
};

Edge edge[100000];
Node node[100000];
int cmp[Maxn][Maxn];
int father[Maxn];
int N,M,edgenum;
int visit[Maxn][Maxn];
int dir[4][2]={0,-1,0,1,1,0,-1,0};

int ccmp(Edge x,Edge y)
{
    return x.len<y.len;
}

bool ISok(int x,int y)
{
    if (x>=0&&x<N&&y>=0&&y<M&&!visit[x][y]&&cmp[x][y]!=-1)
        return true;
    return false;
}

int find_father(int x)
{
    if (x!=father[x])
        father[x]=find_father(father[x]);
    return father[x];
}

void bfs(int x,int y)
{
    Node st;
    queue<Node>Q;
    while (!Q.empty())
        Q.pop();
    memset(visit,0,sizeof(visit));
    st.x=x;
    st.y=y;
    st.step=0;
    Q.push(st);
    visit[x][y]=1;
    while (!Q.empty())
    {
        st=Q.front();
        Q.pop();
        for (int i=0;i<4;i++)
        {
            int dx=st.x+dir[i][0];
            int dy=st.y+dir[i][1];
            if (ISok(dx,dy))
            {
                visit[dx][dy]=1;
                Q.push(Node(dx,dy,st.step+1));
                if (cmp[dx][dy]>=1)
                {
                    edge[edgenum].a=cmp[x][y];
                    edge[edgenum].b=cmp[dx][dy];
                    edge[edgenum].len=st.step+1;
                    edgenum++;
                }
            }
        }
    }
}

void Kruskal(int n)
{
    int r=1,ans=0;
    for (int i=0;i<n;i++)
    {
        int aa=find_father(edge[i].a);
        int bb=find_father(edge[i].b);
        if (aa!=bb)
        {
            r++;
            ans+=edge[i].len;
            father[aa]=bb;
            if (r==n)
                break;
        }
    }
    printf("%d\n",ans);
}

int main()
{
    int cas;
    scanf("%d",&cas);
    while (cas--)
    {
        edgenum=0;
        scanf("%d%d",&M,&N);
        int num=1;
        char str[1000];
        gets(str);  //小心是一组空格,必须用gets
        for (int i=0;i<N;i++)
        {
            for (int j=0;j<M;j++)
            {
                char ch;
                scanf("%c",&ch);
                if (ch==' ')
                    cmp[i][j]=0;
                else if(ch=='#')
                    cmp[i][j]=-1;
                else
                {
                    cmp[i][j]=num;
                    num++;
                }
            }
            getchar();
        }
        for (int i=0;i<N;i++)
        {
            for (int j=0;j<M;j++)
            {
                if (cmp[i][j]>0)
                    bfs(i,j);
            }
        }
        sort(edge,edge+edgenum,ccmp);
        for (int i=0;i<num;i++)
            father[i]=i;
        Kruskal(edgenum);
    }
    return 0;
}



  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值