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. MySQL更新时Error Code:1093和Error Code:1175的解决办法

    Error Code: 1093. You can't specify target table 'ws_product' for update in FROM clause 这个是我们在使用upda ...

  2. Appium官网Introduction

    英文官网:http://appium.io/introduction.html?lang=zh Appium 简介 Appium是一个开源的自动化测试工具,其支持iOS和安卓平台上的原生的,基于移动浏 ...

  3. [Javascript] Intro to the Web Audio API

    An introduction to the Web Audio API. In this lesson, we cover creating an audio context and an osci ...

  4. tomcat URL简写案例:模拟站点www.baidu.com的訪问

    tomcat URL简写案例:模拟站点  * 实际URL:http://www.baidu.com:8080/myweb/1.html  * 实际位置:F:\mywebapps\myweb\1.htm ...

  5. linux查看cpu温度

      分类: linux系统 一.安装  sudo apt-get install lm-sensors   二.查看 linux@cdyemail:~$ sensors k10temp-pci-00c ...

  6. [转] 多线程下变量-gcc原子操作 __sync_fetch_and_add等

    http://blog.sina.com.cn/s/blog_6f5b220601013zw3.html 非常好的原子操作,不用加锁:__sync_fetch_and_add GCC 提供的原子操作 ...

  7. App 启动加载广告页面思路

    需求 很多app(如淘宝.美团等)在启动图加载完毕后,还会显示几秒的广告,一般都有个跳过按钮可以跳过这个广告,有的app在点击广告页之后还会进入一个广告页面,点击返回进入首页.今天我们就来开发一个广告 ...

  8. 安卓扫码:简单的ZXing使用记录

    ZXing是Google提供的条形码.二维码等的生成.解析的库.最近工作需求去研究了一下,主要是研究怎么扫描二维码(QRCode).网上教程也不少,但大多看了不明所以,甚至看了半天都不知道解码到底从哪 ...

  9. Linux开发工具之Makefile(上)

    二.makefile(上) 01.make工具   利用make工具可以自动完成编译工作.这些工作包括:如果修改了某几 个源文件,则只重装新编译这几个源文件:如果某个头文件被修改了,则 重新编译所有包 ...

  10. 地址栏访问Action,后来方法执行两次

    SSH框架,在地址栏输入URL访问Action,后台访问会访问两次.很奇怪. 经排查,最终问题在于方法名称写错了.将getOpinionByPN()修改成queryOpinionByPN(),没有问题 ...