POJ - 3026 Borg Maze(BFS+最小生成树)

题目:

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

中文题意:从 S 出发,去到达每一个 A ,求最小的总路径长度,空格是空地,# 是墙,并且在走的过程中我们可以在 S 或 A 点分裂,也就是从该点可以延伸出多条路径到其他点,但是每一次只能让其中的一个继续行走。

分析:题目中说明我们可以在一个节点处进行分裂,但每次只能选出其中的一个点继续行走,这就意味着我们每次都可以选择最优路径进行行走,所需要的距离恰好是最小生成树的边权之和,这道题主要麻烦的地方有两点,第一个就是读入,需要提醒大家的一点就是,当我们读入x和y后可能在当前行还会读入一些多余的空格,不一定是一个,所以需要我们用一个gets额外处理一下,这个是一个大坑,卡了我好长时间,还有一个麻烦的地方就是,每两个点之间的距离都是未知的,需要我们用bfs去求解节点与节点之间的距离,注意我们不用对每一对节点之间都用一次bfs,我们对某个点跑一次bfs,那么这个点到其他所有的节点之间的距离就都已经知道了,这样下来有多少个节点就需要跑多少遍bfs,我在这个地方由于忽视了这一点,也tle了几发,最后我是用两种方法求的最小生成树,一个是prim另一个是kruscal,下面附上两种方法的代码:

prim版:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=205;
char s[N][N];
bool vis[N][N],v[N*N];
int x,y,cnt;//cnt记录生成树中节点个数 
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
struct point{
	int x,y;
}p[1000];
typedef pair<int,int> PII;
struct node{
	int x,y,dis;
};
int w[N][N],d[N*N],f[N][N];
void bfs(int px,int py)
{
	for(int i=1;i<=x;i++)
	for(int j=1;j<=y;j++)
		vis[i][j]=false,f[i][j]=0x3f3f3f3f;
	queue<node> l;
	f[px][py]=0;
	l.push({px,py,f[x][y]});
	while(!l.empty())
	{
		node t=l.front();
		l.pop();
		if(vis[t.x][t.y]) continue;
		vis[t.x][t.y]=true;
		for(int i=0;i<4;i++)
		{
			int tx=t.x+dx[i],ty=t.y+dy[i];
			if(tx<1||tx>x||ty<1||ty>y) continue;
			if(vis[tx][ty]||s[tx][ty]=='#') continue;
			f[tx][ty]=min(f[t.x][t.y]+1,f[tx][ty]);
			l.push({tx,ty,f[tx][ty]});
		}
	} 
}
int prim()
{
	for(int i=1;i<=cnt;i++)
		d[i]=0x3f3f3f3f,v[i]=false;
	d[1]=0;
	priority_queue<PII,vector<PII>,greater<PII> >q;
	q.push({0,1});
	int ans=0,count=0;
	while(q.size())
	{
		int t=q.top().second;
		q.pop();
		if(v[t]) continue;
		v[t]=true;
		ans+=d[t];
		count++;
		if(count==cnt) break;
		for(int i=1;i<=cnt;i++)
		{
			if(d[i]>w[t][i])
			{
				d[i]=w[t][i];
				q.push({d[i],i});
			}
		}
	}
	return ans;
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		scanf("%d%d",&y,&x);
		gets(s[0]);
		for(int i=1;i<=x;i++)
			gets(s[i]+1);
		cnt=0;
		for(int i=1;i<=x;i++)
		for(int j=1;j<=y;j++)
		{
			if(s[i][j]=='A'||s[i][j]=='S')
				p[++cnt]={i,j};
		}
		for(int i=1;i<cnt;i++)//求出来任何两个位置的最短距离
		{
			bfs(p[i].x,p[i].y);//一次bfs可以求出来该点到所有点的距离 
			for(int j=i+1;j<=cnt;j++)
				w[i][j]=w[j][i]=f[p[j].x][p[j].y];
		}
		printf("%d\n",prim());
	}
	return 0;
}

kruscal版:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=205;
char s[N][N];
bool vis[N][N];
int x,y;
int fu[N*N],f[N][N];
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int find(int x)
{
	if(fu[x]!=x) fu[x]=find(fu[x]);
	return fu[x];
}
struct point{
	int x,y;
}p[N*N];
struct node{
	int x,y,dis;
}q[N*N*N]; 
bool cmp(node a,node b)
{
	return a.dis<b.dis;
}
void bfs(int px,int py)
{
	for(int i=1;i<=x;i++)
	for(int j=1;j<=y;j++)
		vis[i][j]=false,f[i][j]=0x3f3f3f3f;
	queue<node> l;
	f[px][py]=0;
	l.push({px,py,f[x][y]});
	while(!l.empty())
	{
		node t=l.front();
		l.pop();
		if(vis[t.x][t.y]) continue;
		vis[t.x][t.y]=true;
		for(int i=0;i<4;i++)
		{
			int tx=t.x+dx[i],ty=t.y+dy[i];
			if(tx<1||tx>x||ty<1||ty>y) continue;
			if(vis[tx][ty]||s[tx][ty]=='#') continue;
			f[tx][ty]=min(f[t.x][t.y]+1,f[tx][ty]);
			l.push({tx,ty,f[tx][ty]});
		}
	} 
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		scanf("%d%d",&y,&x);
		gets(s[0]);
		for(int i=1;i<=x;i++)
			gets(s[i]+1);
		int cnt=0;
		for(int i=1;i<=x;i++)
		for(int j=1;j<=y;j++)
		{
			if(s[i][j]=='A'||s[i][j]=='S')
				p[++cnt]={i,j};
		}
		for(int i=1;i<=cnt;i++) fu[i]=i;
		int tt=0;
		for(int i=1;i<cnt;i++)//求出来任何两个位置的最短距离 
		{
			bfs(p[i].x,p[i].y);//一次bfs可以求出来该点到所有点的距离 
			for(int j=i+1;j<=cnt;j++)
				q[++tt]={i,j,f[p[j].x][p[j].y]};
		}
		sort(q+1,q+tt+1,cmp);
		int ans=0;
		int cnt1=0;
		for(int i=1;i<=tt;i++)
		{
			int f1=find(q[i].x),f2=find(q[i].y);
			if(f1==f2) continue;
			fu[f1]=f2;
			cnt1++;
			ans+=q[i].dis;
			if(cnt1==cnt-1) break;
		}
		printf("%d\n",ans);
	}
	return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值