POJ - 3026 Borg Maze【BFS+prim】

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14533 Accepted: 4728

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
题意:

在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。遇到S or A才能分路走,其他情况下不能随意分,S和A的作用是一样的,以下把S当成A看;

思路:在A处才能分路走,计算每一个A到其余A处的距离,生成mapp图;现在将所有的A相连,相当于最小生成树,由于点少边比较多,用prim解决;最重要的就是BFS的预处理,空格可以行走,用BFS生成mapp图,再用prim计算这个MST的最短长度

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#define max_n 110
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
char str[max_n][max_n];
int mapp[max_n][max_n],vis[max_n][max_n],pos[max_n][max_n];
int dis[max_n],mst[max_n];
int move[4][2]={1,0,-1,0,0,1,0,-1};
int t,n,m,top;

struct node
{
	int x;
	int y;
	int step;
};

void cal(int x,int y)
{
	memset(vis,0,sizeof(vis));
	queue<node> Q;
	node now,next;
	now.x=x;
	now.y=y;
	now.step=0;
	vis[now.x][now.y]=1;
	Q.push(now);
	while(!Q.empty())
	{
		next=Q.front();
		Q.pop();
		if(str[next.x][next.y]=='A' || str[next.x][next.y]=='S')
			mapp[pos[x][y]][pos[next.x][next.y]]=mapp[pos[next.x][next.y]][pos[x][y]]=next.step;
		for(int i=0;i<4;i++)
		{
			now.x=next.x+move[i][0];
			now.y=next.y+move[i][1];
			if(now.x>=0 && now.x<m && now.y>=0 && now.y<n && str[now.x][now.y]!='#' && !vis[now.x][now.y])
			{
				vis[now.x][now.y]=1;
				now.step=next.step+1;
				Q.push(now);
			}
		}	
	}
}

int prim(int x)
{
	memset(mst,0,sizeof(mst));
	for(int i=1;i<top;i++)
	{
		dis[i]=mapp[1][i];
	}
	int ans=0;
	mst[x]=1;
	for(int i=2;i<top;i++)
	{
		int minn=INF,k=i;
		for(int j=1;j<top;j++)
		{
			if(!mst[j] && dis[j]<minn)
			{
				minn=dis[j];
				k=j;
			}
		}
		ans+=minn;
		mst[k]=1;
		for(int j=1;j<top;j++)
		{
			if(!mst[j] && dis[j]>mapp[k][j])
			{
				dis[j]=mapp[k][j];
			}
		}
	}
	return ans;
}

int main()
{
	scanf("%d",&t);
	getchar();
	while(t--)
	{
		top=1;
		scanf("%d %d\n",&n,&m);//这是正确的格式 
	//	scanf("%d %d",&n,&m); getchar(); 这个竟然错了,好玄学哦; 
		for(int i=0;i<m;i++)
		{
			gets(str[i]);
			for(int j=0;j<n;j++)
			{
				if(str[i][j]=='S' || str[i][j]=='A')
				{
					pos[i][j]=top;
					top+=1;
				}
			}
		}
		for(int i=0;i<=top;i++)
		{
			for(int j=0;j<=top;j++)
			{
				if(i==j) mapp[i][j]=0;
				else mapp[i][j]=INF;
			}
		}
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(str[i][j]=='S' || str[i][j]=='A')
					cal(i,j);
			}
		}
		printf("%d\n",prim(1));
	}
	return 0;
 } 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值