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. Delphi 创建一个url网址快捷方式代码

    procedure CreateURLShortcut(const ShortcutFile, URL: string); var F: TextFile; // text file begin {$ ...

  2. java.lang.NullPointerException - 如何处理空指针异常

    当应用程序试图null在需要对象的情况下使用时抛出.这些包括: 调用null对象的实例方法. 访问或修改null对象的字段. 把长度null当作一个数组. 像访问或修改null阵列一样访问或修改插槽. ...

  3. poj3666(DP+离散化)

    题目链接:http://poj.org/problem?id=3666 思路: 看了讨论区说本题的数据比较弱,只需要考虑不减序列即可,比较懒,所以我也只写了这一部分的代码,思路都一样,能AC就行了. ...

  4. Island Transport

    Island Transport http://acm.hdu.edu.cn/showproblem.php?pid=4280 Time Limit: 20000/10000 MS (Java/Oth ...

  5. phpStudy6——php导出可以设置样式的excel表格

    前言: 一般的后台管理页面肯定少不了excel表格导出的功劳,尤其是那些电商平台的订单导入导出,用户列表的导入导出等,那么本文就介绍php是如何导出excel表格的. php导出excel方法有很多, ...

  6. python之多并发socket

    先看socket多并发的服务端的代码,这里是用多线程实现的多并发socketserver import socketserver # socketserver有四个基本的类,后两个不常用,这4个类处理 ...

  7. 如何给a标签绑定ajax事件

    <a href="review?action=delete&id=${review.id}&articleId=${review.articleId}"cla ...

  8. swift UIAlertController使用 UIAlertController的宽度 为270

    添加子控件 1. 有标题, alert标题高度大概 是 40, 子控件的 Y一般在40 ,如果中间有换行, \n 的高度大概是30 2.alert的宽度 是270, 设置frame 的时候注意 /// ...

  9. SyntaxError: Non-ASCII character '\xe5' in file D:/pcode/xx.py on line 21, but no encoding declared

    from selenium import webdriver from datetime import * import time starttime = datetime.now() print ( ...

  10. MQ基础概念和介绍

    一.中间件 MQ是一种中间件产品,至于什么是中间件,中间件能干什么,参见以下链接: http://baike.baidu.com/view/23710.htm 二.WebSphere MQ的原理 We ...