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
题意:有x个点,其中有一个是起点,起点有不多于100个人,告诉你一张地图,地图上分布着这些点,起点用'S'表示,其余点用‘A’表示,可以走的路为‘ ’,不能走的为‘#’
求从起点出发到所有点所需的最小路径,可以在任意位置分出任意人向某点前进,保证人够,多人走同一路径距离只算一遍
题解:这题题意好难理解……理解后就好办了,题意可以转化为已知x个点的位置,用x-1条边将其连起来,使总边权最小,那不就是一道最小生成树吗?
因为有‘#’的存在,所以必须要把点与点之间的距离用bfs扫出来,之后写一遍最小生成树,然后就没问题了!
新年第一道题1A,真是愉快 代码如下:
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; struct node
{
int from,to,dis;
}si[]; int vis[][],fa[],cnt[][],d[][],tmp=,sum=,ans=,dx[]={,,,-},dy[]={,,-,};
char c[][];
int ttt,n,m; void mem(int n)
{
for(int i=;i<=n;i++)
{
fa[i]=i;
}
} bool cmp(node x,node y)
{
return x.dis<y.dis;
} int find(int x)
{
if(fa[x]!=x)
{
fa[x]=find(fa[x]);
}
return fa[x];
} int union_(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx==fy)
{
return ;
}
fa[fy]=fx;
return ;
} void bfs(int x1,int y1)
{
memset(vis,,sizeof(vis));
memset(cnt,,sizeof(cnt));
queue< pair<int,int> > q;
q.push(make_pair(x1,y1));
vis[x1][y1]=;
while(!q.empty())
{
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int i=;i<=;i++)
{
int dx1=x+dx[i];
int dy1=y+dy[i];
if(dx1<=||dx1>n||dy1<=||dy1>m)
{
continue;
}
if(d[dx1][dy1]!=-&&!vis[dx1][dy1])
{
vis[dx1][dy1]=;
cnt[dx1][dy1]=cnt[x][y]+;
q.push(make_pair(dx1,dy1));
}
if(d[dx1][dy1]>)
{
si[++sum].from=d[x1][y1];
si[sum].to=d[dx1][dy1];
si[sum].dis=cnt[dx1][dy1];
}
}
}
} int main()
{
scanf("%d",&ttt);
while(ttt--)
{
sum=;
ans=;
tmp=;
scanf("%d%d\n",&m,&n);
mem();
for(int i=;i<=n;i++)
{
gets(c[i]);
for(int j=;j<m;j++)
{
if(c[i][j]=='#')
{
d[i][j+]=-;
}
else
{
if(c[i][j]==' ')
{
d[i][j+]=;
}
else
{
if(c[i][j]=='A'||c[i][j]=='S')
{
d[i][j+]=++tmp;
}
}
}
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(d[i][j]>)
{
bfs(i,j);
}
}
}
sort(si+,si+sum+,cmp);
for(int i=;i<=sum;i++)
{
if(union_(si[i].from,si[i].to))
{
ans+=si[i].dis;
}
}
printf("%d\n",ans);
}
}


POJ3026 Borg Maze(bfs求边+最小生成树)的更多相关文章

  1. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

  2. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

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

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

  4. POJ3026 Borg Maze(Prim)(BFS)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12729   Accepted: 4153 Descri ...

  5. poj 3026 Borg Maze (BFS + Prim)

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

  6. POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14165   Accepted: 4619 Descri ...

  7. POJ3026 Borg Maze(最小生成树)

    题目链接. 题目大意: 任意两点(点表示字母)可以连线,求使所有点连通,且权值和最小. 分析: 第一感觉使3维的BFS.但写着写着,发现不对. 应当用最小生成树解法.把每个字母(即A,或S)看成一个结 ...

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

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

  9. POJ - 3026 Borg Maze bfs+最小生成树。

    http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...

随机推荐

  1. linux系统无法挂载U盘

    插上U盘 [ 2407.650440] usb 1-3.3: new high speed USB device number 7 using s5p-ehci [ 2407.887332] usb ...

  2. 1、Flume基础扫盲

    1.概述 Flume是一个分布式.可靠的和高可用的海量日志采集.聚合和传输的系统.支持在系统中定制种类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并写到各种数据接受方(可定制)的 ...

  3. FPGA前世今生(二)

    上期我们介绍了关于FPGA内部最基本的结构,在quartus下可以看到整体的结构. 这是在平面规划图下看到的结构,其中蓝色的小格代表一个LAB.四周边上浅棕色的小格代表IO口. 这是一个LAB的内部结 ...

  4. TELNET协议规范

    ARPA Internet上的主机被要求采用并实现此标准. 介绍 TELNET Protocol的目的是提供一个相对通用的,双向的,面向八位字节的通信方法.它主要的目标是允许接口终端设备的标准方法和面 ...

  5. Java-Runoob:Java 开发环境配置

    ylbtech-Java-Runoob:Java 开发环境配置 1.返回顶部 1. Java 开发环境配置 在本章节中我们将为大家介绍如何搭建Java开发环境. Windows 上安装开发环境 Lin ...

  6. Resize事件和SizeChanged事件

    窗体加载的时候, 就会触发Form_ResizeBeginForm_ResizeEnd 窗体的拖动会触发:Form_ResizeBeginForm_ResizeEnd 窗体的最小化按钮会触发:Form ...

  7. 学习 altera官网 之 timequest

    1.如果启动沿(launch)和锁存沿(latch)是同一时钟域则,latch比launch晚一个时钟周期. 2.数据到达时间 3.时钟到达时间.如果启动沿(launch edge)和锁存沿(latc ...

  8. 简单 JS 弹出层 背景变暗

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 【知识碎片】JavaScript篇

     40.选择器组合 逗号是多选择器空格 是子子孙孙尖括号 只找儿子 39.失去焦点事件blur $("input").blur(function(){ $("input& ...

  10. 如何用CURL将文件下载到本地指定文件夹

    若直接调用下载文件的url有重定向,则需先调用第一个方法,获取到跳转后的url,才可直接下载.否则需要手动点击浏览器的下载确定按钮. 调用示例: $imgpath = "http://www ...