zoukankan      html  css  js  c++  java
  • Poj3026--Borg Maze(Bfs+Prime)

    Borg Maze
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11160   Accepted: 3667

    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

    Source

     
    看题第一眼我是懵B的, 看了点儿小提示知道了是用Bfs先跑出各个顶点到其他点的距离, 然后再用Prime求最小生成树。
    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define N 51
    const int M = 251; 
    const int INF = 0x3f3f3f3f;
    using namespace std;
    int dis[M], map[M][M], vist[M];
    char Maze[N][N]; int n, m, k, vis[N][N];
    int ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
    struct Magic{
        int x, y, step, id;
    }num[M], r, s, t;
    void Bfs(int a, int  b, int c){
        memset(vis, 0, sizeof(vis));
        vis[a][b] = 1;
        r.x = a; r.y = b; r.step = 0;
        queue<Magic> Q;
        Q.push(r);
        while(!Q.empty()){
            s = Q.front();
            Q.pop();
            for(int i = 0; i < k; i++)
                if(num[i].x == s.x && num[i].y == s.y)
                    map[num[i].id][c] = map[c][num[i].id] = s.step; 
            for(int i = 0; i < 4; i++){
                t.x = s.x + ac[i][0];
                t.y = s.y + ac[i][1];
                t.step = s.step + 1;
                if(t.x >= 0 && t.x < n && t.y >= 0 && t.y < m && Maze[t.x][t.y] != '#' && !vis[t.x][t.y]){
                    vis[t.x][t.y] = 1;
                    Q.push(t);
                }
            }
        }
    }
    void Prime(){
        int sum = 0;
        memset(vist, 0, sizeof(vist));
        for(int i = 0; i < k; i++)
            dis[i] = map[0][i];
        vist[0] = 1;
        for(int i = 1; i < k; i++){
            int temp = 0, min = INF;
            for(int j = 0; j < k; j++){
                if(!vist[j] && dis[j] < min){
                    temp = j;
                    min = dis[j];
                }
            }
            if(min == INF)
                return;
            vist[temp] = 1;
            sum += min;
            for(int j = 0; j < k; j++){
                if(!vist[j] && dis[j] > map[temp][j])
                    dis[j] = map[temp][j];
            }
        }
        printf("%d
    ", sum);    
    }
    int main(){
        int T;
        char ch[M]; 
        scanf("%d", &T);
        while(T--){
            k = 0;
            scanf("%d%d", &n, &m);
            gets(ch);   //此地有坑, n, m后可能会有空格的输入;
            for(int i = 0; i < m; i++)
                gets(Maze[i]);
            for(int i = 0; i < m; i++)
                for(int j = 0; j < n; j++){
                    if(Maze[i][j] == 'S' || Maze[i][j] == 'A'){
                        num[k].x = i; num[k].y = j; num[k].id = k++;
                     }
                }
            for(int i = 0; i < k; i++){
                int P = num[i].x;
                int Q = num[i].y;
                Bfs(P, Q, i);
            }
            Prime();
        }
        return 0;
    }
     
  • 相关阅读:
    leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
    【jQuery 区别】.click()和$(document).on("click","指定的元素",function(){});的区别
    【前台 submit的重复提交 错误】submit的重复提交
    【spring 注解 错误】使用controller 作为后台给前台ajax交互数据出错
    【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能
    【Filter 页面重定向循环】写一个过滤器造成的页面重定向循环的问题
    【前台页面 BUG】回车按钮后,页面自动跳转
    【hibernate 执行方法未插入数据库】hibernate的save方法成功执行,但是未插入到数据库
    【maven 报错】maven项目执行maven install时报错Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
    【hibernate criteria】hibernate中criteria的完整用法 转
  • 原文地址:https://www.cnblogs.com/soTired/p/5020173.html
Copyright © 2011-2022 走看看