• poj 3026 Borg Maze bfs+最小生成树


    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
    ",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;
    }
    



  • 相关阅读:
    vue双向数据绑定原理
    vue-router原理
    CSS选择器优化
    静态资源的渲染阻塞
    什么是Base64?Base64的原理是什么?
    OAuth的解释
    AMD与CMD的区别
    AMD的实现
    组件通信的几种方式
    关于istream_iterator<int>(cin)和istream_iterator<int>()的一点分析
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6779947.html
Copyright © 2020-2023  润新知