Borg Maze

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

题意描述:
求出S和A之间距离的最小生成树,‘#’不能走。

解题思路:
将图形存入二维数组中,复制为一个整型的二维矩阵,给S和A排上序号墙的位置为-1,路为0;每次从序号位置开始广搜,找到与其他序号的距离,存入map数组,利用Prim算法求出最小生成树。

#include<stdio.h>
#include<string.h>
# define inf 99999999
int book[110],map[110][110],dis[110];
int a[110][110],food[110][110];//a数组存储复制的整型二维数组,food数组进行标记走过的点 
char s[110][110];
int n,m,num;
struct A
{
	int x;
	int y;
	int step;
}q[3010],t,f;
void bfs(int x,int y)
{
	int next[4][2]={-1,0,
					0,-1,
					1,0,
					0,1};
	int head,tail,k,s;
	memset(food,0,sizeof(food));
	head=0;
	tail=1;
	t.x=x;
	t.y=y;
	t.step=0;
	food[t.x][t.y]=1;
	q[head]=t;
	s=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||food[f.x][f.y]==1)//是墙或者越界、已走过跳过 
				continue;
			f.step=t.step+1;
			if(a[f.x][f.y]!=0&&food[f.x][f.y]==0)
			{
				map[a[x][y]][a[f.x][f.y]]=f.step;//存储当前序号点到下一点的距离 
				s++;
				if(s==num)
					return;
			}
			food[f.x][f.y]=1;
			q[tail++]=f;
		}
	}
}
int main()
{
	int i,j,u,v,min,t,sum;
	while(scanf("%d",&t)!=EOF)
	{
		while(t--)
		{
			scanf("%d%d\n",&m,&n);
			num=0;
			memset(a,0,sizeof(a));
			for(i=0;i<n;i++)
				gets(s[i]);
			memset(map,0,sizeof(map));
			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);
			memset(book,0,sizeof(book));
			for(i=1;i<=num;i++)
				dis[i]=map[1][i];
			book[1]=1;
			sum=0;
			for(i=1;i<num;i++)//prim算法 
			{
				min=inf;
				for(j=1;j<=num;j++)
				{
					if(book[j]==0&&dis[j]<min)
					{
						min=dis[j];
						u=j;
					}
				}
				book[u]=1;
				sum=sum+dis[u];
				for(v=1;v<=num;v++)
				{
					if(book[v]==0&&dis[v]>map[u][v])
						dis[v]=map[u][v];
				}
			}	
			printf("%d\n",sum);		
		}
	}
	return 0;
}