• 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;
    }
  • 相关阅读:
    JSON连载java目的
    2014百度之星预赛(第二场)——Best Financing
    推断值的数组
    Codeforces 437E The Child and Polygon(间隔DP)
    ruby简单的基本 3
    定义和实现二叉树
    C++11并行编程-条件变量(condition_variable)详细说明
    【Bootstrap】自己主动去适应PC、平面、手机Bootstrap网格系统
    使用代码自定义UIView注意一二三
    关于 android 中 postDelayed方法的讲解
  • 原文地址:https://www.cnblogs.com/syhandll/p/4728246.html
Copyright © 2020-2023  润新知