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. C# GC 垃圾回收机制

    今天来谈谈C# 的GC ,也就是垃圾回收机制,非常的受教,总结如下 首先:谈谈托管,什么叫托管,我的理解就是托付C# 运行环境帮我们去管理,在这个运行环境中可以帮助我们开辟内存和释放内存,开辟内存一般 ...

  2. 『转载』C# winform 中dataGridView的重绘(进度条,虚线,单元格合并等)

    原文转载自:http://hi.baidu.com/suming/item/81e45b1ab9b4585f2a3e2243 最近比较浅的研究了一下dataGridView的重绘,发现里面还是有很多东 ...

  3. [转]简单识别 RESTful 接口

         本文描述了识别一个接口是否真的是 RESTful 接口的基本方法.符合 REST 架构风格的接口,称为 RESTful 接口.本文不打算从架构风格的推导方面描述,而是从 HTTP 标准的方面 ...

  4. css的img移上去边框效果及CSS透明度

    css的img移上去边框效果: .v_comment img{ height:36px; height:36px; float:left; padding:1px; margin:2px; borde ...

  5. Javascript基础系列之(八)Javascript的调试与优化

    Javascript的错误主要是语法错误和运行时的错误,前者在代码解析时就会出错,影响程序的运行.后者称为异常,影响它所运行的线程.下面就Javascript常见错误进行分析 1.常见的错误和异常 i ...

  6. abstract和接口

    接口只包含常量和抽象方法,不能实例化. abstract: 1.抽象类不能实例化, 2.可以没有抽象方法.但有了抽象方法,一定要被定义为抽象类. 3.子类没有实现父类中所有的抽象方法.子类也必须定义为 ...

  7. Apache MINA(一)

    Apache MINA is a network application framework which helps users develop high performance and high s ...

  8. 匿名函数自调用(IIFE)

    什么是匿名函数 Javascript中定义函数的方式有多种,函数直接量就是其中一种.如var fun = function(){},这里function如果不赋值给fun那么它就是一个匿名函数.好,看 ...

  9. js常用插件

    1.jQuery Shortcuts 是个超轻量级的方法,使用 jQuery 来绑定快捷键(热键). 2.Underscore封装了常用的JavaScript对象操作方法,用于提高开发效率. 3.Kn ...

  10. oracle基本语句

    ALTER TABLE SCOTT.TEST RENAME TO TEST1--修改表名 ALTER TABLE SCOTT.TEST RENAME COLUMN NAME TO NAME1 --修改 ...