Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9821   Accepted: 3283

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
 #include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include <iostream>
using namespace std;
int map[][];
int vis[][];
int parent[];
int row, col;
int edge_num = ;
int point_num=;
struct edge {
int u, v, w;
} e[ * ];
struct points {
int u, v, d;
} point;
bool cmp(const edge e1,const edge e2){
return e1.w<e2.w;
}
int Kruskal(){
int mindis=;
for(int i=;i<=point_num;i++){
parent[i]=i;
}
sort(e,e+edge_num,cmp);
int pnum=;
for(int i=;i<edge_num&&pnum<point_num;i++){
int k,g;
int u=e[i].u;
int v=e[i].v;
for(k=parent[u];parent[k]!=k;k=parent[parent[k]]);
for(g=parent[v];parent[g]!=g;g=parent[parent[g]]); if(k!=g){
parent[g]=k;
mindis+=e[i].w;
pnum++;
}
}
return mindis;
}
void BFS(int x, int y) {
queue<points> p;
points s;
s.u = x;
s.v = y;
s.d = ;
p.push(s);
memset(vis, , * * sizeof(int));
vis[x][y] = ; while (!p.empty()) {
points tmp = p.front();
p.pop();
for (int i = -; i < ; i += ) {
points next;
next.u = tmp.u + i;
next.v = tmp.v;
next.d = tmp.d + ;
if (next.u >= && next.u < row) {
if (!vis[next.u][next.v]) {
int pos = map[next.u][next.v];
vis[next.u][next.v] = ;
if (pos >= )
p.push(next);
if (pos >= ) {
e[edge_num].u = map[x][y];
e[edge_num].v = pos;
e[edge_num].w = next.d;
edge_num++;
}
}
}
}
for (int i = -; i < ; i += ) {
points next;
next.u = tmp.u;
next.v = tmp.v + i;
next.d = tmp.d + ;
if (next.v >= && next.v < col) {
if (!vis[next.u][next.v]) {
int pos = map[next.u][next.v];
vis[next.u][next.v] = ;
if (pos >= )
p.push(next);
if (pos >= ) {
e[edge_num].u = map[x][y];
e[edge_num].v = pos;
e[edge_num].w = next.d;
edge_num++;
}
}
}
} }
}
int main() {
int n;
scanf("%d", &n);
char tmp[];
for (int i = ; i < n; i++) {
point_num=;
edge_num=;
scanf("%d %d", &col, &row);
gets(tmp); //坑【一串空格】
for (int j = ; j < row; j++) {
for (int k = ; k < col; k++) {
char c;
scanf("%c", &c);
if (c == ' ')
map[j][k] = ;
else if (c == '#')
map[j][k] = -;
else if (c == '\n')
k--;
else
map[j][k] = ++point_num;
} }
for (int j = ; j < row; j++) {
for (int k = ; k < col; k++) {
if (map[j][k] > ) {
BFS(j, k);
}
}
}
int mid=Kruskal();
printf("%d\n",mid);
}
return ;
}

Borg Maze - poj 3026(BFS + Kruskal 算法)的更多相关文章

  1. Borg Maze POJ - 3026 (BFS + 最小生成树)

    题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...

  2. Borg Maze poj 3026

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

  3. J - Borg Maze - poj 3026(BFS+prim)

    在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...

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

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  5. poj 3026(BFS+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12032   Accepted: 3932 Descri ...

  6. POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Specia ...

  7. poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9718   Accepted: 3263 Description The B ...

  8. POJ 3026 Borg Maze(Prim+bfs求各点间距离)

    题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...

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

    题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...

随机推荐

  1. BZOJ 2669- [cqoi2012]局部极小值

    不错的题啊 挺好的结合了容斥和状压DP 保证每个数各不相同,又有大小关系,那么就可以将数字从小到大填. 不难发现 局部极小值<=8,这个可以状压,f[i][j] 表示填了前i个数,局部极小值被填 ...

  2. luogu P1446 [HNOI2008]Cards

    题目链接 luogu P1446 [HNOI2008]Cards 题解 题意就是求染色方案->等价类 洗牌方式构成成了一个置换群 然而,染色数限制不能用polay定理直接求解 考虑burnsid ...

  3. ASIHTTPRequest学习(四)

    如果是IOS5的版本,可能集成过程中会遇到一些问题,我也找到了一些解决方案,比如,集成完后可能会遇到编译提示找不到"libxml/HTMLparser.h",解决这个问题可以参考这 ...

  4. 【SQL】在SQL Server中多表关联查询问题

    好久没有写SQL语句的多表连接查询,总在用框架进行持久化操作.今天写了一个多表关联查询,想根据两个字段唯一确定一条数据 失败的案例如下: SELECT cyb.id,ad.name FROM [Gen ...

  5. vue中自定义指令vue.direvtive,自定义过滤器vue.filter(),vue过渡transition

    自定义指令 默认设置的核心指令( v-model,v-bind,v-for,v-if,v-on等 ),Vue 也允许注册自定义指令.注意,在 Vue2.0 里面,代码复用的主要形式和抽象是组件——然而 ...

  6. LNMP第一部分环境搭建

    1. MySQL安装(同LAMP里面的安装方法)2.  php安装wget  http://cn2.php.net/distributions/php-5.4.37.tar.bz2tar jxf ph ...

  7. j2ee、mvn、eclipse、Tomcat等中文乱码问题解决方法

    一.更改jdk默认编码为UTF-8,保证启动的JVM不会出现中文乱码问题 1.在编译的时候,如果我们没有用 -encoding 参数指定我们的JAVA源程序的编码格式,则javac.exe首先获得我们 ...

  8. EffectiveJava(4)通过私有构造器强化不可实例化的能力

    通过私有构造器强化不可实例化的能力 原理:只有当类不包含显式的构造器时,编译器才会生成缺省的构造器,因此只要让这个类包含私有构造器,他就不能被实例化 这种方式下,子类没有可访问的超类构造器可调用 // ...

  9. Word文档打不开怎么办

    目前一些主流的办公软件给大家日常工作带来了很大便利,比如:Microsoft Office或金山WPS!我们在愉快地使用它们的同时,多少也遇到了一些让人尴尬或头疼的问题,比如:精心制作的文档,突然打不 ...

  10. Node.js 使用http客户端向网站请求数据并保存

    app.js代码: // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // 内置文件处理模块 var fs=requir ...