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;
    }
    勿忘初心
  • 相关阅读:
    CTFHub-Web-Web前置技能-HTTP协议-响应包源代码详解
    BurpSuite环境安装及设置
    i2 Analyst’s Notebook 9学习笔记之入门、基本概念和数据接口
    Python 练习题:用索引取出LIST中的值
    python 练习题:小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点
    zabbix4.0 本地安装详解及步骤
    CentOS 7 安装 mysql 5.7.27 for zabbix
    win7系统 右击任务栏 资源管理器 弹出菜单“已固定”和“最近”项目不显示故障处理
    CentOS 7 新系统 手动配置网络 简要步骤
    CentOS7 firewalld防火墙 启动 关闭 禁用 添加删除规则等 常用命令
  • 原文地址:https://www.cnblogs.com/YY56/p/4735119.html
Copyright © 2011-2022 走看看