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. 开源 免费 java CMS - FreeCMS2.1 会员站内信

    项目地址:http://www.freeteam.cn/ 站内信 1.1.1 写信 从左側管理菜单点击写信进入. 输入收信人.标题.内容后点击发送button. 1.1.2 收件箱 从左側管理菜单点击 ...

  2. Linux命令之dot - 绘制DOT语言脚本描述的图形

    本文链接:http://codingstandards.iteye.com/blog/840055 用途说明 Graphviz (Graph Visualization Software的缩写)是一个 ...

  3. Eclipse3.7默认字体修改-找回Courser-New字体

    1.找到jFace并用WinRAR打开之: jFace的具体位置:$Eclipse目录$/plugins/org.eclipse.jface_3.7.0.I20110522-1430.jar,找到后, ...

  4. Java基础知识强化之多线程笔记01:多线程基础知识(详见Android(java)笔记61~76)

    1. 基础知识: Android(java)学习笔记61:多线程程序的引入    ~    Android(java)学习笔记76:多线程-定时器概述和使用 

  5. git 删除远程master 分支

    ➜  fekit-extension-yo git:(dev) git push origin :master remote: error: By default, deleting the curr ...

  6. 移动前端之 zepto

    移动前端之 zepto http://qtown.corp.qunar.com/media/video/detail?id=1084&type=1&title=%E5%86%AF%E5 ...

  7. C# 内存管理优化畅想(一)---- 大对象堆(LOH)的压缩

    我们都知道,.net的GC是不会压缩大对象堆的,因为其时间开销不可接受,但这是以大对象堆产生大块碎片为代价的,如果以后要分配的大对象比最大的碎片还大,那么即使它比所有碎片的总大小要小,也是无法在不扩展 ...

  8. PHP替换数据库的换行符

    //php 有三种方法来解决 //1.使用str_replace 来替换换行 $str = str_replace(array("\r\n", "\r", &q ...

  9. android调用系统图片浏览器裁切后出现黑边

    是这样的:我使用系统的图片浏览器,然后让它自动跳到图片裁切界面,当我们定义了返回的图片大小过大,而我们实际的图片像素达不到时,系统为我们自动地填充了不够的像素成黑色,那么我们怎么样来解决这个问题呢?不 ...

  10. kvo深入浅出举例

    一,概述   KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知 ...