• POJ


    题目:

    题目链接:

    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
    ",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;
    }
  • 相关阅读:
    BIOS/MBR UEFI/GPT关系与区别-资料整理
    raid 简单了解
    MBR主引导记录
    linux 安装vscode
    chrome 获得点击按钮时的事件
    python计算纪念日相关
    python error: curl: (1) Protocol "'https" not supported or disabled in libcurl
    linux go with vscode
    postman 进阶技巧
    mysql常用时间函数与类型转换
  • 原文地址:https://www.cnblogs.com/20172674xi/p/9536190.html
Copyright © 2020-2023  润新知