POJ 3026 Borg Maze(bfs+prim)

本文介绍了一种用于帮助Borg集体估算在四方向移动下寻找并同化隐藏于迷宫中外星人的最小成本的算法。通过使用广度优先搜索(BFS)构建迷宫中各点间的距离图,并应用Prim算法找出连接所有外星人的最短路径,实现了对复杂迷宫环境的有效探索。

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 lette"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

题意描述:
给出一个n*m的矩阵‘#’代表墙,‘S’起始位置,‘A’代表外星人,在找到一个外星人后,他能被同化也会去找其它的外星人,现在问把所有的外星人全部找完最少需要都少步。

在对A和S标号完毕后,就可以直接去用bfs来构建这个图了,无需在找到S。

解题思路:
先用bfs去搜每个A到S的距离和A到A之间的距离用一个二维数组取构建一个S与A的图,然后再用prim去找出最短的路。

#include<stdio.h>
#include<string.h> 
 
 struct node 
 {
 	int x,y,s;
 }p[2505];
 
char e[55][55];
int next[4][2]={1,0, -1,0, 0,1, 0,-1};
int e1[55][55],step[105][105],m,n;

void bfs(int x,int y)
{
	int book[55][55]; 
	int stratx=x,straty=y;
	int head,tail,k,tx,ty;
	head=1;
	tail=1;
	p[tail].x=stratx;
	p[tail].y=straty;
	p[tail].s=0;
	
	memset(book, 0, sizeof(book));
	book[stratx][straty]=1;
	tail++;
	while(head<tail)
	{
		for(k=0;k<4;k++)
		{
			tx=p[head].x+next[k][0];
			ty=p[head].y+next[k][1];
				
			if(tx<1||ty<1||tx>n||ty>m)
				continue;
						
			if(e[tx][ty]!='#'&&book[tx][ty]==0)
			{
				book[tx][ty]=1;
				p[tail].x=tx;
				p[tail].y=ty;
				p[tail].s=p[head].s+1;
				if(e[tx][ty]=='A'||e[tx][ty]=='S')
					step[e1[tx][ty]][e1[stratx][straty]]=step[e1[stratx][straty]][e1[tx][ty]]=p[tail].s;//构建A与S的二维图
				tail++;
			}
		}
		head++;
	}
}

int prim(int v)
{
	int i,j,count=1,sum=0;
	int min,inf=99999999;
	int dis[2505],book1[2505];
	memset(book1,0,sizeof(book1));
	for(i=1;i<=v;i++)
		dis[i]=step[1][i];	
	book1[1]=1;
	while(count<v)
	{
		min=inf;
		for(i=1;i<=v;i++)
		{
			if(book1[i]==0&&dis[i]<min)
			{
				min=dis[i];
				j=i;
			}
		}
		book1[j]=1;
		count++;
		sum+=dis[j];
		for(int k=1;k<=v;k++)
		{
			if(book1[k]==0&&dis[k]>step[j][k])
				dis[k]=step[j][k];
		}
	}
	return sum;
	 
}

int main(void)
{
	char str[105];
	int i,j,head,tail,stratx,straty,t,tx,ty;
	scanf("%d",&t);
	while(t--)
	{
		memset(e1,0,sizeof(e1));
		int k=1;
		scanf("%d%d",&m,&n);
		for(i=1;i<=n;i++)
		{
			gets(str);
			for(j=1;j<=m;j++)
			{
				scanf("%c",&e[i][j]);
				if(e[i][j]=='S'||e[i][j]=='A')
					e1[i][j]=k++;//给A和S进行标号
				step[i][j]=0;
			 } 
		}
		int v=k;
		for (i=1;i<=n;i++) 
		{
			for(j=1;j<=m;j++)
				if(e[i][j]=='A'||e[i][j]=='S')
					bfs(i,j);
		}
		printf("%d\n",prim(v-1));
	}
	return 0;
}
内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值