Borg Maze (最小生成树+bfs)


    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

题意:

给你一个地图,地图中A代表外星人,S代表出发点,从S点出发,每遇到一个A点S便可分裂成多个。求把所有A都吃完需要多少步。

分析:

其实A和S是一样的,本题的本质就是求连接A和S的最小生成树。无非麻烦的一点就是各个结点之间的权值。

这里采取的办法是,枚举每一个结点进行一遍BFS,求该结点到其他所有结点的距离,若距离小于初始值,则更新该距离。

这样最终得到的就是每两个结点之间的最短距离。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<queue>
void bfs(int sx,int sy);
void prim();
int next[4][2]={1,0,-1,0,0,1,0,-1};
struct data
{
	int x;
	int y;
	int step;
};
#define inf 999999999
char a[110][110];
int b[110][110],book[110][110],e[110][110],m,n,sumn;
int main()
{
	int T,i,j;
	scanf("%d",&T);
	while(T--)
	{
		sumn=0;
		memset(b,0,sizeof(b));
		scanf("%d%d ",&n,&m);
		for(i=1;i<=55;i++)
			for(j=1;j<=55;j++)
			{
				if(i==j)
					e[i][j]=0;
				else
					e[i][j]=inf;
			}
		for(i=0;i<m;i++)
			gets(a[i]);
		for(i=0;i<m;i++)
			for(j=0;j<n;j++)
				if(a[i][j]=='A'||a[i][j]=='S')
				{
					sumn++;
					b[i][j]=sumn;
				}
		for(i=0;i<m;i++)
			for(j=0;j<n;j++)
				if(b[i][j]!=0)
					bfs(i,j);
		prim();
	}
	return 0;
}
void bfs(int sx,int sy)
{
	int k,t=b[sx][sy];
	memset(book,0,sizeof(book));
	queue<data>que;
	while(!que.empty())
	{
		que.pop();
	}
	struct data A,B;
	A.x=sx;
	A.y=sy;
	A.step=0;
	que.push(A);
	book[sx][sy]=1;
	while(!que.empty())
	{
		A=que.front();
		que.pop();
		for(k=0;k<4;k++)
		{
			B.x=A.x+next[k][0];
			B.y=A.y+next[k][1];
			B.step=A.step+1;
			if(book[B.x][B.y]==1||B.x<0||B.y<0||B.x>=m||B.y>=n||a[B.x][B.y]=='#')
				continue;
			if(a[B.x][B.y]=='S'||a[B.x][B.y]=='A')
			{
				e[t][b[B.x][B.y]]=B.step;
				e[b[B.x][B.y]][t]=B.step;	
			}	
			book[B.x][B.y]=1;
			que.push(B); 
		}	
	} 
}
void prim()
{
	int vis[4000],dis[4000];
	memset(vis,0,sizeof(vis));
	int count=0,sum=0,min,i,j,u;
	for(i=1;i<=sumn;i++)
		dis[i]=e[1][i];
	vis[1]=1;
	count++;
	while(count<sumn)
	{
		min=inf;
		for(i=1;i<=sumn;i++)
			if(vis[i]==0&&dis[i]<min)
			{
				min=dis[i];
				u=i;
			}
		vis[u]=1;
		count++;
		sum+=dis[u];
		for(i=1;i<=sumn;i++)
			if(vis[i]==0&&dis[i]>e[u][i])
				dis[i]=e[u][i];
	}
	printf("%d\n",sum);	
}

猜你喜欢

转载自blog.csdn.net/queen00000/article/details/81295779
今日推荐