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;
    }
     
  • 相关阅读:
    java基础16 捕获、抛出以、自定义异常和 finally 块(以及关键字:throw 、throws)
    java基础15 内部类(成员内部类、局部内部类)和匿名内部类
    java基础14 多态(及关键字:instanceof)
    java基础13 接口(及关键字:interface、implements)
    Java 线程控制
    Java 多线程创建和线程状态
    Java New IO
    Java IO流
    Java 集合和泛型
    Java 动态代理
  • 原文地址:https://www.cnblogs.com/soTired/p/5020173.html
Copyright © 2011-2022 走看看