• 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;
    }
    



  • 相关阅读:
    Sql Server中的游标最好只用于有主键或唯一键的表
    SQLServer中DataLength()和Len()两内置函数的区别(转载)
    Sql server bulk insert
    ASP.NET CORE中使用Cookie身份认证
    用.net中的SqlBulkCopy类批量复制数据 (转载)
    使用C#的AssemblyResolve事件和TypeResolve事件动态解析加载失败的程序集
    Entity framework 中Where、First、Count等查询函数使用时要注意
    注意SSIS中的DT_NUMERIC类型转换为字符类型(比如DT_WSTR)时,会截断小数点前的0
    记一次完整的android源码截屏事件的捕获<标记砖>
    ffmpeg添加水印的方法举例 (砖)
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038401.html
Copyright © 2020-2023  润新知