(POJ 3026) Borg Maze 最小生成树+bfs
题目链接:http://poj.org/problem?id=3026、
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 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 =++.
Input On the first line of input there is one integer, N <= , giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that <= x,y <= . 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 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 #####
#A#A##
# # A#
#S ##
##### #####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
题意:从S点开始出发的机器人要把所有的A点同化,同化一个A后,A会变成S,继续同化A,问最短路径把A同化完
方法:先对每个S点与A点编号,再用bfs对每个点搜索,算出从这个点到别的所有的点的距离,最后就是求最小生成树
#include<stdio.h>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include <stack>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a))
#define mod 2147493647
#define N 100
int dir[][]= {{,},{,},{,-},{-,}};
char str[][];
int Map[][],vis[],dis[],vi[][];
int s[][],n,m;
struct node
{
int x,y,s;
};
void dfs(int x,int y)
{
met(vi,);
queue<node>Q;
node q,p;
q.x=x;
q.y=y;
q.s=;
Q.push(q);
vi[x][y]=;
int f=s[x][y];
while(Q.size())
{
q=Q.front();Q.pop(); if(str[q.x][q.y]>='A' && str[q.x][q.y]<='Z')
{
int e=s[q.x][q.y];
Map[f][e]=q.s;
}
for(int i=; i<; i++)
{
p.x=q.x+dir[i][];
p.y=q.y+dir[i][];
p.s=q.s+;
if(p.x< || p.x>=m || p.y< || p.y>=n)
continue;
if(s[p.x][p.y]>= && str[p.x][p.y]!='#'&&!vi[p.x][p.y])
{
Q.push(p);
vi[p.x][p.y]=;
}
}
}
}
int prim(int nn)
{
int ans=;
for(int i=; i<=nn; i++)
{
dis[i]=Map[][i];
vis[i]=;
}
vis[]=;
for(int i=; i<nn; i++)
{
int an=INF,k;
for(int j=; j<=nn; j++)
{
if(!vis[j] && an>dis[j])
an=dis[k=j];
}
ans+=an;
vis[k]=;
for(int j=; j<=nn; j++)
{
if(!vis[j])
dis[j]=min(dis[j],Map[k][j]);
}
}
return ans;
}
int main()
{
int t; scanf("%d",&t);
while(t--)
{
scanf("%d %d ",&n,&m);
int k=;
met(s,);
for(int i=; i<m; i++)
{
gets(str[i]);
for(int j=; j<n; j++)
{
if(str[i][j]>='A' && str[i][j]<='Z')
s[i][j]=k++;
}
}
for(int i=; i<m; i++)
{
for(int j=; j<=n; j++)
if(str[i][j]>='A' && str[i][j]<='Z')
dfs(i,j);
}
printf("%d\n",prim(k-));
}
return ;
}
(POJ 3026) Borg Maze 最小生成树+bfs的更多相关文章
- 快速切题 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)
有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...
- 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 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16625 Accepted: 5383 Descri ...
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- 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加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
随机推荐
- 自定义的带tab的可左右滑动的viewpager之二viewpager与fragment不兼容
总的来说,这个TAB用起来还算方便吧 不过随着用的地方多起来,发现了一些问题,比如下面这个界面: TAB1和TAB2都是表单,保存按钮对两个TAB都有效:若当前显示TAB1,点击保存则保存TAB1的f ...
- 【剑指offer】近期公共祖先
转载请注明出处:http://blog.csdn.net/ns_code/article/details/28113959 剑指offer上的最后一题了,一个递归函数调了一下午,才得到正确的结果. 题 ...
- SiteMesh学习笔记
SiteMesh是一个轻量级的web应用框架,实现了Decorator模式.它的目标是将多个页面都将引用的jsp页面通过配置加载到相应的jsp文件中. 在我们的项目中,每个jsp都需要添加两个top和 ...
- 数据恢复软件使用经验-支持U盘,手机SD卡,硬盘数据,解决图片恢复后打不开的问题
数据恢复软件使用经验-支持U盘,手机SD卡,硬盘数据.解决图片恢复后打不开的问题. 用过非常多数据恢复软件.最早EasyRecovery pro.恢复过U盘.手机SD卡,硬盘数据.但如今下载不了最新版 ...
- StarlingMVC Framework 原理。。。
向starlingmvc 中添加bean后..会根据Metadata标签,分别交给不同的Processor去处理...然后会执行每个bean的postConstruct函数.相当于初始化函数...可以 ...
- mysql升级小结和mysql_upgrade的用途
http://blog.itpub.net/15480802/viewspace-1412259/ mysql升级 1 升级方式 分为In-place和out-of-place,前者直接覆盖当前版本, ...
- php使用技巧--之链接地址
高效PHP程序必知的53个技巧 http://developer.51cto.com/art/201105/265953.htm 非常实用 十个PHP高级应用技巧 http://developer.5 ...
- SpringMVC中使用Jcaptcha实现校验码验证
SpringMVC中使用Jcaptcha实现校验码验证:http://www.tuicool.com/articles/rMzAFj 本文将使用Jcaptcha实现校验码验证,并演示在Spring/S ...
- SQL Server :事务和锁
1.事务 事务概念:全部执行或全部不执行的一条或者多条语句的组合 例子说明:到银行里转账,将一个账户(Tom)里的100元钱转到另一个账户(Jake) update table money=money ...
- CSS的总结(选择器,伪类等...)
关于组合选择器可参考:http://www.cnblogs.com/starof/p/4453458.html 主要内容 CSS概述 CSS和HTML结合的四种方式 CSS样式优先级和代码规范 CSS ...