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. Haskell语言学习笔记(57)Parsec(4)

    Parser 类型 data ParsecT s u m a type Parsec s u = ParsecT s u Identity type Parser = Parsec String () ...

  2. Kotlin语言学习笔记(5)

    委托模式(Delegation) 类的委托 interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fu ...

  3. 全屏幕显示AVI

    uses Unit2; procedure TForm1.Button1Click(Sender: TObject);begin  Form2.Show;  Form2.WindowState := ...

  4. Gson转换时,Double转式化

    package com.mall.core; import java.lang.reflect.Type; import java.text.DecimalFormat; import com.goo ...

  5. ingress 密码验证

    traefik ingress 上面的方式需要引入haprox或者nginx,多引入了一个代理转发层,其实ingress本身就提供了basic auth的支持,在ingress规则中添加额外的认证an ...

  6. cmd 命令相关

    计算相关进程数: tasklist|find /i "cmd.exe" 安装git的可以 tasklist|find /i "cmd.exe"  | wc -l

  7. eclispe 通过git向码云上传

    本文将介绍如何将本地的项目提交到开源中国上去,过程比较详细,实现起来很简单.由于自己也算是一个新手,所以没有做过多的解释,只是单纯的描述了该如何去做. 1.在开源中国上面新建一个空项目 到这里也就结束 ...

  8. webpack 构建同时适用于手机和电脑的调试服务器

    plugins plugins: [ new HtmlWebpackPlugin({ // 使用模板同时生成 pc.html和mobile.html title: 'pc', filename: 'p ...

  9. (转) Ringbuffer为什么这么快?

    原文地址:http://ifeve.com/ringbuffer/ 最近,我们开源了LMAX Disruptor,它是我们的交易系统吞吐量快(LMAX是一个新型的交易平台,号称能够单线程每秒处理数百万 ...

  10. Intersection(Check)

    Intersection http://poj.org/problem?id=1410 Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...