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

    Borg Maze
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 17943   Accepted: 5759

    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

    题意:估计扫描整个迷宫并同化隐藏在迷宫中的相异个体的最小代价,扫描迷宫可以向北、西、南、东移动。棘手的是搜索是超过100个个体组成的群体进行的。当一个相异个体同化时,群体可裂为两个或多个子群体。

    分析:联想到Prim算法思想,每次加一个点,然后找出最短的边,将连接的另一个点加入。用bfs找出每个点到其他点的的距离,然后用Prim找出最小生成树的权

    AC代码

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    using namespace std;
    const int N=1010;
    const int inf=0x3f3f3f3f3f;
    struct node{
        int x,y,s;
        node(){};
        node(int X,int Y,int S): x(X),y(Y),s(S){};
    }q[N*N];
    char s[N][N];
    int e[N][N],a[N][N],dis[N],Next[N];
    int nxt[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
    int n,m,cnt;
    bool vis[N][N],book[N];
    void bfs(int x,int y,int id){
        memset(vis,false, sizeof(vis));
        queue<node> q;
        node t=node(x,y,0);
        q.push(t);
        vis[x][y]=true;
        while(!q.empty()){
            t=q.front();
            q.pop();
            for(int k=0;k<=3;k++){
                int tx=t.x+nxt[k][0];
                int ty=t.y+nxt[k][1];
                if(tx<0||ty<0||tx>=n||ty>=m||s[tx][ty]=='#'||vis[tx][ty]) continue;
                vis[tx][ty]=true;
                if(a[tx][ty]) e[id][a[tx][ty]]=t.s+1;
                node now=node(tx,ty,t.s+1);
                q.push(now);
            }
        }
    }
    int Prim(int st){
        int ans=0;
        memset(book,false, sizeof(book));
        book[st]=true;
        for(int i=1;i<=cnt;i++){
            dis[i]=e[st][i];
        }
        int minn=inf,u;
        for(int i=1;i<cnt;i++){
            minn=inf;
            for(int j=1;j<=cnt;j++){
                if(dis[j]<minn&&!book[j]){
                    minn=dis[j];
                    u=j;
                }
            }
            if(minn==inf) break;
            ans+=minn;
            book[u]=true;
            for(int j=1;j<=cnt;j++){
                if(dis[j]>e[u][j]&&!book[j]){
                    dis[j]=e[u][j];
                }
            }
        }
        return ans;
    }
    void init(){
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(a[i][j]) bfs(i,j,a[i][j]);
            }
        }
    }
    int main(){
        int t;
        scanf("%d",&t);
        while(t--){
    
            memset(a,0, sizeof(a));
            memset(e,0,sizeof(e));
            memset(q,0,sizeof(q));
            scanf("%d%d",&m,&n);
            int c;
            while((c=getchar())==' ');
            for(int i=0;i<n;i++){
                gets(s[i]);
            }
            cnt=0;
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    if(s[i][j]=='A'||s[i][j]=='S'){
                        a[i][j]=++cnt;
                    }
                }
            }
            init();
            int ans=Prim(1);
            printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    github上的每日学习 13
    github上的每日学习 12
    github上的每日学习 11
    github上的每日学习 10
    github上的每日学习 9
    github上的每日学习 8
    github上的每日学习 7
    面向对象程序设计寒假作业2
    MySQL安装和配置
    Fast Packet Processing with eBPF and XDP部分
  • 原文地址:https://www.cnblogs.com/kun-/p/9722080.html
Copyright © 2011-2022 走看看