博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3026——Borg Maze(BFS+最小生成树)
阅读量:2344 次
发布时间:2019-05-10

本文共 3599 字,大约阅读时间需要 11 分钟。

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

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点处,求需要的最小步数。
因为人群可以分裂,其实就是不会走重复的路,模拟一下就正好是求一个最小生成树,其中S点和A点为点,他们之间的最短路径就是边。
最短路径由BFS可求出,然后转化成一棵树,再求最小生成树,注意其中的转换,因为一下用了两个模板,然后变量命名发生了冲突,调了两个多小时。。。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 105#define Mod 10001using namespace std;int x,y;char mp[MAXN][MAXN];int vis[MAXN][MAXN],map[MAXN][MAXN],dis[MAXN*MAXN],vis1[MAXN*MAXN];struct Node{ int x,y,step;};Node p[MAXN*MAXN];int kw[MAXN][MAXN];int num;void dfs(int xi,int yi){ memset(vis,0,sizeof(vis)); Node start,tmp; start.x=xi,start.y=yi; start.step=0; vis[xi][yi]=1; queue
q; q.push(start); while(!q.empty()) { tmp=q.front(); q.pop(); if(mp[tmp.x][tmp.y]=='A'||mp[tmp.x][tmp.y]=='S') { map[kw[xi][yi]][kw[tmp.x][tmp.y]]=tmp.step; map[kw[tmp.x][tmp.y]][kw[xi][yi]]=tmp.step; } Node tmp1; tmp1.x=tmp.x+1,tmp1.y=tmp.y,tmp1.step=tmp.step+1; if(tmp1.x
=0&&!vis[tmp1.x][tmp1.y]&&mp[tmp1.x][tmp1.y]!='#') { vis[tmp1.x][tmp1.y]=1; q.push(tmp1); } tmp1.x=tmp.x,tmp1.y=tmp.y+1,tmp1.step=tmp.step+1; if(tmp1.y
=0&&!vis[tmp1.x][tmp1.y]&&mp[tmp1.x][tmp1.y]!='#') { vis[tmp1.x][tmp1.y]=1; q.push(tmp1); } }}int prim(){ int n=num; int i,j,pos,min,ans=0; memset(vis1,0,sizeof(vis1)); memset(dis,0,sizeof(dis)); vis1[1]=1,pos=1; for(i=1; i<=n; ++i) if(i!=pos) dis[i]=map[pos][i]; for(i=1; i
map[pos][j]) dis[j]=map[pos][j]; } return ans;}int main(){ int t; scanf("%d",&t); while(t--) { memset(mp,0,sizeof(mp)); memset(map,0,sizeof(map)); num=0; cin>>y>>x; gets(mp[0]); for(int i=0; i

转载地址:http://zxcvb.baihongyu.com/

你可能感兴趣的文章
和为s的连续正数序列
查看>>
什么是Redis?什么是nosql?NoSQL数据库的四大分类
查看>>
为什么说Redis是单线程的以及Redis为什么这么快!
查看>>
redis的过期健删除策略以及内存淘汰机制
查看>>
map 如何使用结构体作为自定义键值
查看>>
Mysql几种索引类型的区别及适用情况
查看>>
Redis持久化的两种方式
查看>>
判断一个数组,是否可以分成两个数组之和相等的数组
查看>>
背包问题
查看>>
结构体变量之间的比较和赋值原理
查看>>
C++ const修饰函数、函数参数、函数返回值
查看>>
将单链表的每k个节点之间逆序
查看>>
删除链表中重复的节点——重复节点不保留
查看>>
2018腾讯校招编程题——最重要的城市
查看>>
删除链表中重复的节点——重复节点保留一个
查看>>
实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit()).md
查看>>
链表排序.md
查看>>
进程与线程的区别与联系、进程与线程的通信方式
查看>>
C++与C的区别
查看>>
产生死锁的必要条件及处理方法
查看>>