Borg Maze(bfs+prim)

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点便可分裂成多个,求把所有A都吃完需要多少步。

解题思路:A和S是一样的,本题就是让求连接A和S的最小生成树。首先枚举每一个结点进行一遍BFS,求该结点到其他所有结点的距离,若距离小于初始值,则更新该距离。这样最终得到的就是每两个结点之间的最短距离。再套用prim模板即可。

代码:

#include<stdio.h>
#include<string.h>
#define inf 99999999
char s[110][110];
int a[110][110],b[110][110];
int map[110][110],dis[110],book[110];
int m,n,num;
struct note
{
	int x;
	int y;
	int s;
};
struct note q[3010],t,f;

void bfs(int x,int y)
{
	//printf("x---%d,y---%d\n",x,y);
	int head,tail,k,w;
	int next[4][2]={-1,0,0,-1,1,0,0,1};
	memset(b,0,sizeof(b));
	head=0;
	tail=1;
	t.x=x;
	t.y=y;
	t.s=0;
	b[t.x][t.y]=1;
	q[head]=t;
	w=1;
	while(head<tail)
	{
		t=q[head++];
		for(k=0;k<4;k++)
		{
			f.x=t.x+next[k][0];
			f.y=t.y+next[k][1];
			if(a[f.x][f.y]==-1||f.x<0||f.y<0||f.x>=n||f.y>=m||b[f.x][f.y]==1)
				continue;
			f.s=t.s+1;
			if(a[f.x][f.y]!=0&&b[f.x][f.y]==0)
			{
				map[a[x][y]][a[f.x][f.y]]=f.s;
				w++;
				if(w==num)
					return;
			}
			b[f.x][f.y]=1;
			q[tail++]=f;
		}
	}
}

void prim()
{
	int sum=0;
	int min,i,j,u,k;
	memset(book,0,sizeof(book));
  	/*for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
			{
				printf("%d ",map[i][j]);
			}			
			printf("\n");
	}*/
		
	for(i=1;i<=num;i++)
		dis[i]=map[1][i];
	//for(i=1;i<=num;i++)
		//printf("%d ",dis[i]);	
	book[1]=1;
	for(i=1;i<num;i++)
	{
		min=inf;
		for(j=1;j<=num;j++)
			if(book[j]==0&&dis[j]<min)
			{
				min=dis[j];
				u=j;
			}
		book[u]=1;
		sum+=dis[u];
		for(k=1;k<=num;k++)
			if(book[k]==0&&dis[k]>map[u][k])
				dis[k]=map[u][k];
	}
	printf("%d\n",sum);	
}

int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		num=0;
		scanf("%d%d\n",&m,&n);
		memset(a,0,sizeof(a));
		memset(map,0,sizeof(map));
		for(i=0;i<n;i++)
			gets(s[i]);
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				if(s[i][j]=='#')
					a[i][j]=-1;
				if(s[i][j]=='A'||s[i][j]=='S')
				{
					num++;   //记录顶点数目 
					a[i][j]=num; //记录顶点的位置 
				}	
			}
			
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				if(a[i][j]!=0)
					bfs(i,j);
		prim();	
	}
	return 0;
}

 

猜你喜欢

转载自blog.csdn.net/hello_cmy/article/details/81354470