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);
}
}


最新文章

  1. [原创]Lodop打印, 以及Lodop引用css文件控制打印样式的问题.
  2. Android抓包方法(三)之Win7笔记本Wifi热点+WireShark工具
  3. 第2章 使用JavaScript
  4. linux 搭建nginx环境(centos6.5)
  5. 【Shell脚本学习6】Shell变量:Shell变量的定义、删除变量、只读变量、变量类型
  6. PetShop学习第四天
  7. 阿里巴巴2018届应届生在线编程测验-研发工程师C/C++
  8. Xshell学习--菜鸟篇
  9. [Java] 设计模式:代码形状 - lambda表达式的一个应用
  10. JavaScript在div后添加删除div
  11. JDK 8 之 Stream sorted() 示例
  12. MySQL中 in和exists的区别
  13. python day31--网络编程,tcp,udp的指令,及黏包
  14. ThinkPHP实现支付宝接口功能 代码实例
  15. SVG绘制太极图
  16. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
  17. Requests 校花网图片爬取
  18. Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)
  19. [CC-TRIPS]Children Trips
  20. (转) mysql中left join,right join,inner join的区别

热门文章

  1. 环境无法创建目录,提示Too many links
  2. Array数组(PHP学习)
  3. Java开发进阶技能(附文章引用链接)
  4. java调用python
  5. windows环境下,安装zookeeper~
  6. 科学家开发新AI系统,可读取大脑信息并表达复杂思想
  7. leetcode606
  8. leetcode582
  9. [JBPM3.2]TaskNode的signal属性详解
  10. python实现文件下载的方法总结