zoukankan      html  css  js  c++  java
  • (最小生成树) Borg Maze -- POJ -- 3026

    链接:

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

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/J

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10713   Accepted: 3559

    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

    代码:

    #include<algorithm>
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    
    const int maxn = 105;
    const int oo = 0xfffffff;
    
    int  dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
    char G[maxn][maxn];          //保存地图
    int  D[maxn][maxn];          //记录两点间的距离
    int  use[maxn][maxn];        //标记地图
    int  Index[maxn][maxn];      //记录‘A’或者‘B’的编号
    struct node{int x, y, step;};
    
    void BFS(int k, int M,int N, int x, int y)
    {
        queue<node> Q;
        node s;
        s.x = x, s.y = y, s.step = 0;
        use[s.x][s.y] = k; Q.push(s);
    
        while(Q.size())
        {
            s = Q.front();Q.pop();
            if(G[s.x][s.y]>='A' && G[s.x][s.y] <='Z')
                D[k][ Index[s.x][s.y] ] = s.step;
    
            for(int i=0; i<4; i++)
            {
                node q = s;
                q.x += dir[i][0], q.y += dir[i][1];
    
                if(q.x>=0&&q.x<M && q.y>=0&&q.y<N && G[q.x][q.y]!='#' && use[q.x][q.y]!=k)
                {
                    use[q.x][q.y] = k;
                    q.step += 1;
                    Q.push(q);
                }
            }
        }
    }
    int  Prim(int N)            //这里面的N代表编号最多到N
    {
        int i, dist[maxn], vis[maxn]={0, 1};
        int ans = 0, T=N-1;
    
        for(i=1; i<=N; i++)
            dist[i] = D[1][i];
    
        while(T--)
        {
            int k=1, mini = oo;
    
            for(i=1; i<=N; i++)
            {
                if(!vis[i] && mini > dist[i])
                    mini = dist[i], k=i;
            }
            ans += mini;
            vis[k] = true;
    
            for(i=1; i<=N; i++)
                if(!vis[i])dist[i] = min(dist[i], D[k][i]);
        }
    
        return ans;
    }
    
    int main()
    {
        int T;
    
        scanf("%d", &T);
    
        while(T--)
        {
            int i, j, M, N, t=1;
    
            scanf("%d%d ", &N, &M);
    
            for(i=0; i<M; i++)
            {
                gets(G[i]);
                for(j=0; j<N; j++)
                {
                    if(G[i][j]>='A' && G[i][j]<='Z')
                        Index[i][j] = t++;
                    use[i][j] = 0;
                }
            }
    
            for(i=0; i<M; i++)
            for(j=0; j<=N; j++)
            {
                if(G[i][j]>='A' && G[i][j]<='Z')
                    BFS(Index[i][j], M, N, i, j);
            }
    
            int ans = Prim(t-1);
    
            printf("%d
    ", ans);
        }
    
        return 0;
    }
    勿忘初心
  • 相关阅读:
    [hibernate]org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter
    [extjs] extjs 5.1 API 开发 文档
    [java] Unsupported major.minor version 51.0 错误解决方案
    [kfaka] Apache Kafka:下一代分布式消息系统
    [spring] org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljav 解决
    [spring] 对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾
    [java] java 中Unsafe类学习
    [java] java 线程join方法详解
    [java] jstack 查看死锁问题
    ORACLE DG之参数详解
  • 原文地址:https://www.cnblogs.com/YY56/p/4735119.html
Copyright © 2011-2022 走看看