POJ 3026 Borg Maze
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7998 | Accepted: 2675 |
Description
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
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
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的更多相关文章
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16625 Accepted: 5383 Descri ...
随机推荐
- MyBatis学习 之 四、MyBatis配置文件
目录(?)[-] 四MyBatis主配置文件 properties属性 settings设置 typeAliases类型别名 typeHandlers类型句柄 ObjectFactory对象工厂 pl ...
- Ch04-文字列表的设计
Ch04: 文字列表的设计 4.1 编号列表 语法: <OL> <li>编号1 <li>编号2 ...... </OL> 属性: 编号类型:type=1 ...
- [Irving]字符串相似度-字符编辑距离算法(c#实现)
编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字 ...
- C ~ 指针函数与函数指针的区别
一. 在学习arm过程中发现这“指针函数”与“函数指针”容易搞错,所以今天,我自己想一次把它搞清楚,找了一些资料,首先它们之间的定义: 1.指针函数是指带指针的函数,即本质是一个函数.函数返回类型是某 ...
- 如何给10^7个数据量的磁盘文件排序--bitset
题目: 输入:给定一个文件,里面最多含有n个不重复的正整数(也就是说可能含有少于n个不重复正整数),且其中每个数都小于等于n,n=10^7.输出:得到按从小到大升序排列的包含所有输入的整数的列表. 分 ...
- HDU5780 gcd 欧拉函数
http://acm.hdu.edu.cn/showproblem.php?pid=5780 BC #85 1005 思路: 首先原式化简:x^gcd(a,b)−1 也就是求n内,(公约数是i的 ...
- 简易CSS3 Tab菜单 Tab切换滑块动画
今天要分享一款简易的CSS3 Tab菜单,这款Tab菜单在切换的时候内容块出现飞入飞出的动画效果,尽管看起来非常简单,但是你完全可以在上面定制自己喜欢的Tab菜单.前面也分享过一些Tab菜单,像CSS ...
- Hbase 基本命令
启动Hbase:./start-hbase.sh 进入Hbase shell控制台:./hbase shell 查看所有表 list 查看数据库状态:status 查看数据库版本:vers ...
- Windows 窗体—— 键盘输入工作原理
方法 注释 PreFilterMessage 此方法在应用程序级截获排队的(也称为已发送的)Windows 消息. PreProcessMessage 此方法在 Windows 消息处理前在窗体和控件 ...
- HDU-4738 Caocao's Bridges 边联通分量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:在有重边的无向图中,求权值最小的桥. 注意trick就好了,ans为0时输出1,总要有一个 ...