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

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

题意:在一个M行N列的迷宫中(看下样例别弄反了),‘#’表示墙不可以走,其他都可以走,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母A,输出这条路径L的总长度。

思路先把所有A,S点编号,用BFS处理出来A,S中每一个点到剩余所有点的最短路径长度,然后以A,S为点,A到S的最短路径长度为边权建立一个新图,最后对于这个新图求最小生成树就是答案。

题很坑,不止有50个点,差不多有100个点;输入后面可能还有好多空格所以在输入后边加个\n。我写的有点麻烦,大神写的都比较简单。以后继续改进

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
char q[100][100];
int flag[100][100];
int Father[10000];
int n,m,o,u; 
struct Node
{
	int x,y,z;
}s[10000];
struct node
{
	int x1,y1,steps;
}Now,Next;
struct Note
{
	int x2,y2,w;
}sx[10000];
bool compare(Note x,Note y)
{
	return x.w < y.w;
}
int Find(int x)
{
	if(x != Father[x])
	{
		Father[x] = Find(Father[x]);
	}
	return Father[x];
}
void Kruskal(int ans)
{
	int u = 0;
	int cnt = 0;
	for(int i = 0 ; i < o ; i++)
	{
		if(Find(sx[i].x2) != Find(sx[i].y2))
		{
			Father[Find(sx[i].x2)] = Find(sx[i].y2);
			cnt += sx[i].w;
			u++;
			if(u == ans)
			{
				printf("%d\n",cnt);
				break;
			}
		}
	}	
}
void bfs(int x,int y,int z)
{
	memset(flag,0,sizeof(flag));
	Now.x1 = x,Now.y1 = y,Now.steps = 0;
	flag[Now.x1][Now.y1] = 1;
	int p = z;
	queue<node> ss;
	ss.push(Now);
	while(!ss.empty())
	{
		Now = ss.front();
		ss.pop();
		for(int i = 0 ; i < 4 ; i++)
		{
			int X = dir[i][0] + Now.x1;
			int Y = dir[i][1] + Now.y1;
			if(X >= 0 && Y >= 0 && X < n && Y < m && q[X][Y] != '#' && flag[X][Y] == 0)
			{
				flag[X][Y] = 1;
				Next.x1 = X,Next.y1 = Y,Next.steps = Now.steps + 1;
				ss.push(Next);
				if(q[Next.x1][Next.y1] == 'A' || q[Next.x1][Next.y1] == 'S')
				{
					for(int i = 0 ; i < u ; i++)
					{
						if(Next.x1 == s[i].x && Next.y1 == s[i].y)
						{
							sx[o].x2 = p;
							sx[o].y2 = s[i].z;
							sx[o++].w = Next.steps;
						}
					}
				}
			}
		}
	}
}
int main()
{
	int k;
	scanf("%d",&k);
	while(k--)
	{
		u = 0;
		scanf("%d%d\n",&n,&m);
		int num = 1;
		for(int i = 0 ; i < m ; i++)
		{
			gets(q[i]);
			for(int j = 0 ; j < n ; j++)
			{
				if(q[i][j] == 'S' || q[i][j] == 'A')
				{
					s[u].x = i;
					s[u].y = j;
					s[u++].z = num++;
				}
			}
		}
		for(int i = 0 ; i <= num ; i++)
		{
			Father[i] = i;
		}
		o = 0;
		for(int i = 0 ; i < u ; i++)
		{
			bfs(s[i].x,s[i].y,s[i].z);
		}
		sort(sx,sx + o,compare);
		Kruskal(u - 1);
	}
}

猜你喜欢

转载自blog.csdn.net/MySweetFamily/article/details/80051722