POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze
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'是要到达的点。
输出从S开始路过所有的A点所需要的路程(重复路过只算一次)。
解题思路:
因为重复的路过某个点,路程只算一次,就相当于计算一个包含所有的'A'和'S'的最小树。
使用BFS来计算边的权值(上下左右四种情况,简单的BFS,注意初始点的dis为0),来构成完全无向图,再用Kruskal算法算出权值和即可。(注意题目数据范围,数组开小了会WA)
PS : POJ坑爹的后台,每行后面可能有坑爹的多余空格,注意处理掉即可。
Code:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#define MAXN 1000
using namespace std;
struct point
{
int x,y;
} P[];
struct point q[];
int N,M,father[];
bool vis[][],v[][];
int dis[];
struct edge
{
int begin,end;
int dis;
} T[*];
void init()
{
memset(vis,,sizeof(vis));
for (int i=; i<=; i++)
father[i]=i;
}
int bfs(int a,int b)
{
int front=,rear=;
q[front]=P[a];
for (int i=; i<=M; i++)
for (int j=; j<=N; j++)
v[i][j]=vis[i][j];
dis[front]=;
while (front<rear)
{
if (q[front].x==P[b].x&&q[front].y==P[b].y) break;
int x=q[front].x,y=q[front].y;
int a[]= {x-,x+,x,x},b[]= {y,y,y+,y-};
for (int i=; i<=; i++)
if (a[i]>=&&a[i]<=M&&b[i]>=&&b[i]<=N&&!v[a[i]][b[i]])
{
q[rear].x=a[i],q[rear].y=b[i];
dis[rear]=dis[front]+;
v[a[i]][b[i]]=;
rear++;
}
front++;
}
return dis[front];
}
int find(int x)
{
if (father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
father[fx]=fy;
}
bool cmp(struct edge a,struct edge b)
{
return a.dis<b.dis;
}
int main()
{
int C;
cin>>C;
while (C--)
{
init();
cin>>N>>M;
int k=;
char ch;
while ()
{
ch=getchar();
if (ch=='\n') break;
}
for (int i=; i<=M; i++)
{
for (int j=; j<=N; j++)
{
char tmp;
scanf("%c",&tmp);
if (tmp=='#') vis[i][j]=;
else vis[i][j]=;
if (tmp=='A'||tmp=='S')
{
P[k].x=i,P[k].y=j;
k++;
}
}
while ()
{
ch=getchar();
if (ch=='\n') break;
}
}
int t=;
for (int i=; i<k; i++)
for (int j=; j<i; j++)
{
T[t].begin=i;
T[t].end=j;
T[t].dis=bfs(i,j);
t++;
}
int cnt=,sum;
sort(T+,T+t,cmp);
for (int i=; i<t; i++)
{
if (find(T[i].begin)!=find(T[i].end))
{
cnt+=T[i].dis;
join(T[i].begin,T[i].end);
sum++;
if (sum==k-) break;
}
}
printf("%d\n",cnt);
}
return ;
}
POJ3026——Borg Maze(BFS+最小生成树)的更多相关文章
- POJ3026 Borg Maze(bfs求边+最小生成树)
Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- POJ - 3026 Borg Maze bfs+最小生成树。
http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- POJ3026 Borg Maze(Prim)(BFS)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12729 Accepted: 4153 Descri ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14165 Accepted: 4619 Descri ...
- 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...
随机推荐
- union 中可以存储的是不带构造函数的类对象
union 中可以存储的是不带构造函数的类对象 否则不符合逻辑 为什么不符合逻辑?
- 清理c盘垃圾(将一下代码复制到记事本然后把后缀名改为xxx.bat,然后双击,就ok了!!)
@echo off echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive%\*._m ...
- 对于观察者模式,Reactor模式,Proactor模式的一点理解
最近就服务器程序IO效率这一块了解一下设计模式中的Reacotr模式和proactor模式,感觉跟观察者模式有些类似的地方,网上也看了一些其他人对三者之间区别的理解,都讲得很仔细,在此根据自己的理解做 ...
- java应用uploadify 3.2丢失session
java应用uploadify 3.2丢失session http://c-bai.iteye.com/blog/1829269 uploadify上传用的是一个flash插件. flash中有个bu ...
- iOS Mac系统下Ruby环境安装
由EasyIOS引出的一系列问题:转载的上一篇CocoaPods安装和使用教程中说明了,为什么要使用cocoapods ,但是要安装cocoapods需要Ruby环境,安装Ruby环境首先需要安装Xc ...
- python staticmethod classmethod
http://www.cnblogs.com/chenzehe/archive/2010/09/01/1814639.html classmethod:类方法staticmethod:静态方法 在py ...
- YII千万级PV架构经验分享--俯瞰篇--性能介绍
一张图,啥也不说了.直接上图,大图真难画. 呃,非得写满二百个字,其实本来想画均衡负债,一些服务器假设列子的,突然发现,没有业务要求,画不出来.写了这么久了,天天熬夜,得休息几天再继续.其实还有非常重 ...
- HttpUnit学习笔记
<!-- htmlUnit --> <dependency> <groupId>net.sourceforge.htmlunit</groupId> & ...
- Spark小课堂Week1 Hello Spark
Spark小课堂Week1 Hello Spark 看到Spark这个词,你的第一印象是什么? 这是一朵"火花",官方的定义是Spark是一个高速的.通用的.分布式计算系统!!! ...
- Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)
Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)