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. canvas学习笔记:小小滴公式,大大滴乐趣

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 最近想弄一个网页,把自己学HTML5过程中做的部分DEMO放上去做集合,但是,如果就仅仅做个网页把所有DEMO一个一个排列又觉得太难看了. ...

  2. ASP Request.ServerVariables 参数集

    转自:http://blog.csdn.net/chinmo/article/details/2096871 Request.ServerVariables("Url") 返回服务 ...

  3. iOS 自定义控件开发(中)

    <iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> 接上篇iOS自定义控件开发之后,我们尝试另外一种. 在Xcode的右边,会看到如下的图 其中,上面有一个:C ...

  4. Daily English

  5. SVN快速入门(TSVN)

    作者: 北京群英汇信息技术有限公司 网址: http://www.ossxp.com/ 版本: 0.1-35 日期: 2011-07-05 10:51:59 版权信息: 目录 1   安装Tortoi ...

  6. 《TCP/IP详解卷1:协议》第17、18章 TCP:传输控制协议(1)-读书笔记

    章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...

  7. iOS -- 给model赋值时走了[self setValuesForKeysWithDictionary:dic]不走setvalue: forked:

    这是一个小坑, 看看你的BaseModel的便利构造器的方法: + (__kindof BaseModel *)modelWithDic:(NSDictionary *)dic { return [[ ...

  8. 微信内置浏览器的JsAPI(WeixinJSBridge续)[转载]

    原文地址:  http://www.baidufe.com/item/f07a3be0b23b4c9606bb.html 之前有写过几篇关于微信内置浏览器(WebView)中特有的Javascript ...

  9. git for windows 入门随笔

    引言: Git 是当前最流行的集中化的版本控制程序之一(版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订情况的系统),Git 只关心文件数据的整体是否发生变化,而大多数其他系统则只关心文件 ...

  10. u1-nav-css

    header:before, header:after ,.navigation:before, .navigation:after,.nav-row:before, .nav-row:after,. ...