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.

输入:

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 letterS’’ 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.

输出:

For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.

样例输入:

2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  

样例输出:

8
11

很显然,如果想要路径最小,那么尽可能的将他们聚合到一起,走同一个路径
转化成最小生成树问题
利用广搜+prim算法可以实现
代码如下:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define inf 9999999
using namespace std;
typedef pair<int,int> P;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int n,m;
int cnt;
int xx[3000];
int yy[3000];
int a[100][100];
int dis[3000][3000];
int d[100][100];
int mincost[100];
int used[10000];
void bfs(int u)
{
	queue<P> que;
	memset(d,-1,sizeof(d));
	que.push(P(xx[u],yy[u]));
	d[xx[u]][yy[u]]=0;
	while(!que.empty())
	{
		P p=que.front();
		que.pop();
		for(int i=0;i<4;i++)
		{
			int nx=dx[i]+p.first;
			int ny=dy[i]+p.second;
			if(nx>=0&&nx<n&&ny>=0&&ny<m&&d[nx][ny]==-1&&a[nx][ny]==1)
			{
				que.push(P(nx,ny));
				d[nx][ny]=d[p.first][p.second]+1;
			}
		}
	}
	for(int i=0;i<cnt;i++)
	{
		dis[u][i]=d[xx[i]][yy[i]];
	}
}
int prim()
{
	fill(mincost,mincost+cnt,inf);
	memset(used,0,sizeof(used));
	mincost[0]=0;
	int res=0;
	while(1)
	{
		int v=-1;
		for(int i=0;i<cnt;i++)
		{
			if(!used[i]&&(v==-1||mincost[i]<mincost[v]))
			{
				v=i;
			}
		}
		if(v==-1)break;
		res+=mincost[v];
		used[v]=1;
		for(int i=0;i<cnt;i++)
		{
			mincost[i]=min(mincost[i],dis[v][i]);
		}
	}
	printf("%d\n",res);
}
int main()
{
	int ttt;
	scanf("%d",&ttt);
	while(ttt--)
	{
		scanf("%d%d",&m,&n);
		char bb;
		cnt=0;
		while(bb=getchar())
			if(bb=='\n')break;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				scanf("%c",&bb);
				if(bb=='A'||bb=='S')
				{
					xx[cnt]=i;
					yy[cnt++]=j;
				}
				if(bb=='#')a[i][j]=2;
				else a[i][j]=1; 
			}
			getchar();
		}
		for(int i=0;i<cnt;i++)
		{
			bfs(i);
		}
		prim();
	}
	return 0;
}

额,看着是很长,其实其中有不少的行数被用来声明了
基本上都是模板,应该不难理解

上一篇:[转帖]Borg论文解读


下一篇:manifest解析