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的最小生成树,#为墙。

思路:

    先找到地图中所有的A和S,并给他们编号。利用广度优先搜索出他们两两之间的距离存入map数组中,然后利用Prim求出最小生成树即可。

    输入是个坑,因为第二行的m,n后面可能还有许多空格,要么直接换行,要么gets掉,不然会WA。

    没有仔细看输出,所以数组开小了,数组开小了竟然不是RE而是WA。

代码:

#include<stdio.h>
#include<string.h>
#define inf 99999999
int map[60][60],book[60],dis[60];
int book1[60][60],s[60][60];
char a[60][60];
int m,n,c;
struct note
{
	int x,y,step;
}que[3010],f,t;
void bfs(int x,int y)
{
	int next[4][2]={0,1, 1,0, -1,0, 0,-1};
	memset(book1,0,sizeof(book1));
	int head,tail,k,count;		
	head=0;
	tail=1;
	t.x=x;
	t.y=y;
	t.step=0;
	book1[t.x][t.y]=1;
	que[head]=t;
	count=1;
	while(head<tail)
	{
		t=que[head++];
		for(k=0;k<4;k++)
		{
			f.x=t.x+next[k][0];
			f.y=t.y+next[k][1];
			if(s[f.x][f.y]==-1||f.x<0||f.y<0||f.x>=m||f.y>=n||book1[f.x][f.y]==1)
				continue;
			f.step=t.step+1;
			if(s[f.x][f.y]!=0&&book1[f.x][f.y]==0)
			{
				map[s[x][y]][s[f.x][f.y]]=f.step;
				count++;
				if(count==c)
					return;
			}
			book1[f.x][f.y]=1;
			que[tail++]=f;
		}
	}
}
void prim()
{
	int i,j,k,count=0,sum=0,min,u;
	for(i=1;i<=c;i++)
		dis[i]=map[1][i];
	book[1]=1;
	for(i=1;i<c;i++)
	{
		min=inf;
		for(j=1;j<=c;j++)
		{
			if(book[j]==0&&dis[j]<min)
			{
				min=dis[j];
				u=j;
			}
		}
		book[u]=1;
		sum+=dis[u];
		for(k=1;k<=c;k++)
		{
			if(book[k]==0&&dis[k]>map[u][k])
				dis[k]=map[u][k];
		}
	}
	printf("%d\n",sum);
}
int main()
{
	int t,i,j;
	scanf("%d",&t);
	while(t--)
	{
		c=0;
		memset(book,0,sizeof(book));
		memset(s,0,sizeof(s));
		memset(map,0,sizeof(map));
		scanf("%d%d\n",&n,&m);
		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]=='#')
					s[i][j]=-1;
				if(a[i][j]=='A'||a[i][j]=='S')
					s[i][j]=++c;
			}
		for(i=0;i<m;i++)
			for(j=0;j<n;j++)
			{
				if(s[i][j]>0)
					bfs(i,j);
			}
		prim();
	}
	return 0;
}