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. Android 省市县 三级联动(android-wheel的使用)[转]

    转载:http://blog.csdn.net/lmj623565791/article/details/23382805 今天没事跟群里面侃大山,有个哥们说道Android Wheel这个控件,以为 ...

  2. python 学习笔记 基础

    python对象三要素: identity(值):对应于内存的地址,不可修改type(类型):不可修改value(值): mutable :可以修改 immutable:不可以修改 引用计数当引用计数 ...

  3. 百度地图api之如何自定义标注图标

    在百度地图api中,默认的地图图标是一个红色的椭圆形.但是在项目中常常要求我们建立自己的图标,类似于我的这个 操作很简单,分如下几步进行 步骤一:先ps一个图标,大小要合适,如果要背景透明的,记得保存 ...

  4. .net(全局文件,错误页,静态页,IIS配置及防黑)

    添加全局应用程序类. <%@ Application Language="C#" %> <script runat="server"> ...

  5. tomcat发布去掉项目的名称

    1.找到tomcat的文件夹,打开conf目录的server.xml,我的是Linux系统所以用vi server.xml,总之打开就行 2.找到如下语句 加入下面箭头的路径,docBase为项目所在 ...

  6. ASP.NET-FineUI开发实践-16(二)

    实现那还差点,在事件参数里我传了一个boolall选中状态参数,这个参数由前台给的,RowSelect 传的是index 行号,就是改这,通过$符号来分开的, if (commandArgs.Leng ...

  7. dede当前位置各种写法

    方法一.Dedecms当前位置{dede:field name='position'/} 方法二.dede:field name='position' runphp='yes'}          $ ...

  8. PHP简单计算器

    工作之余,写着玩的. <?php $num1 = $_POST['num1']; $num2 = $_POST['num2']; $yusuan = $_POST['yusuan']; $jie ...

  9. 全志A10_linux3.0内核编译记录

    第一次尝试编译时按照 Android A10(4.0.3)系统编译详解.pdf 编译打包出来的镜像可用,但是内核没有生成Module.symvers .后来进一步研究才知道 ./build.sh 脚本 ...

  10. 【转载】ASP.NET页面运行机制以及请求处理流程

    本文转至 ASP.NET页面运行机制以及请求处理流程 IIS处理页面的运行机制 IIS自身是不能处理像ASPX扩展名这样的页面,只能直接请求像HTML这样的静态文件,之所以能处理ASPX这样扩展名的页 ...