Borg Maze POJ - 3026 BFS+最小生成树

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 letterS’’ 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 Input

8
11

解决
题意其实刚开始有点不太好转化(图论不都是这样吗,) 我们主要要抓住总和最小的步数这个条件 然后我们又发现题目上的S和A两种标记点都可以分裂 自然而然想到最小生成树 问题的关键在于建图 我们需要每两个标志点之间的最小距离 方便生成一个权值最小的树 那么我们的方法就是bfs 刚开始觉得每两点之间都要计算最短路 那么未免时间耗费也太大了 但是给定的点却很少 所以方案是可行的 我们的最终方案就是bfs建图(邻接矩阵和邻接表均可(邻接表堆优化更方便)),图上跑一边最小生成树 当然克鲁斯卡尔和普里姆都是ok的 最后注意输入(吃掉数字后面的空格)

AC代码

//bfs建图 + 最小生成树
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<cstdlib>
#include<utility>
#include<cstdio>
#include<cstring>
using namespace std;

namespace{
    const int INF = 0x3f3f3f3f;
    const int maxn = 2000;
    typedef pair<int,int>pa;
    typedef pair<int,pa>pii;
    bool vis[maxn][maxn];//标记
    int w[maxn][maxn];//bfs建立的图 可以邻接表表示
    char mp[maxn][maxn];//初始图
    int low[maxn];//Prim中用到的到每一点的最小权值
    int q,m,n,flag;
    map<pa,int>id;//给每一个点给予标记
    vector<pa>vec;
    int x[4] = {0,0,-1,1};
    int y[4] = {1,-1,0,0};
}

int bfs(pa& p){
    memset(vis,0,sizeof(vis));
    queue<pii>que;
    que.push(make_pair(0,p));//第一个参数为步数
    vis[p.first][p.second] = true;
    while(!que.empty()){
        pii tmp = que.front();
        que.pop();
        for(int i=0;i<4;++i){
            int xx = tmp.second.first+x[i];
            int yy = tmp.second.second+y[i];
            if(mp[xx][yy]!='#' && xx>=0 && xx<n && yy>=0 && yy<m && !vis[xx][yy]){
                vis[xx][yy] = 1;
                if(mp[xx][yy] == 'A' || mp[xx][yy] == 'S'){
                    w[flag][id[make_pair(xx,yy)]] = tmp.first+1;
                    w[id[make_pair(xx,yy)]][flag] = tmp.first+1;//无向图
                }else{//" "
                    que.push(make_pair(tmp.first+1,make_pair(xx,yy)));
                }
            }
        }
    }
}

int Prim(int cep){//邻接矩阵的表示法
    //low数组维护一个距x点的最短距离
    bool vis[maxn];
    memset(vis,0,sizeof(vis));
    vis[0] = true;//从零点开始找
    int ans = 0,p = 0;
    for(int i=0;i<cep;++i) low[i] = w[p][i]; 
    for(int i=1;i<cep;++i){
        int Max = INF;
        for(int j=0;j<cep;++j){//找到最小权
            if(!vis[j] && Max>low[j]){
                Max = low[p=j];
            }
        }
        ans += Max;
        vis[p] = 1;
        for(int j=0;j<cep;++j){
            if(!vis[j] && low[j] > w[p][j])
                low[j] = w[p][j];
        }
    }
    return ans;
}

void init(){
    id.clear();
    vec.clear();
    memset(low,0,sizeof(low));
}

int main(){
    scanf("%d",&q);
    while(q--){
        init();
        scanf("%d %d",&m,&n);
        cin.getline(mp[0],100);
        for(int i=0;i<n;++i){
            cin.getline(mp[i],100);//gets用不了
            for(int j=0;j<m;++j){
                if(mp[i][j]=='A' || mp[i][j]=='S'){
                    id[make_pair(i,j)] = vec.size(); //给每一个点给予一个编号
                    vec.push_back(make_pair(i,j));
                }
            }
        }
        int temp=vec.size();
        for(int i=0;i<temp;++i)
        for(int j=0;j<temp;++j)
        if(i==j) w[i][j] = 0;
        else w[i][j] = INF;

        for(int i=0;i<vec.size();++i){
            flag = id[vec[i]];
            bfs(vec[i]);
        }
        printf("%d\n",Prim(vec.size()));
    }
    return 0;
}
posted @ 2022-07-02 13:18  李兆龙的博客  阅读(17)  评论(0编辑  收藏  举报