(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 ...
随机推荐
- C语言调用Lua函数
记得上学时,初中英文课本中,上网叫做surfing the internet,中文叫网上冲浪. 那个时期,人们常常称互联网为赛博空间.现在工作了,大量的零碎时间用于上微博,知乎,QQ.这些碎片化的阅读 ...
- [IOS]自定义长触屏事件
写一个Demo来自定义一个长触屏事件,自定义长按手势. 实现步骤: 1.创建一个自定义手势类,命名为LongPressGestureRecognizer,在创建的时候继承UIGestureRecogn ...
- [ES6] 18. Map
ES6 provides Map, it is a set of k-v pair. Key can be number, string, object, function and even unde ...
- Jetty学习(一)
最近做一个项目,需要动态添加与移除servlet容器的http端口,并且启动都是嵌入式的.因此,果断选择了Jetty. 在模块化方面,Jetty是做的相当给力的一个容器,对 ...
- UNIX标准化及实现之POSIX标准必需头文件
POSIX标准定义的必需头文件 头文件 说明 <dirent.h> 目录项 <fcntl.h> 文件控制 <fnmatch.h> 文件名匹配类型 <glob. ...
- A beginner’s guide to Cache synchronization strategies--转载
原文地址:http://vladmihalcea.com/2015/04/20/a-beginners-guide-to-cache-synchronization-strategies/ Intro ...
- 嵌入式 Linux 应用:概述
转载:http://www.ibm.com/developerworks/cn/linux/embed/embl/overview/index.html 从腕表到基于群集的超级计算机 在对嵌入式 ...
- 琐碎-hadoop2.2.0伪分布式和完全分布式安装(centos6.4)
环境是centos6.4-32,hadoop2.2.0 伪分布式文档:http://pan.baidu.com/s/1kTrAcWB 完全分布式文档:http://pan.baidu.com/s/1s ...
- Swift 3必看:新的访问控制fileprivate和open
在swift 3中新增加了两中访问控制权限 fileprivate和 open.下面将对这两种新增访问控制做详细介绍. fileprivate 在原有的swift中的 private其实并不是真正的私 ...
- 还在用GCD?来看看NSOperation吧
在iOS开发中,谈到多线程,大家第一时间想到的一定是GCD.GCD固然是一套强大的多线程解决方案,能够解决绝大多数的多线程问题,但是他易于上手难于精通且到处是坑的特点也注定了想熟练使用它有一定的难度. ...