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. 20145222黄亚奇《Java程序设计》第1周学习总结

    教材学习内容总结 BJVM是Java程序唯一认识的操作系统,其可执行文件为.class文档 Java的三大平台为Java SE,Java EE,Java ME. Java SE的四个部分为:JVM,J ...

  2. 记一次w3wp占用CPU过高的解决过程(Dictionary和线程安全)

    项目上线以来一直存在一个比较揪心的问题,和一个没有信心处理的BUG,那就是在应用程序启动时有可能会导致cpu跑满99%或持续在一个值如50%左右,这样一来对服务器的压力是非常大的,经常出现服务器无法远 ...

  3. 轻松理解JS基本包装对象

    今天来讨论一下JS中的基本包装对象(也叫基本包装类型),之前刚学到这里的时候,自己也是一头雾水,不明白这个基本包装对象到底是个什么鬼,后来找了很多资料,终于看清了它的真面目.首先呢,我们现在复习一下J ...

  4. Mongodb使用基本之——安装

    版本是:Mongodb 3.2.1 本来是想用python做个爬虫然后爬取一些数据放到数据库上的,想着想着以为NoSQL会很流行,就用了Mongodb,结果,一折腾真是不容易. 遇到的第一个问题:官网 ...

  5. ncp的简单实用

    'use strict';//这是一个简单的应用var Promise = require('bluebird');var ncp = require('ncp').ncp;var fs = requ ...

  6. groovyConsole — the Groovy Swing console

    1. Groovy : Groovy Console The Groovy Swing Console allows a user to enter and run Groovy scripts. T ...

  7. [Windows 64] (搬运)价值¥ 6,499的软件Navicat Premium11.2.11 最新版及其注册机

    Navicat Premium可以连接6种数据库并开发> 转载于:http://www.52pojie.cn/thread-529020-1-1.html

  8. nginx 下 location 配置解释

    当我们在使用负载均衡和反向代理的时候 我们会考到虚拟主机下面有着个配置 现在我们看一下反向代理的location 下面的配置实例: server { listen 80 ;    监听的端口号 ser ...

  9. Spring MVC框架

    这个Spring Web MVC 框架提供了模型视图控制器的架构,这种结构能够被用来开发灵活的和松耦合的Web应用程序. 这种MVC模式能够将应用程序分离成不同的层面,(输入逻辑,业务逻辑,UI逻辑) ...

  10. Tomcat tomcat-users.xml详解

    conf/tomcat-users.xml <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rol ...