当前位置: 首页 > 工具软件 > BORG > 使用案例 >

POJ3026 Borg Maze【BFS+最小生成树】

宗意蕴
2023-12-01

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代表出发点,从S点出发,每遇到一个A点便可分裂成多个。求把所有A都吃完需要多少步。

思路:很明显这道题要我求将图中所有A点和S点连接在一起的最小生成树,这里最小生成树可以直接使用prim()算法的模板,但是有一个难点是,如何知道所有字母点之间的距离呢?这里我先对所有字母点(S点和A点)使用了一遍BFS计算各点之间的距离,其中alpha[i][j]用于标记地图中坐标为(i, j)的点是否为字母以及当为字母点时该点的编号。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f

int x, y, num;
string *map;
int alpha[60][60];
int dist[110][110], vis[51][51], edge[110][110];
int dx[4] = { 0,0,-1,1 };
int dy[4] = { -1,1,0,0 };

void bfs(int ix, int jy)
{
	queue<pair<int, int>>q;
	pair<int, int>p;
	p.first = ix, p.second = jy;
	q.push(p);
	memset(vis, 0, sizeof(vis));
	vis[ix][jy] = 1;
	memset(dist, 0, sizeof(dist));
	while (!q.empty())
	{
		int tmpx = q.front().first, tmpy = q.front().second;
		q.pop();
		if (alpha[tmpx][tmpy])
		{
			edge[alpha[ix][jy]][alpha[tmpx][tmpy]] = dist[tmpx][tmpy];
		}
		for (int i = 0; i < 4; i++)
		{
			int nextx = tmpx + dx[i];
			int nexty = tmpy + dy[i];
			if (nextx < 0 || nexty < 0 || nextx >= y || nexty >= x)
				continue;
			if (vis[nextx][nexty] || map[nextx][nexty] == '#')
				continue;
			vis[nextx][nexty] = true;
			dist[nextx][nexty] = dist[tmpx][tmpy] + 1;
			p.first = nextx, p.second = nexty;
			q.push(p);
		}
	}
}

int prim()
{
	int dis[110];
	int book[110];
	int sum = 0;
	memset(book, 0, sizeof(book));
	for (int i = 1; i <= num; i++)
		dis[i] = inf;
	dis[1] = 0;
	for (int i = 1; i <= num; i++)
	{
		int u = -1, MIN = inf;
		for (int j = 1; j <= num; j++)
		{
			if (book[j] == 0 && dis[j] < MIN)
			{
				MIN = dis[j];
				u = j;
			}
		}
		if (u == -1)break;
		book[u] = 1;
		sum += dis[u];
		for (int v = 1; v <= num; v++)
		{
			if (book[v] == 0 && edge[u][v] != 0 && edge[u][v] < dis[v])
				dis[v] = edge[u][v];
		}
	}
	return sum;
}

int main()
{
	int n;
	scanf("%d", &n);
	while (n--)
	{
		memset(alpha, 0, sizeof(alpha));
		scanf("%d%d", &x, &y);
		num = 0;
		string a;
		getline(cin, a);

		map = new string[y];
		for (int i = 0; i < y; i++)
		{
			getline(cin, map[i]);
			for (int j = 0; j < x; j++)
			{
				if (map[i][j] == 'S' || map[i][j] == 'A')
					alpha[i][j] = ++num;
			}
		}
		for (int i = 0; i < y; i++)
		{
			for (int j = 0; j < x; j++)
				if (alpha[i][j])
					bfs(i, j);
		}
		printf("%d\n", prim());
	}
	return 0;
}

 

 类似资料: