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. vs2008 c++工程如何设置生成调试信息

    记录一个使用vs2008碰到的问题: 今天在用vs2008的时候,想封装一个lib库,建了一个c++的lib工程,后来为了测试函数功能,想偷懒就直接在工程中加了个main函数,并且把工程属性prope ...

  2. Java使用基本JDK操作ZIP文件以及zip文件的加密、解密等功能

    Java使用基本JDK操作ZIP文件 http://blog.csdn.net/zhyh1986/article/details/7723649 Java解压和压缩带密码的zip文件 http://b ...

  3. JSONP 含jquery 实例

     前言: 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Soc ...

  4. php版获取重定向后地址的代码分享

    如何获取重定向的地址呢?我们用php实现这样的功能,分享下我的代码,有需要的朋友参考下. 代码如下: <?php //取重定向的地址 class RedirectUrl{ //地址 var $u ...

  5. jsp+oracle 排序分页+Pageutil类

    1.rownum和排序 Oracle中的rownum的是在取数据的时候产生的序号,所以想对指定排序的数据去指定的rowmun行数据就必须注意了. SQL> select rownum ,id,n ...

  6. android.support.v7.app.AppCompatActivity

    1.Android Studio (api 23) 新建项目的时候 Activity public class MainActivity extends AppCompatActivity 2.系统默 ...

  7. MySQL 5.1.63 单机配置多实例(简单配置)

    需求: 在一台服务器上通过源码编译安装一个版本为5.1.63版本MySQL数据库: 方案:将所有配置文件与数据等均存放在/home/zhaoshuangshuang下.在同一个MySQL中运行两个实例 ...

  8. 十六、mysql 分区之 简单sql优化1

    .使用 show session status like '%Com_%'; 可以查看当前连接的各个sql的执行频率 show global status like '%Com_%'; 可以查看从上次 ...

  9. 七、mysql索引选择

    .myisam,bdb,innodb,memory 单表至少支持16个索引 .create index id_index on emp (id) 为emp表创建一个名为id_index的id字段的索引 ...

  10. PHP webserver 之 soap 生成wsdl文件

    <?php /** * Copyright (c) , Braulio Jos?Solano Rojas * All rights reserved. * * Redistribution an ...