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
题目链接:http://poj.org/problem?id=3026
题意:给你一个地图,求将地图中的字符连接起来的最小生成树。
解题思路:A和S是一样的,所以从哪个点开始都是一样的,最小生成树直接套模板就可以,在之前只要把每个字符间的距离存入地图中即可,那么因为点比较少,直接利用bfs枚举每一个字符到其余字符的距离即可,如第一个样例,一共需要3*4的距离,距最后套用prim模板即可。
代码:
#include<stdio.h>
#include<string.h>
#define inf 99999999
int m,n,num;
char s[110][110];
int a[110][110],b[110][110];
int map[110][110],dis[110],book[110];
int next[4][2]={-1,0, 0,-1, 1,0, 0,1};
struct note
{
int x;
int y;
int s;
}q[3010],t,f;
void bfs(int x,int y)
{
//printf("x---%d,y---%d\n",x,y);
int head,tail,k,d;
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;
d=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;
d++;
if(d==num)
return;
}
b[f.x][f.y]=1;
q[tail]=f;
tail++;
}
head++;
}
}
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;
}
另外:
#include<stdio.h>
#include<string.h>
#define inf 99999999
int m,n,num;
char s[110][110];
int a[110][110],b[110][110];
int map[110][110],dis[110],book[110];
int next[4][2]={-1,0, 0,-1, 1,0, 0,1};
struct note
{
int x;
int y;
int s;
}q[3010],t,f;
void bfs(int x,int y)
{
int head,tail,k,d;
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;
d=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;
d++;
if(d==num)
return;
}
b[f.x][f.y]=1;
q[tail]=f;
tail++;
}
head++;
}
}
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;
char aa[110];
scanf("%d",&t);
while(t--)
{
num=0;
scanf("%d%d",&m,&n);
gets(aa);
// 6 5\r\n
// 6 5\n
//
// ch=getchar();//scanf("%c",&ch);
// while(ch!='\n')
// ch=getchar();
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;
}