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

这个题的目的就是,找一个最小生成树,把所有的字母链接起来;

大概思路;

1.输入编号

2.找出每两个字母之间的 权重并储存

3.对于数组中的数据按权重排序

4.并查集+Kruskal算法最小生成树。

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
int v[],mapp[][],vis[][];
int m,n,num,c[]= {,,,-},r[]= {,-,,};
struct node
{
int a;
int b;
int weight;
} s[],p,q;
queue<node>que;
int cmp(const void*a,const void *b)
{
return (*(node*)a).weight-(*(node*)b).weight;
}
int findl(int n)
{
return v[n]==n?n:findl(v[n]);
}
void get()//输入函数
{
int i,j,k=;
char ch;
memset(mapp,,sizeof(mapp));//清零
scanf("%d %d",&n,&m);
char str[];//据说输入m,n后会有很多空格,我就是因为这个WA了一次
gets(str);
m++,n++;
for(i=; i<m; i++)
{
for(j=; j<n; j++)
{
scanf("%c",&ch);
if(ch=='A'||ch=='S')//大于O的表示字母
mapp[i][j]=k++;
if(ch==' ')//-1表示可以走的路
mapp[i][j]=-;
}
getchar();//去掉换行
}
}
void bfs(int i,int j,int step)
{
int a,b,k;
a=p.a=i,b=p.b=j,p.weight=step;
memset(vis,,sizeof(vis));
vis[a][b]=;
que.push(p);
while(!que.empty())
{
p=que.front();
que.pop();
for(k=; k<; k++)
{
if(!mapp[p.a+c[k]][p.b+r[k]]||vis[p.a+c[k]][p.b+r[k]])
continue;
if(mapp[p.a+c[k]][p.b+r[k]]>)//找到字母,将字母序号,权重存入数组
{
s[num].a=mapp[a][b];
s[num].b=mapp[p.a+c[k]][p.b+r[k]];
s[num].weight=p.weight+;
num++;
}
q.a=p.a+c[k],q.b=p.b+r[k],q.weight=p.weight+;
que.push(q);
vis[q.a][q.b]=;
}
} }
int main()
{
int t,i,j,sum; scanf("%d",&t);
while(t--)
{
num=sum=;
get();//输入函数
for(i=; i<m; i++)
{
for(j=; j<n; j++)
{
if(mapp[i][j]>)
bfs(i,j,);
}
}
qsort(s,num,sizeof(node),cmp);//按权值从小到大排序
for(i=; i<; i++)
v[i]=i;
for(i=; i<num; i++)
{
if(findl(s[i].a)!=findl(s[i].b))//简单的并查集
{
sum+=s[i].weight;
v[findl(s[i].a)]=s[i].b;
}
}
printf("%d\n",sum);
}
return ;
}

Borg Maze poj 3026的更多相关文章

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

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

  2. Borg Maze - poj 3026(BFS + Kruskal 算法)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 Description The B ...

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

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

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

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

  5. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  6. poj 3026 Borg Maze (BFS + Prim)

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

  7. POJ 3026 Borg Maze【BFS+最小生成树】

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

  8. 【POJ 3026】Borg Maze

    id=3026">[POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值 ...

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

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

随机推荐

  1. Linux kernel驱动相关抽象概念及其实现 之“linux设备模型kobject,kset,ktype”

    kobject,kset,ktype三个很重要的概念贯穿Linux内核驱动架构,特转载一篇博文: (转载自http://blog.csdn.net/gdt_a20/article/details/64 ...

  2. 经典SQL语句大全之提升

    二.提升 1.说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)法一:select * into b from a where 1<>1(仅用于SQlServer)法 ...

  3. lenky的个人站点 ----LINUX 内核进程

    http://www.lenky.info/archives/category/nix%E6%8A%80%E6%9C%AF/%E5%86%85%E6%A0%B8%E6%8A%80%E6%9C%AF

  4. ARC和非ARC文件混编

    在编程过程中,我们会用到很多各种各样的他人封装的第三方代码,但是有很多第三方都是在非ARC情况下运行的,当你使用第三方编译时出现和下图类似的错误,就说明该第三方是非ARC的,需要进行一些配置. 解决方 ...

  5. vim 缩写abbreviation

    创建 :ab abbreviation pharse 取消 :unab abbreviation 缩写使用 insert模式下输入缩写,Enter键获得pharse.

  6. .net 学习路线感想

    从上到大学到现在工作,已经有六年多了,发现学习编程到以开发为工作也是一个挺长的过程的. 大学中,从c语言到java.C#到其他各种语言的学习,还有其他知识的学习如:数据库(oracle.sql Ser ...

  7. (转载)loadrunner简单使用——HTTP,WebService,Socket压力测试脚本编写

    原文出处:http://ajita.iteye.com/blog/1728243/ 先说明一下,本人是开发,对测试不是特别熟悉,但因工作的需要,也做过一些性能测试方面的东西.比较久之前很简单的用过,最 ...

  8. Objective - C 中NSString (字符串)与C中的字符串转换问题

    NSString是一个常用的类,NSString是原生支持unicode C中的字符串 比如char * a = "hello world";  是utf8类型的, char* d ...

  9. 344. Reverse String(C++)

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...

  10. javascript——归并方法

    <script type="text/javascript"> //ECMAScript5 还新增了2个归并数组的方法:reduce()和reduceRight(). ...