zoukankan      html  css  js  c++  java
  • 图论 --- BFS + MST

    Borg Maze
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7844   Accepted: 2623

    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<queue>
    #include<cstdio>
    #define INF 1<<30
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int n,m,ans,cnt;
    struct Node
    {
        int x,y,step;
    };
    Node node[110];
    int row,line;
    int dist[60];
    char Map[60][60];
    bool vis[60][60],vist[60]; //邻接矩阵存储
    int mark[60][60],dis[110][110];     //给每个字母一个编号
    int dir[4][2]={-1,0, 0,1, 1,0, 0,-1};
    
    void bfs(int index)
    {
        queue<Node> que;
        memset(vis, false, sizeof(vis));
        Node now, next;
        node[index].step = 0;
        que.push(node[index]);
        vis[node[index].x][node[index].y] = true;
        while(!que.empty())
        {
            now = que.front();
            que.pop();
            int x = now.x, y = now.y;
            for(int i = 0; i < 4; ++i)
            {
                int tx = x + dir[i][0], ty = y +dir[i][1];
                if(vis[tx][ty] == false && Map[tx][ty] != '#') //如果这一步可以走
                {
                    next.x = tx;
                    next.y = ty;
                    vis[tx][ty] = true;
                    next.step = now.step + 1;
                    que.push(next);
                    if(Map[next.x][next.y] == 'A' || Map[next.x][next.y] == 'S')
                        dis[ mark[ node[index].x ][ node[index].y ] ][ mark[next.x][next.y] ] = next.step;
                }
    
            }
        }
    }
    
    
    int prim()
    {
        for(int i = 0; i < cnt; ++i)
        {
            dist[i] = INF;
            vist[i] = false;
        }
        dist[0] = 0;
        while(1)
        {
            int min = INF, now = -1;
            for(int i = 0; i < cnt; ++i)
            {
                if(min > dist[i] && vist[i] == false)
                {
                    min = dist[i];
                    now = i;
                }
            }
            if(now == -1)
                return ans;
            ans += min;
            vist[now] = true;
            for(int i = 0; i < cnt; ++i)
                if(vist[i] == false && dist[i] > dis[now][i])
                    dist[i] = dis[now][i];
        }
        return ans;
    }
    
    int main()
    {
        int n_case;
        scanf("%d", &n_case);
        while(n_case--)
        {
            cnt = ans = 0;
            memset(mark, 0, sizeof(mark));
            scanf("%d%d", &row, &line);
            char ch;
            while(ch = getchar(), ch != '
    ');
            for(int i = 0; i < line; ++i)
            {
                for(int j = 0; j < row; ++j)
                {
                    Map[i][j] = getchar();
                    if(Map[i][j] == 'A' || Map[i][j] == 'S')
                    {
                        mark[i][j] = cnt;
                        node[cnt].x = i;
                        node[cnt++].y = j;
                    }
                }
                while(ch = getchar(), ch != '
    ');
            }
            for(int i = 0; i < cnt; ++i)
                bfs(i);  
            prim();
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    Python 编程笔记(小白初学篇)
    博客园设置好看的主题!站在巨人的肩膀上眺望远方!!
    Matlab 画地图之 m_map
    SSO+PHS 同步问题修正解决
    从已删除邮箱copy数据到活动邮箱
    微软AD相关操作的免费工具
    phpize是什么
    apache的bin目录下的apxs有什么作用? PHP模块加载运行方式
    docker
    sed 命令
  • 原文地址:https://www.cnblogs.com/crazyacking/p/3751838.html
Copyright © 2011-2022 走看看