POJ 3026 Borg Maze(最小生成树--prime+BFS)

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10543   Accepted: 3494

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


题目主要是求将A和S连在一起的最短路径,稍微困难的就是距离了,
其实用一个BFS就可以解决了,用BFS求出各点之间的距离,然后就可
以prime了。不过坑点还是在输入那里,详情看代码。

ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<iostream>
#include<algorithm>
#define MAXN 10100
#define INF 0x7fffffff
#define max(a,b) a>b?a:b
#define min(a,b) a>b?b:a
using namespace std;
struct s
{
	int x;
	int y;
	int step;
	int id;
}p[MAXN],a,b;
char map[55][55];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int pri[MAXN][MAXN];
int v[55][55];
int v1[MAXN];
int dis[MAXN];
int n,m,k;
int sum;
int check(int xx,int yy)
{
	if(xx<0||xx>=m||yy<0||yy>=n||map[xx][yy]=='#')
	return 0;
	if(v[xx][yy])
	return 0;
	return 1;
}
void bfs(int aa,int bb,int num)
{
	int i;
	memset(v,0,sizeof(v));
	queue<s>q;
	a.x=aa;
	a.y=bb;
	a.step=0;
	v[aa][bb]=1;
	q.push(a);
	while(!q.empty())
	{
		a=q.front();
		q.pop();
		for(i=1;i<k;i++)//如果此点和记录的点相同,记录路径
		{
			if(p[i].x==a.x&&p[i].y==a.y)
			{
				pri[num][p[i].id]=pri[p[i].id][num]=a.step;
			}
		}
		for(i=0;i<4;i++)
		{
			b.x=a.x+dir[i][0];
			b.y=a.y+dir[i][1];
			if(check(b.x,b.y))
			{
				b.step=a.step+1;
				v[b.x][b.y]=1;
				q.push(b);
			}
		}
	}
}
void prime()
{
	int i,e,j;
	int M;
	memset(v1,0,sizeof(v1));
	for(i=1;i<k;i++)
	dis[i]=pri[1][i];
	v1[1]=1;
	sum=0;
	for(i=1;i<k;i++)
	{
		M=INF;
		for(j=1;j<k;j++)
		{
			if(v1[j]==0&&dis[j]<M)
			{
				M=dis[j];
				e=j;
			}
		}
		if(M==INF)
		break;
		v1[e]=1;
		sum+=M;
		for(j=1;j<k;j++)
		{
			if(v1[j]==0)
			dis[j]=min(dis[j],pri[e][j]);
		}
	}
	printf("%d\n",sum);
}
int main()
{
	int t,i,j;
	char ch[200];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		gets(ch);  //没错,就是这里,看了discuss里面,说后面可能会有空格.....
		k=1;
        for(i=0;i<m;i++)  
        {  
            gets(map[i]);
        }
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)  
            {  
               if(map[i][j]=='A'||map[i][j]=='S')  
               {  
                   p[k].x=i;  //记录
                   p[k].y=j;
                   p[k].id=k;
                   k++;
                } 
            }   
        }  
        for(i=1;i<k;i++)//从各个点开始
        {
        	int bx=p[i].x;
        	int by=p[i].y;
        	bfs(bx,by,i);
		}
	    prime();
	}
	return 0;
}


你可能感兴趣的:(POJ 3026 Borg Maze(最小生成树--prime+BFS))