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. liunx 用户切换 su sudo

    liunx 用户操作#useradd test#passwd test 用户身份切换su 切换用户 需要知道切换用户的密码1.su [-lm] [-c命令] [username] #su -login ...

  2. DFS POJ 3087 Shuffle'm Up

    题目传送门 /* 题意:两块扑克牌按照顺序叠起来后,把下半部分给第一块,上半部给第二块,一直持续下去,直到叠成指定的样子 DFS:直接模拟搜索,用map记录该字符串是否被搜过.读懂题目是关键. */ ...

  3. LeetCode 要记得一些小trick

    最近搞了几场编程比赛,面试题或者是LeetCode周赛.每次都不能做完,发现时间不够用. 看了别人的代码才知道,同样实现相同的功能,可能别人只需要用一个恰当的函数,就会比自己少些不少代码,争得了时间. ...

  4. BST二叉查找树转双向链表DoubleLinke

    问题:在不创建任何新的节点的情况下,实现将一颗BST变成有序的双向链表. 分析: 在结构上,如图的一颗BST,每个节点都有left right指针分别指指向左右儿子.结构上和双向链表节点是完全相同的. ...

  5. 393 UTF-8 Validation UTF-8 编码验证

    详见:https://leetcode.com/problems/utf-8-validation/description/ C++: class Solution { public: bool va ...

  6. redis+mysql读写方案

    前言:在web服务端开发的过程中,redis+mysql是最常用的存储解决方案,mysql存储着所有的业务数据,根据业务规模会采用相应的分库分表.读写分离.主备容灾.数据库集群等手段.但是由于mysq ...

  7. string与int的相互转换C++(转)

    string与int之间的相互转换C++(转) #include<iostream> #include<string> #include<sstream> usin ...

  8. Elasticsearch--扩展索引结构

    目录 索引树形数据 索引非扁平数据 索引关系型数据 使用嵌套对象 评分与嵌套查询 使用主从关系 索引树形数据 使用path_analyzer分析树形数据字段 索引非扁平数据 数据如下: { " ...

  9. 华硕(ASUS)X554LP笔记本在64位win7下无线网络连接问题

    还是那台华硕(ASUS)X554LP笔记本,无线网卡为 Qualcomm Atheros AR956x,某天换了个环境,WIFI(此处简称为WIFI网A)就连不上网了.手机.其它笔记本用无线连接都没问 ...

  10. JVM 参数含义

    JVM参数的含义 实例见实例分析 参数名称 含义 默认值   -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,J ...