POJ 3026 Borg Maze BFS + Prim

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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值