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

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

Source

—————————————————————————————————————
题意:在一个迷宫里,起点是S,A代表外星人,我们需要找到最短的路径将S和所有的A连接起来,输出最短的这个路径总长
思路:先bfs建图,再最小生成树处理

注意:测试数据多了莫名其妙的空格,getchar()会wa,看了discuss在知道,智商题浪费时间

#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<cstring>
using namespace std;
#define LL long long struct node
{
int u,v,w;
} p[1000005]; struct point
{
int x,y,t;
}; char mp[105][105];
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
int vis[105][105]; int n,m,cnt,tot,pre[10005]; bool cmp(node a,node b)
{
return a.w<b.w;
}
void init()
{
for(int i=0; i<10005; i++)
pre[i]=i;
} int fin(int x)
{
return pre[x]==x?x:pre[x]=fin(pre[x]);
} void kruskal()
{
sort(p,p+cnt,cmp);
init();
int cost=0;
int ans=0;
for(int i=0; i<cnt; i++)
{
int a=fin(p[i].u);
int b=fin(p[i].v);
if(a!=b)
{
pre[a]=b;
cost+=p[i].w;
ans++;
}
if(ans==tot-1)
{
break;
}
}
printf("%d\n",cost);
} bool check(int x,int y)
{
if(x<0||x>=n||y<0||y>=m||mp[x][y]=='#')
return 0;
return 1;
} void build(int x,int y)
{
memset(vis,0,sizeof vis);
point f,d;
f.x=x;
f.y=y;
f.t=0;
vis[x][y]=1;
queue<point>q;
q.push(f);
while(!q.empty())
{
f=q.front();
q.pop();
if((mp[f.x][f.y]=='A'||mp[f.x][f.y]=='S')&&(f.x!=x||f.y!=y))
{
p[cnt].u=100*x+y;
p[cnt].v=100*f.x+f.y;
p[cnt++].w=f.t;
}
for(int i=0; i<4; i++)
{
int xx=f.x+dir[i][0];
int yy=f.y+dir[i][1];
if(!vis[xx][yy]&&check(xx,yy))
{
d.x=xx;
d.y=yy;
d.t=f.t+1;
vis[xx][yy]=1;
q.push(d);
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
char temp[51];
gets(temp);
for(int i=0; i<n; i++)
gets(mp[i]);
cnt=0,tot=0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(mp[i][j]=='A'||mp[i][j]=='S')
build(i,j),tot++;
kruskal();
}
return 0;
}


POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏的更多相关文章

  1. linux中的网络通信指令 分类: 学习笔记 linux ubuntu 2015-07-06 16:02 134人阅读 评论(0) 收藏

    1.write write命令通信是一对一的通信,即两个人之间的通信,如上图. 效果图 用法:write <用户名> 2.wall wall指令可将信息发送给每位同意接收公众信息的终端机用 ...

  2. Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏

    Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...

  3. HDU6027 Easy Summation 2017-05-07 19:02 23人阅读 评论(0) 收藏

    Easy Summation                                                             Time Limit: 2000/1000 MS ...

  4. NavBarControl控件 2015-07-23 16:56 2人阅读 评论(0) 收藏

    NavBarControl控件 1.      新建一个windows窗体应用程序项目 2.      在工具箱中的Navigation& Layout选项卡下找到NavBarControl, ...

  5. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. iOS开发网络数据之AFNetworking使用 分类: ios技术 2015-04-03 16:35 105人阅读 评论(0) 收藏

    http网络库是集XML解析,Json解析,网络图片下载,plist解析,数据流请求操作,上传,下载,缓存等网络众多功能于一身的强大的类库.最新版本支持session,xctool单元测试.网络获取数 ...

  7. Hdu4135 Co-prime 2017-06-27 16:03 25人阅读 评论(0) 收藏

    Co-prime Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Subm ...

  8. Oracle 字符集的查看和修改 分类: H2_ORACLE 2013-06-19 16:52 316人阅读 评论(0) 收藏

    一.什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE 支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.它使数据库 ...

  9. Copy page via powershell and not save as template 分类: Sharepoint 2015-07-16 16:39 4人阅读 评论(0) 收藏

    By save as template informaton of the page get lost, e.g. permissions. To avoid this, use powershell ...

随机推荐

  1. Boost.Hana

    Boost.Hana Boost.Hana 是一个元编程的库.它为不同种类数据的集合以及类型的集合提供了容器和算法. #include <boost/hana.hpp> namespace ...

  2. Haskell语言学习笔记(38)Lens(1)

    Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...

  3. intelij创建MapReduce工程

    1.创建一个maven工程 2.POM文件 <?xml version="1.0" encoding="UTF-8"?><project xm ...

  4. Python—— *与** 参数说明

    Python *与** 参数说明 '''*用来传递任意个无名字参数,这些参数会一个Tuple的形式访问''' def fall(*z): print sum(z) print "keys t ...

  5. Error: Cannot find a valid baseurl for repo: epel

    修改一下/etc/yum.repos.d/epel.repo文件, enable=1改为enable=0

  6. xpath的层级与逻辑定位

    xpath的层级与逻辑定位: 之前我们是通过class和id,name,如果我们所需要的元素没有class,id,name这样的元素,怎么定位呢 1.在不使用xpath情况下:元素没有属性值得时候怎么 ...

  7. mysq 数据库基本管理

    命令表: 1.设置mysql数据库密码 mysqladmin -u root password 'new-password' 'new-password'为数据库密码. 2.登陆mysql数据库 my ...

  8. cf451C-Predict Outcome of the Game

    http://codeforces.com/problemset/problem/451/C A - Predict Outcome of the Game Time Limit:2000MS     ...

  9. [leetcode]403. Frog Jump青蛙过河

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  10. discuz回贴通知插件实现-用户状态设置

    1.获取用户提交数据 discuz通过$_GET来获取全部数据,包括($_GET,$_POST). else if($_GET['pluginop'] == 'set') { //获取用户提交数据 $ ...