(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 ...
随机推荐
- Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题
A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...
- delphi treeview 鼠标移动显示hint信息
procedure TForm1.TreeView1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var Nod ...
- thrift学习笔记
Thrift学习笔记 一:thrift介绍 Thrift是facebook开发的用来处理各不同系统之间数据通讯的rpc服务框架,后来成为apche的开源项目.thrift支持多种程序语言,包括Java ...
- mysql 5.1 到 mysql 5.2的出现的索引BTREE问题 use near 'USING BTREE
转自:http://hi.baidu.com/our_poll/item/669c5ce885b33ff1e0a5d4fc 我本机测试是安装的 mysql 5.1 , 但服务器上确是使用的 mysql ...
- JAVA反射机制学�
JAVA反射机制:对于随意一个类,都可以知道这个类的全部属性和方法:对于随意一个对象,都可以调用它的随意一个方法和属性:这样的动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制. J ...
- Android 滑动效果高级篇(八)—— 自定义控件
自定义控件,较常用View.ViewGroup.Scroller三个类,其继承关系如下: 本示例自定义控件,实现一个Gallery效果,并添加了一个显示View个数和位置的bar条,效果图: 自定义控 ...
- mysql 优化工具
explain profiling 建议提供以下信息 show table status like 'audit';show create table audit;show index from a ...
- Getting Started with Zend Framework MVC Applications
Getting Started with Zend Framework MVC Applications This tutorial is intended to give an introducti ...
- 小白日记9:kali渗透测试之主动信息收集(二)四层发现:TCP、UDP、nmap、hping、scapy
四层发现 四层发现的目的是扫描出可能存活的IP地址,四层发现虽然涉及端口扫描,但是并不对端口的状态进行精确判断,其本质是利用四层协议的一些通信来识别主机ip是否存在. 四层发现的优点: 1.可路由且结 ...
- 通过java实现对数据库的增删改查
package cn.hncu; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet; ...