POJ 3026 Borg Maze

本文介绍了一个迷宫搜索问题,通过BFS算法计算起点到各目标点的距离,并利用Prim算法找到总路径长度最短的搜索方案。适用于算法竞赛及路径规划问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9073 Accepted: 3020

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

这道题的意思就是找S到每个A的距离的最小和,先用BFS找出每个点到其他点的距离,再用Prim求出最小距离

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>

using namespace std;

const int INF=0x3f3f3f3f;
char Map[110][110];
int vis[110][110];//标记数组
int dis[110][110];//记录两个点之间的距离
int point[110][110];//记录点的坐标
int d[110];//记录某个点到其他点的距离
int sum;//记录点的数量
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

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

queue<node>q;

void BFS(int a,int b)
{
	memset(vis,0,sizeof(vis));//每取一个点都要遍历其他所有点,因此必须清理vis数组,
	vis[a][b]=1;
	node s,t,r;
	s.x=a;
	s.y=b;
	s.step=0;
	q.push(s);
	while(!q.empty())
	{
		node t=q.front();
		q.pop();
		if(point[t.x][t.y])
		{
			dis[point[s.x][s.y]][point[t.x][t.y]]=t.step;
			//cout<<dis[point[s.x][s.y]][point[t.x][t.y]]<<" " ;
		}
		for(int i=0;i<4;i++)
		{
			r.x=t.x+dx[i];
			r.y=t.y+dy[i];
			r.step=t.step+1;
			if(Map[r.x][r.y]!='#'&&!vis[r.x][r.y])
			{
				q.push(r);
				vis[r.x][r.y]=1;
			}
		}
	}
	//cout<<endl;

}

void Prim()
{
	int pos,Min;
	int ans=0;
	int v[110];
	memset(v,0,sizeof(v));
	memset(d,INF,sizeof(d));
	v[1]=1;
	//cout<<sum<<endl;
	for(int i=2;i<=sum;i++)
	{
		d[i]=dis[1][i];
		//cout<<d[i]<<endl;
	}
	for(int i=1;i<sum;i++)
	{
		Min=INF;
		for(int j=2;j<=sum;j++)
		{
			if(!v[j]&&Min>d[j])
			{
				pos=j;
				Min=d[j];
			}
		}
		v[pos]=1;
		ans+=Min;
		for(int j=2;j<=sum;j++)
		{
			if(!v[j]&&d[j]>dis[pos][j])
			{
				d[j]=dis[pos][j];
			}
		}
	}
	printf("%d\n",ans);
}

int main()
{
	int T;
	int a,b;
	char str[110];
	scanf("%d",&T);
	while(T--)
	{
		sum=0;
		scanf("%d%d",&a,&b);
		memset(Map,'#',sizeof(Map));
		memset(vis,0,sizeof(vis));
		memset(dis,0,sizeof(dis));
		memset(point,0,sizeof(point));//开始忘记清理point数组,导致第二组数据一直不对
		memset(d,0,sizeof(d));
		while(!q.empty())
		{
			q.pop();
		}
		gets(str);
		for(int i=0;i<b;i++)
		{
			gets(Map[i]);//输入数据中有空格,要用gets
			for(int j=0;j<a;j++)
			{
				if(Map[i][j]=='A'||Map[i][j]=='S')
				{
					point[i][j]=++sum;//由于后面要用point数组来判断点,因此sum最好从1开始
					//cout<<sum<<endl;
					//sum++;
					//point[sum].x=i;
					//point[sum].y=j;
					//point[sum].flag=1;
					//sum++;
				}
			}
		}
		for(int i=0;i<b;i++)
		{
			for(int j=0;j<a;j++)
			{
				if(point[i][j])//取每一个点,找其到其他点的距离
				{
					BFS(i,j);
				}
			}
			//BFS(i);
		}
		Prim();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值