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 对所有S和A建立统统BFS一遍,建立邻接矩阵,然后Prim算法,这是第一次错误代码。没找出哪里错了。。。。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 101
#define INF 0x3f3f3f3f
int dis[MAXN][MAXN],lowcost[MAXN],m,n,tol,dir[][]={{,},{-,},{,},{,-}};
char g[MAXN][MAXN];
bool been[MAXN][MAXN];
struct node
{
int x,y;
};
struct qnode
{
int x,y,step;
};
node a[MAXN];
void BFS(int x,int y,int i)
{
memset(been,false,sizeof(been));
qnode tmp;
tmp.x = x;
tmp.y = y;
tmp.step = ;
been[x][y] = true;
queue<qnode> q;
q.push(tmp);
while(!q.empty())
{
tmp = q.front();
q.pop();
if(g[tmp.x][tmp.y]>=''&&g[tmp.x][tmp.y]<='')
{
dis[i][g[tmp.x][tmp.y]-''] = tmp.step;
}
for(int i=;i<;i++)
{
int tx = tmp.x+dir[i][],ty = tmp.y+dir[i][];
if(tx>=&&ty>=&&tx<n&&ty<m&&g[tx][ty]!='#'&&!been[tx][ty])
{
been[tx][ty] = true;
qnode n_node;
n_node.x = tx;
n_node.y = ty;
n_node.step = tmp.step+;
q.push(n_node);
}
}
}
}
int Prim()
{
int ret = ;
bool vis[MAXN];
memset(vis,false,sizeof(vis));
for(int i=;i<tol;i++)
lowcost[i] = dis[][i];
lowcost[] = ;
vis[] = true;
for(int j=;j<tol;j++)
{
int Minc = INF,k=-;
for(int i=;i<tol;i++)
{
if(!vis[i]&&lowcost[i]<Minc)
{
k = i;
Minc = lowcost[i];
}
}
if(k==-) return -;
vis[k] = true;
ret+=Minc;
for(int i=;i<tol;i++)
{
if(!vis[i]&&dis[k][i]<lowcost[i])
lowcost[i] = dis[k][i];
}
}
return ret;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>m>>n;//n行m列
tol = ;
string str;
for(int i=;i<n;i++)
{
getline(cin,str);
if(str.empty())
{
i--;
continue;
}
for(int j=;j<m;j++)
{
g[i][j] = str[j];
if(str[j]=='A'||str[j]=='S')
{
g[i][j] = ''+tol;
a[tol].x = i;
a[tol++].y = j;
}
}
}
for(int i=;i<tol;i++)
for(int j=;j<tol;j++)
dis[i][j] = INF;
for(int i=;i<tol;i++)
BFS(a[i].x,a[i].y,i);
int ans = Prim();
cout<<ans<<endl;
}
return ;
}

最小生成树+BFS J - Borg Maze的更多相关文章

  1. J - Borg Maze

    J - Borg Maze 思路:bfs+最小生成树. #include<queue> #include<cstdio> #include<cstring> #in ...

  2. J - Borg Maze - poj 3026(BFS+prim)

    在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...

  3. poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9718   Accepted: 3263 Description The B ...

  4. J - Borg Maze +getchar 的使用注意(二维字符数组的输入)

    题目链接: https://vjudge.net/contest/66965#problem/J 具体思路: 首先将每个点之间的最短距离求出(bfs),A 或者 S作为起点跑bfs,这样最短距离就求出 ...

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

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

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

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

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

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

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

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

  9. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

随机推荐

  1. php可以定义数组的常量吗

    是这样吗?<?php define('BEST_PHPER',array('name'=>'巩文','address'=>'china')); My God,明确告诉你不可以:原因是 ...

  2. 洛谷 P1037 产生数

    题目描述 给出一个整数n(n<10^30)和k个变换规则(k≤15). 规则: 一位数可变换成另一个一位数: 规则的右部不能为零. 例如:n=234.有规则(k=2): 2->53-> ...

  3. 反向代理与Real-IP和X-Forwarded-For(转)

    如下图所示,客户端通过Nginx Proxy1 和 Nginx Proxy2 两层反向代理才访问到具体服务Nginx Backend(或如Tomcat服务).那Nginx Backend如何才能拿到真 ...

  4. C++小项目-吃豆子游戏

    GMap.h #pragma once //保证头文件只被编译一次 #include "stdafx.h" #define MAP_LEN 19 //逻辑地图大小 (逻辑地图由行. ...

  5. Linux C编程 GCC的使用

    本篇文章面向一些会linux文件操作与vim基本命令,编程大佬请移步勿喷. gcc是默认安装的,但是其还缺少常用的头文件和库文件,所以还需要build-essential这个包,可以在联网状态下使用如 ...

  6. CentOS6.5磁盘分区和挂载操作记录

    CentOS6.5磁盘分区和挂载操作记录. [root@CentOS ~]# fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders ...

  7. 几个不同的tab切换示例

    上一篇<论tab切换的几种实现方法>中讲了tab切换的4种不同实现原理,那么,现在到理论联系实际的时候了,下面就写几个实例. 一.仿”中国人民大学“官网的tab切换,背景是图片,效果图如下 ...

  8. js文件中引用其他js文件

    这一个功能的作用是做自己的js包时,可以通过引入一个整体的js文件而引入其他js. 只需要在总体的js加上这一句话 document.write("<script type='text ...

  9. 左耳听风 ARTS Week 001

    要求:1.每周至少做一个 leetcode 的算法题 2.阅读并点评至少一篇英文技术文章 3.学习至少一个技术技巧 4.分享一篇有观点和思考的技术文章 1.每周至少做一个 leetcode 的算法题 ...

  10. 世界上最受欢迎的10个Linux发行版

    帮助新的Linux用户在越来越多的Linux发行版中选择最合适的操作系统,是创建这个网页的原因.它列出了迄今为止最流行的10个Linux发行版(另外增加的是FreeBSD,到目前为止最为流行的BSD系 ...