(POJ 3026) Borg Maze 最小生成树+bfs

时间:2022-09-11 08:04:49

题目链接: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 最小生成树&plus;bfs prim算法 难度&colon;0

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

  2. poj 3026 Borg Maze &lpar;最小生成树&plus;bfs&rpar;

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

  3. POJ 3026 Borg Maze【BFS&plus;最小生成树】

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

  4. POJ 3026 Borg Maze(bfs&plus;最小生成树)

    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 最小生成树 &plus; 广搜

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

  7. poj 3026 Borg Maze &lpar;BFS &plus; Prim&rpar;

    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 &lpar;bfs &plus; 最小生成树&rpar;

    链接: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. H5实现本地预览图片

    我们使用H5可以很容易的实现图片上传前对其进行预览的功能 Html代码如下: <!DOCTYPE html> <html lang="en"> <he ...

  2. HTTP状态码(2xx,3xx,4xx,5xx)

    HTTP状态码负责表示客户端请求的返回结果,标记服务器的处理结果. HTTP常用状态码分为5种:   类别 原因短语 1xx Informational(信息状态码) 接受请求正在处理 2xx Suc ...

  3. 函数式中的 currying

    currying 是函数式语言中经常遇到的一个概念,翻译成 柯里化,不是库里化. currying 指的是将接收多个参数的函数变换成接收一个单一参数,并且返回接收余下的参数而且返回结果的新函数的技术. ...

  4. 强制span不换行

    对于上一篇提到的overflow的问题我好像搞懂一些了.事情大概是这个样子的:如果用了float属性,那么元素就会脱离文本的束缚,无法无天起来,这肯定是猿类无法忍受的.要想让他们乖乖就范,要么用清除浮 ...

  5. &period;NET4&period;0 &lowbar;&lowbar;doPostBack未定义

    方法一.浏览器设置成兼容模式. 方法二.安装服务器版的.Net40的补丁.http://download.csdn.net/detail/5653325/6642051 方法三.点击VS的工具菜单-- ...

  6. 21&period; javacript高级程序设计-Ajax与Comet

    1. Ajax与Comet 1.1 XMLHttpRequest对象 IE5是第一款引入XHR对象的浏览器,IE5中是通过MSXML库中的一个ActiveX对象实现的.因此在IE中可能存在MSXML2 ...

  7. redis 初探

    2014年6月24日 17:50:57 解压redis后进入源码目录,只用执行make命令就可以完成安装了 安装完成后到src目录里,将 redis-server redis-cli redis.co ...

  8. UESTC 1854

    题目意思  就是说 有一个起点一个终点  和一些虫洞,从一个虫洞进去然后出来,是不需要消耗时间的,注意点就是,虫洞是一条线段,你可以从线段的任意位置进去,从任意位置出来: 所以从一个虫洞到另一个虫洞的 ...

  9. Java实现邮箱找回密码

    通过邮件找回密码功能的实现 1.最近开发一个系统,有个需求就是,忘记密码后通过邮箱找回.现在的系统在注册的时候都会强制输入邮箱,其一目的就是 通过邮件绑定找回,可以进行密码找回.通过java发送邮件的 ...

  10. WPF Multi-Touch 开发:基础触屏操作(Raw Touch)

    原文 WPF Multi-Touch 开发:基础触屏操作(Raw Touch) 多点触控(Multi-Touch)就是通过与触屏设备的接触达到人与应用程序交互的操作过程.例如,生活中经常使用的触屏手机 ...