POJ 3026 Borg Maze(最小生成树)

    xiaoxiao2021-03-25  217

    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 Output

    8 11

    题目大意 题意有些晦涩难懂…直白讲就是在这样一个二维矩阵里,求‘S’到达所有‘A’的最短路径,即最小生成树。

    解题思路 最小生成树的算法还是模板化,就是需要自己通过广搜计算各路径长度,对为‘A’或‘S’的地方标记,进行BFS得出任意两点间距离。

    代码实现

    #include <iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define maxn 106 #define INF (1<<26) char matrix[maxn][maxn]; //初始的二维矩阵 int V[maxn][maxn]; //标记该位置是否是'A'或'S' int edge[maxn][maxn]; //任意两点间的距离 int mincost[maxn] ; //从当前生成树集合 S 到每个顶点的最小权值 int length[maxn][maxn]; //标记当前点与点之间的距离,然后赋值于edge[][] int m,n; int dire[][2]= {{1,0},{0,1},{-1,0},{0,-1}}; bool flag[maxn]; void bfs(int fx,int fy) { queue<pair<int,int> >q; memset(length,-1,sizeof(length)); length[fx][fy]=0; q.push(make_pair(fx,fy)); while(!q.empty()) { pair<int,int> temp=q.front(); q.pop(); if(V[temp.first][temp.second]!=-1) edge[V[fx][fy]][V[temp.first][temp.second]]=length[temp.first][temp.second]; for(int i=0; i<4; i++) { int nx=temp.first+dire[i][0]; int ny=temp.second+dire[i][1]; if(nx>0&&nx<n&&ny>0&&ny<m&&matrix[nx][ny]!='#'&&length[nx][ny]==-1) { length[nx][ny]=length[temp.first][temp.second]+1; q.push(make_pair(nx,ny)); } } } } void prim(int x) { for(int i=0; i<x; i++) { mincost[i]=INF; flag[i]=false; } int ans=0; mincost[0]=0; while(true) { int v=-1; for(int u=0; u<x; u++) if(!flag[u]&&(v==-1||mincost[u]<mincost[v])) v=u; if(v==-1)break; ans+=mincost[v]; flag[v]=true; for(int u=0; u<x; u++) mincost[u]=min(mincost[u],edge[u][v]); } printf("%d\n",ans); } int main() { int T; int num; scanf("%d",&T); while(T--) { num=0; memset(V,-1,sizeof(V)); scanf("%d %d\n",&m,&n); for(int i=0; i<n; i++) { gets(matrix[i]); for(int j=0; j<m; j++) if(matrix[i][j]=='A'||matrix[i][j]=='S') V[i][j]=num++; } for(int i=0; i<n; i++) for(int j=0; j<n; j++) { if(V[i][j]!=-1) bfs(i,j); } prim(num); } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-219.html

    最新回复(0)