zoukankan      html  css  js  c++  java
  • POJ 3026 Borg Maze

    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

    题意:m*n的地图,'A'代表外星人,'S'代表查找的始点,现在要求从S出发找到所有的A,问最小的花费是多少,每一步的移动都花费1。

    首先要BFS求出任意一个S或A之间的最小花费,然后构图,建立最小生成树,得到最小权值即最小花费

    #include<stdio.h>
    #include<queue>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const int N=110;
    
    struct node
    {
        int x, y, s;
    };
    int G[N][N], dist[N], vis[N], no[N][N], m, n, ans; ///no数组存放的是第(i,j)位置是A或S的第几个序号,ans代表S和A的总个数
    int dir[4][2] = { {1,0}, {-1,0}, {0,1}, {0,-1} };
    char Map[N][N];
    
    void Init()
    {
        int i, j;
    
        for (i = 0; i < N; i++)
        {
            dist[i] = INF;
            vis[i] = 0;
            for (j = 0; j < N; j++)
            {
                G[i][j] = INF;
                no[i][j] = 0;
            }
            G[i][i] = 0;
        }
        ans = 0;
    }
    
    void BFS(int x, int y, int z)
    {
        int i, visit[N][N], nx, ny;
        node now, next;
        queue<node>Q;
    
        memset(visit, 0, sizeof(visit));
    
        now.x = x; now.y = y; now.s = 0;
        Q.push(now);
        visit[x][y] = 1;
    
        while (!Q.empty())
        {
            now = Q.front(); Q.pop();
    
            if (Map[now.x][now.y] == 'S' || Map[now.x][now.y] == 'A')
                G[z][no[now.x][now.y]] = G[no[now.x][now.y]][z] = now.s; ///遇到A和S,就保存要查询的节点到该点的最小距离(最小生成树是无向图)
    
            for (i = 0; i < 4; i++)
            {
                next.x = nx = now.x + dir[i][0];
                next.y = ny = now.y + dir[i][1];
    
                if (nx >= 0 && nx < m && ny >= 0 && ny < n && Map[nx][ny] != '#' && !visit[nx][ny])
                {
                    visit[nx][ny] = 1;
                    next.s = now.s+1;
                    Q.push(next);
                }
            }
        }
    }
    
    int Prim()
    {
        int i, j, Min, idex, num = 0;
    
        for (i = 1; i <= ans; i++)
            dist[i] = G[1][i];
        vis[1] = 1; ///!!!易掉
    
        for (i = 1; i < ans; i++)
        {
            Min = INF;
    
            for (j = 1; j <= ans; j++)
            {
                if (!vis[j] && dist[j] < Min)
                {
                    Min = dist[j];
                    idex = j;
                }
            }
    
            vis[idex] = 1;
            num += Min;
    
            for (j = 1; j <= ans; j++)
            {
                if (dist[j] > G[idex][j] && !vis[j])
                    dist[j] = G[idex][j];
            }
        }
    
        return num;
    }
    
    int main ()
    {
        int T, i, j, num;
    
        scanf("%d", &T);
    
        while (T--)
        {
            Init();
    
            scanf("%d%d ", &n, &m);
            for (i = 0; i < m; i++)
                gets(Map[i]);
    
            for (i = 0; i < m; i++)
                for (j = 0; j < n; j++)
                    if (Map[i][j] == 'S' || Map[i][j] == 'A')
                        no[i][j] = ++ans; ///将该位置节点的序号记录下来
    
            for (i = 0; i < m; i++)
                for (j = 0; j < n; j++)
                    if (Map[i][j] == 'S' || Map[i][j] == 'A')
                        BFS(i, j, no[i][j]); ///每次遇到A和S就进行BFS,查找其与其他所有的S和A的距离
    
            num = Prim();
    
            printf("%d
    ", num);
        }
    
        return 0;
    }
  • 相关阅读:
    Android -- Camera2(Android5.0)
    Android -- Camera.ShutterCallback
    Android -- selector&&StateListDrawable
    Centos安装FTP服务器和配置
    Android -- setWillNotDraw()
    Android -- ViewPager切换动画,PageTransformer
    关于通信的关键词UDP/(TCP/IP)/IPC/RPC/.NET Remoting/WebService/WCF/Http 系列
    已禁用对分布式事务管理器(MSDTC)的网络访问。请使用组件服务管理工具启用 DTC 以便在 MSDTC 安全配置中进行网络访问。
    JavaScript的NaN-唯一 一个自己不等于自己的对象!!
    【ShoppingPeeker】-基于Webkit内核的爬虫蜘蛛引擎 ShoppingWebCrawler的姊妹篇-可视化任务Web管理
  • 原文地址:https://www.cnblogs.com/syhandll/p/4728246.html
Copyright © 2011-2022 走看看