POJ3026--Prim算法--Borg Maze

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
6 5              
######
#A#A##
# # A#
#S  ##
######

空格位于"6 5              ",得丢弃

/*
地图50*50
思路:首先用广搜确定各个顶点到其他顶点的最短路.用dis[][]数组来存
用cost[]数组来存到每个顶点的最短距离。初始化inf
每一次从未标记的点中选择一个距离最短的点。(第一次可以随便。不过第一次选定那个点距离就是0)
然后用该点去更新所有未标记的点的距离。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
#define inf 0x3f3f3f3f
char map[58][58];
int dis[58][58];
int diszm[108][108];
int cost[108];
bool vis[108];
int heng[]={0,0,1,-1};
int zong[]={1,-1,0,0};
int r,c,num;
inline int min(int a,int b)
{
	return a>b?b:a;
}
struct Node
{
	int rr,cc,key;
}node[108];
void bfs(int start)//这个bfs的作用就是求每个字母到其他字母的最短路径
{
	queue <int> qr;
	queue <int> qc;
	memset(dis,0x3f,sizeof(dis));
	int r1=node[start].rr,c1=node[start].cc;
	dis[r1][c1]=0;
	qr.push(r1),qc.push(c1);
	while(!qr.empty())
	{
		int r_=qr.front(),c_=qc.front();
		qr.pop(),qc.pop();
		for(int i=0;i<4;i++)
		{
			int rr_=r_+heng[i],cc_=c_+zong[i];
			if(((rr_>=1&&rr_<=r)&&(cc_>=1&&cc_<=c))&&(map[rr_][cc_]!='#'))//这个点不越界而且可以走
			{
				if(dis[r_][c_]+1<dis[rr_][cc_])
				{
					qr.push(rr_),qc.push(cc_);
					dis[rr_][cc_]=dis[r_][c_]+1;
				}
			}
		}
	}
	for(int i=1;i<=num;i++)
	{
		diszm[start][i]=dis[node[i].rr][node[i].cc];
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	char str[100];
	while(t--)
	{
		num=0;//num用来记录有多少个个节点
		scanf("%d%d",&c,&r);
		char fuck=getchar();
		while(fuck!='\n')
		{
			fuck=getchar();
		}
		int k=0;
		int sr,sc;
		for(int i=1;i<=r;i++)
		{
			for(int j=1;j<=c;j++)
			{
				map[i][j]=getchar();
				if(map[i][j]=='A'||map[i][j]=='S')
				{
					node[++num].rr=i;
					node[num].cc=j;
					if(map[i][j]=='S')
					{
						sr=i;sc=j;
					}
				}
			}
			getchar();
		}
		for(int i=1;i<=num;i++)
		{
			bfs(i);
		}
		int len=0;
		memset(cost,0x3f,sizeof(cost));
		memset(vis,0,sizeof(vis));
		cost[1]=0;
		for(int i=1;i<=num;i++)
		{
			int x,minlen=inf;
			for(int j=1;j<=num;j++)
			{
				if(!vis[j])
				{
					minlen=cost[j]<minlen?cost[x=j]:minlen;
				}
			}
			len+=cost[x];
			vis[x]=1;
			for(int j=1;j<=num;j++)
			{
				cost[j]=min(cost[j],diszm[x][j]);
			}
		}
		printf("%d\n",len);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值