Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7998   Accepted: 2675

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

最小生成树问题,主要是要求出点点距离,即字母间边权。这里用BFS求出了边权,用prim算法求出了最小生成树路径。

AC代码例如以下:

<pre name="code" class="cpp">#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#define inf 100000
using namespace std; char map[60][60];
int vis[60][60],v[60][60];
int n,m,tt,ans;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1}; struct H
{
int h,z;
}a[10005]; int w[251][251],lc[251],vv[60][60];//W是边权记录,LCprim算法运用,VV记录第几个字母的坐标
struct HH
{
int h,z,st;
}; int bfs(int h,int z)
{
int i;
v[h][z]=1;
queue <HH> q;
HH a,b,c;
a.h=h;
a.z=z;
a.st=0;
q.push(a);
while(!q.empty())
{
b=q.front();
q.pop();
if(vv[b.h][b.z])//统计边权
{
w[vv[h][z]][vv[b.h][b.z]]=b.st;
w[vv[b.h][b.z]][vv[h][z]]=b.st;
//cout<<vv[h][z]<<" "<<vv[b.h][b.z]<<" "<<b.st<<endl;
} for(i=0;i<4;i++)
{
c.h=b.h+dx[i];
c.z=b.z+dy[i];
if(c.h>=0&&c.h<n&&c.z>=0&&c.z<m&&map[c.h][c.z]!='#'&&!v[c.h][c.z])
{
v[c.h][c.z]=1;
c.st=b.st+1;
q.push(c);
}
}
}
} void prim()
{
int i,j;
int m,id;
for(i=1;i<tt;i++)
{
lc[i]=w[1][i];
} lc[1]=0;
for(j=1;j<tt-1;j++)
{
m=inf;
for(i=1;i<tt;i++)
if(lc[i]!=0&&lc[i]<m)
{m=lc[i];id=i;}
ans+=m;
lc[id]=0;
for(i=1;i<tt;i++)
{
if(lc[i]!=0&&lc[i]>w[id][i])
lc[i]=w[id][i];
}
} } int main()
{
int i,j,l;
int t;
char c[10];
scanf("%d",&t);
gets(c);
while(t--)
{
tt=1;
memset(vis,0,sizeof vis );
cin>>m>>n;
gets(c);
for(i=0;i<n;i++)
gets(map[i]);
//for(i=0;i<n;i++)
//cout<<map[i]<<endl;
int bj=0;
memset(vv,0,sizeof vv);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
if(map[i][j]=='A'||map[i][j]=='S')
{
vv[i][j]=tt++;//记录字母序号及个数
}
}
for(i=1;i<tt;i++)
for(j=1;j<=i;j++)
if(i==j)
w[i][j]=0;
else {
w[i][j]=inf;
w[j][i]=inf;
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if((map[i][j]=='A'||map[i][j]=='S')&&!vis[i][j])
{
vis[i][j]=1;
memset(v,0,sizeof v);
bfs(i,j);//求出这个字母到各个字母产生的边权
} } ans=0;
prim();//prim算法的运用
cout<<ans<<endl;
}
return 0;
}

POJ 3026 Borg Maze的更多相关文章

  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. POJ 3026 Borg Maze【BFS+最小生成树】

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

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

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

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

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

  5. poj 3026 Borg Maze 最小生成树 + 广搜

    点击打开链接 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7097   Accepted: 2389 ...

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

    Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...

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

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

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

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

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

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

随机推荐

  1. 2016计蒜之道复赛 百度地图的实时路况 floyd+cdq分治

    链接:https://nanti.jisuanke.com/t/11217 奉上官方题解: 枚举 d(x , y , z) 中的 y,把 y 从这个图中删去,再求这时的全源最短路即可,使用 Floyd ...

  2. ASIHTTPREQUEST 文档

    http://blog.csdn.net/ysysbaobei/article/details/17026577 Please note that I am no longer working on ...

  3. 【LR】版本问题

    前台信息工作笔记本系统是: widows7 64位操作系统 (1)loadrunner11 软件 --兼容性问题的解决与环境配置要求 地址:http://bgwan.blog.163.com/blog ...

  4. ASP.NET开发在JavaScript有中文汉字时出现乱码时简单有效的解决

    一般情况在使用ASP.NET开发使用JavaScript有中文汉字时不会出现乱码情况,比如:alert('您看到我了吗?');这样直接输入中文汉字的代码中是不会出现乱码的,如果出现了,一是检查Web. ...

  5. document.getElementsByClassName方法的重写(OVERRIDE)

    众所周知,对于IE8以下的浏览器(IE8居然是WIN7预装的)没有document.getElementsByClassName,网上也有很多重写的方法,以下是本人在项目中所使用的方法 documen ...

  6. AHOI2013 Round2 Day2 简要题解

    第一题: 第一问可以用划分树或主席树在O(nlog2n)内做出来. 第二问可以用树状数组套主席树在O(nlog2n)内做出来. 我的代码太挫了,空间刚刚卡过...(在bzoj上) 第二题: 分治,将询 ...

  7. UVALive 7456 Least Crucial Node (并查集)

    Least Crucial Node 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/C Description http://7 ...

  8. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  9. HDU2079选课时间(母函数)

    母函数的简单应用http://acm.hdu.edu.cn/showproblem.php?pid=2079 介绍见另一篇随笔HDU1028Ignatius and the Princess III( ...

  10. CodeForces 705C Thor (模拟+STL)

    题意:给定三个操作,1,是x应用产生一个通知,2,是把所有x的通知读完,3,是把前x个通知读完,问你每次操作后未读的通知. 析:这个题数据有点大,但可以用STL中的队列和set来模拟这个过程用q来标记 ...