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+最小生成树)的更多相关文章

  1. POJ3026 Borg Maze(bfs求边+最小生成树)

    Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...

  2. poj 3026 Borg Maze (bfs + 最小生成树)

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

  3. POJ - 3026 Borg Maze bfs+最小生成树。

    http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...

  4. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

  5. POJ3026 Borg Maze(Prim)(BFS)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12729   Accepted: 4153 Descri ...

  6. POJ 3026 Borg Maze (最小生成树)

    Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...

  7. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  8. POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14165   Accepted: 4619 Descri ...

  9. 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...

随机推荐

  1. Shell 循环读取文件

    使用Shell将Windows环境下的文件拷贝到Linux下面的用法. 在linux下,将dos文件格式转换成linux文件格式的用法,vi打开,然后转到命令格式,执行,然后保存,就可以转换成linu ...

  2. ZigBee 入网详解

    本文将根据Sniffer来详细解释ZigBee终端设备入网的整个流程,原创博文. 当协调器建立好网络后,终端设备执行zb_startrequest函数,准备入网时,他们两者之间详细的流程如下.

  3. 3月3日(3) Binary Tree Preorder Traversal

    原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的..  啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...

  4. Linux C 程序 文件属性,文件删除(15)

    dup ,dup2,fcntl,ioctl系统调用 . dup ,dup2 函数 int dup(int oldfd) int dup(int oldfd , int newfd) dup用来复制参数 ...

  5. IE下不支持option的onclick事件

    <select> <option onclick="test('www.hao123.com')"value="www.hao123.com" ...

  6. DevExpress gridView 小结(一)

    一:第一列显示行号  CustomDrawRowIndicator this.gridViewDevice.IndicatorWidth = 40; this.gridViewDevice.Custo ...

  7. [Linux]学习笔记(1)

    说到Linux就不得不提UNIX,因为Linux是从UNIX系统发展来的,两系统极为相似,可以在UNIX操作系统上使用的功能都可以在Linux上使用,只可能有少许的差异: UNIX系统中所有的东西都是 ...

  8. phpcms v9 自定义伪静态的分页函数

    因为.这个页面还没做好..等做好了..再给大家演示...... 调用方法:$page_attr=pages_open($num[0]['cun'],$get_page,$max_page,'http: ...

  9. c# 判断点是否在区域内 点在区域内 在多边形内 判断

    方法一 算法 : public int isLeft(Point P0, Point P1,Point P2)        {            int abc= ((P1.X - P0.X) ...

  10. Describe the difference between repeater, bridge and router.

    中继器,路由器,网桥,网关的区别. 路由器:网络层设备,实现复杂的路径选择,控制IP包从源到目的地的路径:根据IP地址进行选路和转发IP数据包 中继器:物理层设备,物理信号的重新生成,对信号进行整形和 ...