Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6971   Accepted: 2345

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
 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int INF = 0x3f3f3f3f;
int dis[][],vis[][];
int m,n,cnt;
char map[][];
struct node
{
int x,y;
int id;
}cor[];
struct sear
{
int x,y;
int step;
};
queue <sear> que; int bfs(int x, int y,int ex, int ey)
{
memset(vis,,sizeof(vis));
while(!que.empty())
que.pop();
que.push((struct sear){x,y,});
vis[x][y] = ; while(!que.empty())
{
struct sear u = que.front();
que.pop();
if(u.x == ex && u.y == ey)
return u.step;
if(map[u.x-][u.y] != '#' && !vis[u.x-][u.y])
{
vis[u.x-][u.y] = ;
que.push((struct sear){u.x-,u.y,u.step+});
}
if(map[u.x+][u.y] != '#' && !vis[u.x+][u.y])
{
vis[u.x+][u.y] = ;
que.push((struct sear){u.x+,u.y,u.step+});
}
if(map[u.x][u.y-] != '#' && !vis[u.x][u.y-])
{
vis[u.x][u.y-] = ;
que.push((struct sear){u.x,u.y-,u.step+});
}
if(map[u.x][u.y+] != '#' && !vis[u.x][u.y+])
{
vis[u.x][u.y+] = ;
que.push((struct sear){u.x,u.y+,u.step+});
}
}
}
int prim_dis[];
int prim_vis[];
int prim(int id)
{
int i,j;
int ans = ;
memset(prim_vis,,sizeof(prim_vis));
prim_vis[id] = ;
for(i = ; i < cnt; i++)
prim_dis[i] = dis[id][i];
for(i = ; i < cnt; i++)
{
int min = INF,pos;
for(j = ; j < cnt; j++)
{
if(prim_dis[j] < min && !prim_vis[j])
{
min = prim_dis[j];
pos = j;
}
}
prim_vis[pos] = ;
ans += min;
for(j = ; j < cnt; j++)
{
if(!prim_vis[j] && prim_dis[j] > dis[pos][j])
prim_dis[j] = dis[pos][j];
}
}
return ans;
} int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
cnt = ;
scanf("%d %d",&m,&n);
char space[];
gets(space);
for(i = ; i < n; i++)
{
gets(map[i]);
for(j = ; j < m; j++)
{
if(map[i][j] == 'A' || map[i][j] == 'S')
cor[cnt++] = ((struct node){i,j,cnt});
}
}
for(i = ; i < cnt; i++)
{
for(j = ; j < cnt; j++)
{
if(i == j) dis[i][j] = ;
else dis[i][j] = INF;
}
}
for(i = ; i < cnt; i++)
{
for(j = i+; j < cnt; j++)
{
dis[i][j] = dis[j][i] = bfs(cor[i].x,cor[i].y,cor[j].x,cor[j].y);
}
}
printf("%d\n",prim());
}
return ;
}

Borg Maze(bfs+prim)的更多相关文章

  1. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  2. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

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

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

  4. POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  5. POJ 3026 Borg Maze(Prim+bfs求各点间距离)

    题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...

  6. POJ3026 Borg Maze(bfs求边+最小生成树)

    Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...

  7. POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

  8. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  9. poj 3026 Borg Maze (bfs + 最小生成树)

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

随机推荐

  1. TCP具体解释(3):重传、流量控制、拥塞控制……

    传输数据 在TCP的数据传送状态.非常多重要的机制保证了TCP的可靠性和强壮性.它们包括:使用序号.对收到的TCP报文段进行排序以及检測反复的数据:使用校验和来检測报文段的错误.使用确认和计时器来检測 ...

  2. java 线程三种实现方式

    1继承thread public class MultiThread1 extends Thread{ public void run(){ for(int i=0; i<7; i++){ Sy ...

  3. Android(java)学习笔记243:多媒体之视频播放器

    1.这里我们还是利用案例演示视频播放器的使用: (1)首先,我们看看布局文件activity_main.xml,如下: <RelativeLayout xmlns:android="h ...

  4. sublime text3 插件配置

    (转) sublme text 全程指引:http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html 使用Package Con ...

  5. python之路,Day24 常用设计模式学习

    python之路,Day24 常用设计模式学习   本节内容 设计模式介绍 设计模式分类 设计模式6大原则 1.设计模式介绍 设计模式(Design Patterns) --可复用面向对象软件的基础 ...

  6. (转)php中global和$GLOBALS[]的分析之一

    PHP 的全局变量和 C 语言有一点点不同,在 C 语言中,全局变量在函数中自动生效,除非被局部变量覆盖     这可能引起一些问题,有些人可能漫不经心的改变一个全局变量.PHP 中全局变量在函数中使 ...

  7. 想加入一行代码吗?使用<code>标签

    在介绍语言技术的网站中,避免不了在网页中显示一些计算机专业的编程代码,当代码为一行代码时,你就可以使用<code>标签了,如下面例子: <code>var i=i+300;&l ...

  8. 学会用Clang来进行内存泄露分析

    最近项目出现了内存泄露的问题,对于PC x86平台来说,一点点的内存泄露往往不会出错,很难进行debug调试.这个时候我们可以用到苹果给我们带来的神器--Clang编译器来进行内存泄露分析检测,开关打 ...

  9. shell每日发邮件

    LOGFILE="$fank/"`date +"%Y%m%d"`"data"#每日文件 from="abc@123.com&quo ...

  10. 2.2.4 转换 Path

    在NIO.2里可以很容易地合并Path,在两个Path中再创建Path或对Path进行比较: Demo: import java.nio.file.Path; import java.nio.file ...