POJ 3026:Borg Maze:BFS+prim

本文介绍了一个迷宫搜索问题,通过BFS搜索算法确定各节点间距离,并利用最小生成树算法找到扫描迷宫的最短路径总和。特别提到了在实现过程中需要注意的一些细节,如输入数据的特殊格式。

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

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8086 Accepted: 2703

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

题目很好,以二维迷宫的方式给出各节点的关系,从S出发时刻和遇到A时刻,都可以分叉出多个继续搜索,求解最少的路径和。很明显,使用BFS搜索迷宫,找出每两个节点之间需要的距离,就进入无向图完全图的最小生成树求解方式,非常清晰的思路。

但是,对于不太会STL的人来讲,手写BFS需要手写队列,那么队列的大小定义是个问题,当然也可以使用循环队列。这就为该题目的坑爹做了很好的掩护。题目的坑爹之处在于:在输入用例中,x,y后面有不同数量的空格!直接使得下面的读取图数据混乱。由于途中也有空格表示可以行走的地方,因此,读取数据的时候不可以使用scanf(%s),如果你使用scanf(%c)就会发现:图的后面也有空格,导致整个图输入混乱!必须使用gets(),然后在x,y后面也要使用gets()来读取坑爹的空格。

就是因为没有空格,使得一直RTE,我还以为是因为我的队列设置不够,一直增大队列,还使用循环队列。不知道出题人的数据怎么给的,那么大的坑,要是不去讨论区,打死看不出来问题~
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 100000000
int n,tu[105][105],a[105][105],x,y,num;
int usetu[105][105],use[105],d[105];
char ts[205];
typedef struct node
{
	int tx,ty;
	int ceng;
}node;
node duilie[1000];
typedef struct nnode
{
	int ttx,tty;
}nnode;
nnode zuobiao[205];
int head,end;
void init()
{
	int i,j;
	int k=2;
	scanf("%d%d",&x,&y);
	gets(ts);
	for(i=0;i<y;i++)
	{
		gets(ts);
		for(j=0;j<x;j++)
		{
			switch(ts[j])
			{
			case '#':tu[i][j]=-1;break;
			case ' ':tu[i][j]=0;break;
			case 'S':tu[i][j]=1;
				zuobiao[1].ttx=i;
				zuobiao[1].tty=j;break;
			case 'A':tu[i][j]=k;
				zuobiao[k].ttx=i;
				zuobiao[k].tty=j;
				k++;break;
			default:break;
			};
		}
	}
	num=k-1;
	for(i=1;i<=num;i++)
		for(j=1;j<=num;j++)
			a[i][j]=maxn;
	memset(a,0,sizeof(a));
}
void bfs(int hao,int beginx,int beginy)
{
	int i,j;
	int tceng,temx,temy;
	int temhao;
	duilie[0].ceng=0;
	duilie[0].tx=beginx;
	duilie[0].ty=beginy;
	head=0;
	end=1;
	usetu[beginx][beginy]=1;
	memset(usetu,0,sizeof(usetu));
	while(end!=head)
	{
		tceng=duilie[head].ceng;
		temx=duilie[head].tx;
		temy=duilie[head].ty;
		head=(head+1)%1000;
		if(tu[temx][temy]>hao)
		{
			temhao=tu[temx][temy];
			a[hao][temhao]=a[temhao][hao]=tceng;
		}
		if(tu[temx-1][temy]!=-1&&!usetu[temx-1][temy])
		{
			duilie[end].ceng=tceng+1;
			duilie[end].tx=temx-1;
			duilie[end].ty=temy;
			end=(end+1)%1000;
			usetu[temx-1][temy]=1;
		}
		if(tu[temx+1][temy]!=-1&&!usetu[temx+1][temy])
		{
			duilie[end].ceng=tceng+1;
			duilie[end].tx=temx+1;
			duilie[end].ty=temy;
			end=(end+1)%1000;
			usetu[temx+1][temy]=1;
		}
		if(tu[temx][temy-1]!=-1&&!usetu[temx][temy-1])
		{
			duilie[end].ceng=tceng+1;
			duilie[end].tx=temx;
			duilie[end].ty=temy-1;
			end=(end+1)%1000;
			usetu[temx][temy-1]=1;
		}
		if(tu[temx][temy+1]!=-1&&!usetu[temx][temy+1])
		{
			duilie[end].ceng=tceng+1;
			duilie[end].tx=temx;
			duilie[end].ty=temy+1;
			end=(end+1)%1000;
			usetu[temx][temy+1]=1;
		}
	}
}
void prim()
{
	int i,j;
	int minc,mind;
	memset(use,0,sizeof(use));
	memset(d,0,sizeof(d));
	use[1]=1;
	d[1]=0;
	for(i=2;i<=num;i++)
		d[i]=a[1][i];
	for(j=0;j<num-1;j++)
	{
		minc=maxn;
		for(i=1;i<=num;i++)
		{
			if(!use[i]&&minc>d[i])
			{
				minc=d[i];
				mind=i;
			}
		}
		if(minc!=maxn)
		{
			use[mind]=1;
			for(i=1;i<=num;i++)
			{
				if(!use[i]&&d[i]>a[mind][i])
					d[i]=a[mind][i];
			}
		}
	}
}
void print()
{
	int i,j;
	int sum=0;
	for(i=1;i<=num;i++)
		sum+=d[i];
	printf("%d\n",sum);
}
int main()
{
	int i,j;
	while(scanf("%d",&n)!=EOF&&n)
	{
		while(n--)
		{
			init();
			for(i=1;i<=num;i++)
				bfs(i,zuobiao[i].ttx,zuobiao[i].tty);
            prim();
			print();
		}
	}
	return 0;
}


资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值