现在的位置: 首页 > 综合 > 正文

POJ 3026 Borg Maze BFS + Prim

2014年01月21日 ⁄ 综合 ⁄ 共 3247字 ⁄ 字号 评论关闭
Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4808   Accepted: 1617

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

 

恶心的题目。

题意: 给出 一个m*n的迷宫,空格代表路径,#代表墙, 现在求 从S到达所有A的最短路径和, 重复的路径不计算;

思路: 将 所有A和S 看做 节点, 则可以构成一个 包含N个节点的无向完全连通图, 权值 为 字母到其他字母的 距离;BFS求出字母到字母的距离;

则 问题可看做 求 改图的最小生成树;利用Prim求解即可;

 

注意 数据有bug, m*n后 有空格 所以 不能用getchar();

 

#include <iostream>
#include <queue>
using namespace std;
const int M = 120;
int x[4]={0,0,1,-1};
int y[4]={1,-1,0,0};
int dis[M][M],low[M],node[M][M];
char map[M][M];
bool visit[M][M];
int n,m,node_sum;
struct Node
{
	int x,y,dis;
};
void BFS(int i, int j)
{
	memset(visit, false, sizeof(visit));
	visit[i][j]=true;
	Node b = {i, j, 0};
	queue<Node> N;
	N.push(b);
	while(!N.empty())
	{
		Node front = N.front();
		if(node[ front.x ][ front.y ])
			dis[ node[b.x][b.y] ][ node[front.x][front.y] ] = front.dis;
		N.pop();
		for(i=0; i<4; i++)
		{
			int xx = front.x+x[i];
			int yy = front.y+y[i];
			if(xx>=1&&xx<=n && yy>=1&&yy<=m)
			if(map[xx][yy]!='#' && !visit[xx][yy])
			{
				Node t = {xx, yy, front.dis+1};
				N.push(t);
				visit[xx][yy] = true;
			}
		}
	}
}
int Prim()
{
	int s=1,i,count=1,prim_sum=0,t;
	bool flag[M]={false};
	flag[s] = true;
	for(i=1; i<=node_sum; i++)
		low[i] = 99999;
	while(count < node_sum)
	{
		int min_dis = 99999;
		for(i=1; i<=node_sum; i++)
		{
			if(!flag[i] && low[i]>dis[s][i])
				low[i] = dis[s][i];
			if(!flag[i] && low[i]<min_dis)
			{
				min_dis = low[i];
				t = i;
			}
		}
		s = t; count++;
		flag[s] = true;
		prim_sum += min_dis;
	}
	return prim_sum;
}
int main()
{
	int t,i,j;
	char temp[M];
	scanf("%d", &t);
	while(t--)
	{
		memset(map, '#', sizeof(map));
		memset(dis, 0, sizeof(dis));
		memset(node, 0,sizeof(node));
		node_sum = 0;
		scanf("%d %d", &m, &n);
		gets(temp);
		for(i=1; i<=n; i++)
		{
			gets(map[i]);
			for(j=1; j<=m; j++)
			{
				if(map[i][j]=='A' || map[i][j]=='S')
					node[i][j]=++node_sum;
			}
		}
		for(i=1; i<=n; i++)
		for(j=1; j<=m; j++)
		{
			if(node[i][j])
				BFS(i,j);
		}
		printf("%d\n", Prim());
	}
	return 0;
}

 

抱歉!评论已关闭.