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. timestamp ---自动更新修改时间 与 记录首次插入时间

    自动更新修改时间: mysql> create table z(a int ,b timestamp on update current_timestamp); mysql> insert ...

  2. freemarker if..else.. 的使用

    FreeMarker是一款模板引擎,今天在做Pad端的时候正好用到,用法非常简单: 在xml配置页面的文件中,直接使用 <#if 1=1> //条件成立要显示的内容 </#if> ...

  3. 关于jsp web项目中的javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/el/ELResolver错误

    错误: javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/el/ELResolver org.apache.j ...

  4. Java基础知识强化之集合框架笔记16:List集合的特有功能概述和测试

    1. List集合的特有功能概述: (1)添加功能: void add(int index, Object element):在指定位置添加元素 (2)获取功能: Object get(int ind ...

  5. css 权威指南笔记( 五)结构和层叠

    特殊性 重要性 !important; 继承 向上传播例外,应用到body元素的背景样式可以传递到html元素,相应对的可以定义其画布. 大多数框模型属性(包括外边距.内边距.背景.边框)都不能继承 ...

  6. 安装Visual Studio 2013 中文社区版

    Visual Studio 2013 免费了,我收到邮件后,立即从邮件的下载连接安装了 Visual Studio Community 2013 with Update 4 . 安装后几天没打开,今天 ...

  7. spring下载dist.zip

    http://repo.springsource.org/libs-release-local/org/springframework/spring/ 选择对应版本下载即可

  8. openmpi+NFS+NIS搭建分布式计算集群

    1.        配置防火墙 正确配置防火墙的过滤规则,否则会造成NFS文件系统的挂载失败,NIS账户认证的失败,mpirun远程任务实例投放的失败.一般情况下,计算集群是在内部局域网中使用,所以可 ...

  9. WebForm开发常用代码

    1.获取服务器绝对路径: public static string GetMapPath(string strPath) { if (HttpContext.Current != null) { re ...

  10. 织梦dede编辑器ckeditor如何添加中文字体不乱码

    dedecms内容编辑器ckeditor没有中文字体,找了很多教程都是千篇一律,而且都是错的,终于找到了一篇,结合自己的实际操作,来教您如何添加中文字体,并且解决乱码问题.   工具/原料 dedec ...