POJ 3026 Borg Maze BFS和prim

2 篇文章 0 订阅
                             Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10547 Accepted: 3495

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

Source

 

题意: 给出 一个m*n的迷宫,空格代表路径,#代表墙,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。重复走过的路不再计算。

解题思路:由于求从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度而且重复走过的路不再计算。觉得应该使用prim算法求在最小生成树,这样无论哪个点做起点都是一样的,复走过的路就不再计算,(通常选取第一个点),因此起点不是S也没有关系,所以把所有的A和S都可以一视同仁,看成一模一样的顶点就可以了,然后利用bfs求取任意两点的距离.

 
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
using namespace std;
#define INF 0x7fffffff
int a[1001][1001];
int b[1001][1001];
int mark[1001][1001];
char vis[1001][1001];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int h,l;
struct note{
	int x,y,step;
	friend bool operator<(note a1,note a2)//要求求最短距离,用其对代码优化
	{
		return a1.step>a2.step;
	}
}s[1000],k1,k2;
int fun(struct note k2)//判断是否能往下走
{
	if(k2.x<1||k2.x>h||k2.y<1||k2.y>l||vis[k2.x][k2.y]=='#'||mark[k2.x][k2.y])
	return 0;
	return 1;
}
void bfs(int qh,int ql)//利用bfs求一点到其余点的距离
{
	k1.x=qh;
	k1.y=ql;
	k1.step=0;
	memset(mark,0,sizeof(mark));
	mark[qh][ql]=1;//标记访问过的点
	priority_queue<note>q;
	q.push(k1);
	while(!q.empty())
	{
		k1=q.top();
		q.pop();
		if(vis[k1.x][k1.y]=='A'||vis[k1.x][k1.y]=='S')
		  a[b[qh][ql]][b[k1.x][k1.y]]=k1.step;
		for(int i=0;i<4;i++)
		{
			k2.x=k1.x+dx[i];
			k2.y=k1.y+dy[i];
			k2.step=k1.step+1;
			if(fun(k2))
			{
				q.push(k2);
				mark[k2.x][k2.y]=1;
			}
		}
	}
}
void prim(int k)//利用prim求取最短路
{
	int i,j,z,max;
	int b[1000],map[1000],min;
	for(i=1;i<=k;i++)
	{
		b[i]=a[1][i];
	}
	memset(map,0,sizeof(map));
	map[1]=1;
	max=0;
	for(i=1;i<k;i++)
	{
		min=INF;
		for(j=1;j<=k;j++)
		{
			if(!map[j]&&min>b[j])
			{
				z=j;
				min=b[j];
			}
		}
		if(min==INF)
		break;
		max+=min;
		map[z]=1;
		for(j=1;j<=k;j++)
		{
			if(!map[j]&&b[j]>a[z][j])
			b[j]=a[z][j];
		}
	}
	printf("%d\n",max);
}
int main()
{
	int n,k,i,j;
	char ch[101];
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&l,&h);
		k=0;
		gets(ch);//用其吃掉换行符
		for(i=1;i<=h;i++)
		{
			getchar();
			for(j=1;j<=l;j++)
			{
				scanf("%c",&vis[i][j]);
				if(vis[i][j]=='A'||vis[i][j]=='S')
				{
					k++;
					b[i][j]=k;//记录有多少A.S点,以及它们的具体位置
					
				}
			}
		}
		for(i=1;i<=k;i++)
		{
			for(j=1;j<=k;j++)
			{
				if(j==i)a[i][j]=0;
				else a[i][j]=INF;
			}
		}
		for(i=1;i<=h;i++)
		{
			for(j=1;j<=l;j++)
			{
				if(vis[i][j]=='A'||vis[i][j]=='S')
				bfs(i,j);
			}
		}
		prim(k);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值