POJ - 3026(BFS+最小生成树.krustal)

题目:

题目链接:

http://poj.org/problem?id=3026

Borg Maze

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17462   Accepted: 5631

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

    1. 2

    2.  

      6 5

    3.  

      #####

    4.  

      #A#A##

    5.  

      # # A#

    6.  

      #S ##

    7.  

      #####

    8.  

      7 7

    9.  

      #####

    10.  

      #AAA###

    11.  

      # A#

    12.  

      # S ###

    13.  

      # #

    14.  

      #AAA###

    15.  

      #####

Sample Output

    1. 8

    2.  

      11

题目思路:

其实s 和 a是一样的 只要求出连接A和S的最小生成树就可以了

主要就是求每个点之间的距离就可以了

具体求法用 BFS遍历每一个点  用邻接表储存  用kruskal算法求最小生成树就可以了

比较坑的就是

一 题目样例里有莫名其妙的一串空格  在输入x y后有一大串空格  要读入并舍弃

二 数组要开大 n<50  我开55 就wa 开205就ac 。。。 迷。。。

AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
char pic[205][205];
int alien[205][205],allroad,vis[205][205],t,n,m,num,father[205],sum;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
struct r
{
    int a,b,dis;
}road[100000];
struct node
{
    int x,y,step;
};
bool cmp(struct r a,struct r b)
{
    return a.dis<=b.dis;
}
void bfs(int bx,int by)
{
    memset(vis,0,sizeof(vis));
    queue <node> q;
    while(!q.empty())q.pop();
    node b,now,next;
    b.x=bx,b.y=by,b.step=0;
    vis[bx][by]=1;
    q.push(b);
    int c=0;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        if(vis[now.x][now.y]==0 && alien[now.x][now.y]>alien[bx][by])
        {
            c++;allroad++;
            road[allroad].a=alien[bx][by];
            road[allroad].b=alien[now.x][now.y];
            road[allroad].dis=now.step;
            if(c==num-1)break;
        }
        for(int d=0;d<4;d++)
        {
            next = now;
            next.x += dx[d];
            next.y += dy[d];
            if(next.x<0 || next.y<0 || next.x>=n || next.y>=m)continue;
            if(vis[next.x][next.y]==1 || pic[next.x][next.y]==‘#‘)continue;
            next.step++;
            vis[next.x][next.y]=1;
            if(alien[next.x][next.y]>alien[bx][by])
            {
                c++;allroad++;
                road[allroad].a=alien[bx][by];
                road[allroad].b=alien[next.x][next.y];
                road[allroad].dis=next.step;
                if(c==num-1)break;
            }
            q.push(next);
        }
        if(c==num-1)break;
    }
}
int findd(int x);
int main()
{
    char temp[110];
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        scanf("%d %d",&m,&n);
        gets(temp);
        memset(alien,0,sizeof(alien));
        num=0;
        for(int i=0; i<n; i++)
        {
            cin.getline(pic[i],m+1);
            for(int j=0; j<m; j++)
                if(pic[i][j]==‘A‘ || pic[i][j]==‘S‘)
                {
                    num++;
                    alien[i][j]=num;
                }
        }
        allroad=-1;
        int flag=0;
        int c=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                {
                    if(alien[i][j]>0)
                    {
                        bfs(i,j);
                        c++;
                    }
                    if(c==num-1){flag=1;break;}
                }
            if(flag == 1)break;
        }
        sort(road,road+allroad+1,cmp);
        for(int i=1;i<=num;i++)
            father[i]=i;
        c=0;
        sum=0;
        for(int i=0;i<=allroad;i++)
        {
            int ha = findd(road[i].a);
            int hb = findd(road[i].b);
            if(ha != hb)
            {
                c++;
                father[ha]=hb;
                sum += road[i].dis;
                if(c == num-1)break;
            }
        }
        printf("%d\n",sum);

    }
    return 0;
}
int findd(int x)
{
    int a=x;
    while(father[x]!=x)
        x=father[x];
    while(x!=father[x])
    {
        int z=a;
        a=father[a];
        father[z]=x;
    }
    return x;
}

原文地址:https://www.cnblogs.com/20172674xi/p/9536190.html

时间: 2024-10-01 05:59:10

POJ - 3026(BFS+最小生成树.krustal)的相关文章

poj 3026(BFS+最小生成树)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12032   Accepted: 3932 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 desc

Borg Maze - poj 3026(BFS + Kruskal 算法)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 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 gr

poj 3026 Borg Maze (bfs + 最小生成树)

链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走),空格代表空位(可走),S代表搜索起点(可走) A代表外星人站(可走),现在要从S出发,将S和所有的A之间都连通,求路线总距离最小值 分析:可以先用bfs将所有的A,S两两之间的最短距离,题目的目的是将S与所有的A连通,使得总距离最小, 所有任选一点开始按最小生成树的算法做就行,并非非要从S点开始 注:题目输入x,y后可能有很多空格,可以用gets将多余的空格取走,开数组是尽量开大点,之前虽然开的比题目数据     稍大,但一

POJ 3026 Borg Maze &amp; UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)

http://poj.org/problem?id=3026 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1248 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8498   Accepted: 2862 Description The Bor

POJ - 3026 Borg Maze BFS加最小生成树

Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算读懂了题目,也比较难想到这用到了最小生成树的知识,因为可以分身,所以每个点可以向其他点都连上边.可以用bfs预处理出所有的S点,A点的连线,再跑一遍最小生成树,即可得出答案.这里有几点注意,一开始我bfs没有记录step,而是直接找到一点后算曼哈顿距离,这是不对的,因为可能是绕了一个圈到了这个点.还

POJ 3026 Borg Maze

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7998   Accepted: 2675 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 descr

【POJ 3026】Borg Maze

[POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队可以无限分割 问搜索到所有alien所需要的总步数 即求一个无向图 包含所有的点并且总权值最小(最小生成树 BFS+最小生成树 Prim/Kruskal-懒死了 就这么贴吧--凑活看( ̄┰ ̄*) #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue&

poj 3026

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8383   Accepted: 2813 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 descr

POJ3026——Borg Maze(BFS+最小生成树)

Borg Maze DescriptionThe 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