POJ - 3026 Borg Maze (BFS + Minimum Spanning Tree)

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

The meaning of the question : In a maze with M rows and N columns (see the example below, don't do it the other way around), '#' means that the wall cannot be walked, and the others can be walked. There are also two English letters A and S, now from S Starting, it is required to connect all letters A with the shortest path L, and output the total length of this path L.

Idea : First number all points A and S, use BFS to process the shortest path length from each point in A and S to all remaining points, then use A and S as points, and the shortest path length from A to S as the edge weight to establish A new graph, and finally finding the minimum spanning tree for this new graph is the answer.

The question is very pitiful, there are not only 50 points, but almost 100 points; there may be many spaces after the input, so add a \n after the input. What I wrote was a little troublesome, but what God wrote is relatively simple. Continue to improve in the future

#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;
						}
					}
				}
			}
		}
	}
}
intmain()
{
	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);
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324698127&siteId=291194637