POJ 3026 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 lette"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

题意描述:
给出一个n*m的矩阵‘#’代表墙,‘S’起始位置,‘A’代表外星人,在找到一个外星人后,他能被同化也会去找其它的外星人,现在问把所有的外星人全部找完最少需要都少步。

在对A和S标号完毕后,就可以直接去用bfs来构建这个图了,无需在找到S。

解题思路:
先用bfs去搜每个A到S的距离和A到A之间的距离用一个二维数组取构建一个S与A的图,然后再用prim去找出最短的路。

#include<stdio.h>
#include<string.h> 
 
 struct node 
 {
 	int x,y,s;
 }p[2505];
 
char e[55][55];
int next[4][2]={1,0, -1,0, 0,1, 0,-1};
int e1[55][55],step[105][105],m,n;

void bfs(int x,int y)
{
	int book[55][55]; 
	int stratx=x,straty=y;
	int head,tail,k,tx,ty;
	head=1;
	tail=1;
	p[tail].x=stratx;
	p[tail].y=straty;
	p[tail].s=0;
	
	memset(book, 0, sizeof(book));
	book[stratx][straty]=1;
	tail++;
	while(head<tail)
	{
		for(k=0;k<4;k++)
		{
			tx=p[head].x+next[k][0];
			ty=p[head].y+next[k][1];
				
			if(tx<1||ty<1||tx>n||ty>m)
				continue;
						
			if(e[tx][ty]!='#'&&book[tx][ty]==0)
			{
				book[tx][ty]=1;
				p[tail].x=tx;
				p[tail].y=ty;
				p[tail].s=p[head].s+1;
				if(e[tx][ty]=='A'||e[tx][ty]=='S')
					step[e1[tx][ty]][e1[stratx][straty]]=step[e1[stratx][straty]][e1[tx][ty]]=p[tail].s;//构建A与S的二维图
				tail++;
			}
		}
		head++;
	}
}

int prim(int v)
{
	int i,j,count=1,sum=0;
	int min,inf=99999999;
	int dis[2505],book1[2505];
	memset(book1,0,sizeof(book1));
	for(i=1;i<=v;i++)
		dis[i]=step[1][i];	
	book1[1]=1;
	while(count<v)
	{
		min=inf;
		for(i=1;i<=v;i++)
		{
			if(book1[i]==0&&dis[i]<min)
			{
				min=dis[i];
				j=i;
			}
		}
		book1[j]=1;
		count++;
		sum+=dis[j];
		for(int k=1;k<=v;k++)
		{
			if(book1[k]==0&&dis[k]>step[j][k])
				dis[k]=step[j][k];
		}
	}
	return sum;
	 
}

int main(void)
{
	char str[105];
	int i,j,head,tail,stratx,straty,t,tx,ty;
	scanf("%d",&t);
	while(t--)
	{
		memset(e1,0,sizeof(e1));
		int k=1;
		scanf("%d%d",&m,&n);
		for(i=1;i<=n;i++)
		{
			gets(str);
			for(j=1;j<=m;j++)
			{
				scanf("%c",&e[i][j]);
				if(e[i][j]=='S'||e[i][j]=='A')
					e1[i][j]=k++;//给A和S进行标号
				step[i][j]=0;
			 } 
		}
		int v=k;
		for (i=1;i<=n;i++) 
		{
			for(j=1;j<=m;j++)
				if(e[i][j]=='A'||e[i][j]=='S')
					bfs(i,j);
		}
		printf("%d\n",prim(v-1));
	}
	return 0;
}
发布了53 篇原创文章 · 获赞 1 · 访问量 1352

猜你喜欢

转载自blog.csdn.net/fgets__/article/details/99588441
今日推荐