Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9220   Accepted: 3087

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

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int col , row , cnt;
char maze[][] ;
int l[][] ;
int vis[][] ;
int map[][] ;
const int inf = 0x3f3f3f3f ;
int move[][] = { , , , , - , , , -} ; void bfs (int sx , int sy)
{
queue <pair <int , int> > q ;
while (!q.empty ())
q.pop () ;
memset (vis , - , sizeof(vis)) ;
vis[sx][sy] = ;
q.push (make_pair(sx , sy)) ;
while (!q.empty ()) {
pair <int , int> k = q.front () ;
q.pop () ;
if (l[k.first][k.second] != -)
map [l[sx][sy]] [l[k.first][k.second]] = vis [k.first][k.second] ;
for (int i = ; i < ; i++) {
int tx = k.first + move[i][] ;
int ty = k.second + move[i][] ;
if (maze[tx][ty] == '#' || vis[tx][ty] != -)
continue ;
vis[tx][ty] = vis[k.first][k.second] + ;
q.push (make_pair(tx , ty)) ;
}
}
} void prim ()
{
int p[] , d[] ;
for (int i = ; i < cnt ; i++) {
d[i] = map[][i] ;
p[i] = ;
}
d[] = ;
int ans = ;
for (int i = ; i < cnt - ; i++) {
int minc = inf , k ;
for (int j = ; j < cnt ; j++) {
if (d[j] && d[j] < minc) {
minc = d[j] ;
k = j ;
}
}
d[k] = ;
for (int j = ; j < cnt ; j++) {
if (d[j] && d[j] > map[k][j]) {
d[j] = map[k][j] ;
p[j] = k ;
}
}
ans += minc ;
}
printf ("%d\n" , ans) ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
int T ;
scanf ("%d" , &T) ;
while (T--) {
scanf ("%d%d" , &col , &row) ;
gets(maze[]) ;
int tol = ;
memset (l , - , sizeof(l)) ;
for (int i = ; i < row ; i++) {
gets (maze[i]) ;
for (int j = ; j < col ; j++) {
if (maze[i][j] == 'A' || maze[i][j] == 'S') {
l[i][j] = tol++ ;
}
}
}
for (int i = ; i < row ; i++) {
for (int j = ; j < col ; j++) {
if (l[i][j] != -) {
bfs (i , j);
}
}
}
cnt = tol ;
prim () ;
}
return ;
}

这道题有巨坑,收空格一定要用gets , 我用getchar RE了一个下午

Borg Maze(MST & bfs)的更多相关文章

  1. Borg Maze(BFS+MST)

    Borg Maze http://poj.org/problem?id=3026 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  2. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  3. POJ 3026 Borg Maze(bfs+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6634   Accepted: 2240 Descrip ...

  4. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

  5. POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16625   Accepted: 5383 Descri ...

  6. POJ3026 Borg Maze(Prim)(BFS)

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

  7. POJ 3026 Borg Maze【BFS+最小生成树】

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

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

    题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced ...

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

    有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...

随机推荐

  1. 配置sonar、jenkins进行持续审查

    本文以CentOS操作系统为例介绍Sonar的安装配置,以及如何与Jenkins进行集成,通过pmd-cpd.checkstyle.findbugs等工具对代码进行持续审查. 一.安装配置sonar ...

  2. Orchard用LiveWriter写博客

    本文链接:http://www.cnblogs.com/souther/p/4544241.html Orchard本身提供一个内建的管理面板来写博客,许多人更喜欢采用客户端提交的方式,例如Windo ...

  3. jQuery Easy UI 开发笔记

    1.jQuery Easy UI主要的运行原理是通过核心的代码调用插件来实现UI效果的 2.jQuery Easy UI插件与插件之间的关系是: 一.独立式插件: 独立式插件是指:不与其他的插件具有相 ...

  4. JS闭包文章--(翻译)Callbacks in Loops

    原文地址:http://tobyho.com/2011/11/02/callbacks-in-loops/ 某些时候,你需要在循环里创建一个回调函数.我们来试试给页面里每个链接增加点击事件. var ...

  5. linq查询语句转mongodb

    && (与操作) 子表达式可以通过&&合并来查询满足所有子表达式的文档 var query = from c in collection.AsQueryable< ...

  6. [BZOJ 2186][Sdoi2008]沙拉公主的困惑(欧拉函数)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2186 分析: 就是要求1~n!中与m!互质的数的个数 首先m!以内的就是φ(m!) 关 ...

  7. go语言的模板,text/template包

    go语言的模板,text/template包 定义 模板就是将一组文本嵌入另一组文本里 传入string--最简单的替换 package main import ( "os" &q ...

  8. “耐撕”团队 2016.04.05 站立会议

    1. 时间: 20:10--20:25  共计15分钟. 2. 成员: Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:ht ...

  9. 软工实践练习——使用Git进行代码管理

    GITHUB上的预备活动: 注册 创建小组Organization,邀请组员进来 将代码库fork到小组Organization底下 下载并使用GIT: Git的安装 使用Git进行代码管理 1.从百 ...

  10. 执行quartz报错java.lang.NoClassDefFoundError: javax/transaction/UserTransaction

    使用maven ,可以在 http://mvnrepository.com 中去查找 pom 配置如何写 <!-- https://mvnrepository.com/artifact/org. ...