poj 3026

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16203   Accepted: 5274

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到其他s或者A所用的最小步数,也就是相当于将所有的S或者A都是一个

点,现在只需要求出将这些点连接时所用到的最小步数即可。此处运用bfs求出一点到其他各点的路径长度,之后运用prim

求出最小的路径总和即可。

代码:

扫描二维码关注公众号,回复: 2343619 查看本文章

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 99999999
char s[60][60];
int m,n,max1;
int a1[4][2]={0,1,1,0,0,-1,-1,0};
int a[110][110],d[110][110],e[110][110];
typedef struct stu{
	int x,y,z;
}st; 
void bfs(int x,int y)
{
	st v,o;
	memset(e,0,sizeof(e));
	queue<st>q;
	v.x=x,v.y=y,v.z=0;
	e[x][y]=1;
	q.push(v);
	int i;
	while(!q.empty())
	{
		o=q.front();
		q.pop();
		if(s[o.x][o.y]=='A'||s[o.x][o.y]=='S')
		  a[d[x][y]][d[o.x][o.y]]=o.z;
		for(i=0;i<4;i++)
		{
			v.x=o.x+a1[i][0];
			v.y=o.y+a1[i][1];
			if(v.x>n||v.y>m||v.x<1||v.y<1||s[v.x][v.y]=='#'||e[v.x][v.y])
			 continue;
            v.z=o.z+1;
            e[v.x][v.y]=1;
            q.push(v);
		}
	}
	return ;
}
int main()
{
	int t,i,j,k,c,sum,min;
	int b[110],dis[110];
	char s0[110];	
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&m,&n);
		memset(s,0,sizeof(s));
		gets(s0);
		memset(d,0,sizeof(d));
		k=2;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				scanf("%c",&s[i][j]);
				if(s[i][j]=='A')
				  d[i][j]=k++; 
				if(s[i][j]=='S')
				  d[i][j]=1;
			}
			gets(s0);
		}
		for(i=1;i<=k;i++)
		 for(j=1;j<=k;j++)
		   if(i==j)
		    a[i][j]=0;
		   else
		    a[i][j]=inf;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				if(d[i][j])
				   bfs(i,j);
			}
		}
		for(i=1;i<k;i++)
		 dis[i]=a[1][i],b[i]=0;
		 b[1]=1;
		c=1;sum=0;
		while(c<k-1)
		{
			min=inf;
			for(i=1;i<k;i++)
			{
				if(!b[i]&&min>dis[i])
				  min=dis[i],j=i;
			}
			b[j]=1;c++;sum+=min;
			for(i=1;i<k;i++)
			{
				if(!b[i]&&dis[i]>a[j][i])
				  dis[i]=a[j][i];
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

错误分析:题目中的每行后面有可能有多个空格存在,不能用getchar()进行字符的处理。

猜你喜欢

转载自blog.csdn.net/qq_39259536/article/details/79539845
今日推荐