题目链接: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的更多相关文章

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

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

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

    有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...

  3. POJ 3026 Borg Maze【BFS+最小生成树】

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

  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(bfs,最小生成树,英语题意题,卡格式)

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

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

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

  7. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

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

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

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

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

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

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

随机推荐

  1. codeforces Gym 100500C D.Hall of Fame 排序

    Hall of Fame Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachmen ...

  2. 返回ListBox选中的多项目

    //返回ListBox选中的多项目 procedure TForm1.Button2Click(Sender: TObject);vari:Integer;s:string;begin   for i ...

  3. JAVA中调用CMD命令,并输出运行结果

    package com.wzw.util; import java.io.BufferedReader; import java.io.InputStreamReader; public class ...

  4. iOS开发——图形编程OC篇&OpenGL ES2.0编程步骤

    OpenGL ES2.0编程步骤 OpenGL ES (OpenGL for Embedded Systems) 是 OpenGL 三维图形 API 的子集,针对手机.PDA和游戏主机等嵌入式设备而设 ...

  5. Percona-Galera-Monitoring-Template监控模板说明

    http://blog.chinaunix.net/uid-16844903-id-4054635.html 官网链接:http://www.percona.com/doc/percona-monit ...

  6. Python拼接多张图片

    写机器学习相关博文,经常会碰到很多公式,而Latex正式编辑公式的利器.目前国内常用的博客系统,好像只有博客园支持,所以当初选择落户博客园.我现在基本都是用Latex写博文,然后要发表到博客园上与大家 ...

  7. UrlPathEncode与UrlEncode的区别

    UrlEncode与UrlPathEncode 的基本作用都是对 URL 字符串进行编码 不同点总结如下: 不同点 UrlEncode UrlPathEncode 处理空格的方式 替换成“+” 替换成 ...

  8. Android(java)学习笔记106-2:反射机制

    1.反射机制: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为 ...

  9. 第二节:Maven的运行机制

    Maven 的运行机制分为两个分别是生命周期和插件 首先我们来说说Maven的生命周期 1.1:生命周期是个个阶段组成的 1.2:Maven的生命周期是相互独立的,他们之间没有交集 1.3:阶段是有顺 ...

  10. 《Cortex-M0权威指南》之绪论

    转载请注明来源:cuixiaolei的技术博客 1.1 为什么要选择Cortex-M0 为了满足现代超低功耗微控制器和混合信号设备的需要,ARM推出了Cortex-M0处理器.Cortex-M0在保持 ...