传送门:Borg Maze

题意:有一个迷宫,里面有一些外星人,外星人用字母A表示,#表示墙,不能走,空格可以走,从S点出发,在起点S和A处可以分叉走,问找到所有的外星人的最短路径是多少?

分析:分别bfs由S和所有A出发到其他点的距离,然后建好图进行最小生成树处理即可。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 110
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct edge
{
int u,v,w;
edge() {}
edge(int u,int v,int w):u(u),v(v),w(w){}
bool operator<(const edge &a)const
{
return w<a.w;
}
} e[N*N];
struct node
{
int x,y,step;
node(){}
node(int x,int y,int step):x(x),y(y),step(step){}
};
int fa[N],tot,total;
char str[N][N];
int num[N][N],vis[N][N],n,m;
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int MST(int n)
{
int ans=;
for(int i=;i<=n;i++)fa[i]=i;
sort(e,e+tot);
for(int i=; i<tot; i++)
{
int a=find(e[i].u);
int b=find(e[i].v);
if(a==b)continue;
fa[a]=b;
ans+=e[i].w;
}
return ans;
}
bool judge(int a,int b)
{
return a>=&&a<=n&&b>=&&b<=m&&str[a][b]!='#'&&!vis[a][b];
}
void bfs(int x,int y)
{
queue<node>que;
while(!que.empty())que.pop();
FILL(vis,);
vis[x][y]=;
que.push(node(x,y,));
while(!que.empty())
{
node now=que.front();que.pop();
for(int i=-;i<=;i++)
for(int j=-;j<=;j++)
{
if(i+j==||i==j)continue;
int a=now.x+i,b=now.y+j,step=now.step+;
if(judge(a,b))
{
if(str[a][b]=='A'||str[a][b]=='S')
{
e[tot++]=edge(num[x][y],num[a][b],step);
}
vis[a][b]=;
que.push(node(a,b,step));
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
gets(str[]);
for(int i=;i<=n;i++)
gets(str[i]);
total=;tot=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(str[i][j]=='A'||str[i][j]=='S')
{
num[i][j]=++total;
}
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(str[i][j]=='A'||str[i][j]=='S')
{
bfs(i,j);
}
}
printf("%d\n",MST(total));
}
}

poj3206(bfs+最小生成树)的更多相关文章

  1. 【bzoj4242】水壶 BFS+最小生成树+倍增LCA

    题目描述 JOI君所居住的IOI市以一年四季都十分炎热著称. IOI市是一个被分成纵H*横W块区域的长方形,每个区域都是建筑物.原野.墙壁之一.建筑物的区域有P个,编号为1...P. JOI君只能进入 ...

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

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6634   Accepted: 2240 Descrip ...

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

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

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

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

  5. poj 3026(BFS+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12032   Accepted: 3932 Descri ...

  6. LOJ #2876. 「JOISC 2014 Day2」水壶 BFS+最小生成树+倍增LCA

    非常好的一道图论问题. 显然,我们要求城市间的最小生成树,然后查询路径最大值. 然后我们有一个非常神的处理方法:进行多源 BFS,处理出每一个城市的管辖范围. 显然,如果两个城市的管辖范围没有交集的话 ...

  7. POJ 3026 Borg Maze 广搜(BFS)+最小生成树

    题意:从S出发,去抓每一个A,求总路径最短长度.在S点和A点人可以分身成2人,不过一次只能让一个人走. 思路是先利用BFS求出各点之间的距离,建成图,再套用最小生成树模板. 一次性A了.不过觉得在判断 ...

  8. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

  9. Borg Maze POJ - 3026 (BFS + 最小生成树)

    题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...

随机推荐

  1. SDUT 2893-B(DP || 记忆化搜索)

    B Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 有n块地板排成一条直线,从左到右编号为1,2,3. . . n-1,n,每 ...

  2. sql server日期字段值的比较

    sql server中对日期字段值的比较 sql server中对日期字段的比较方式有多种,介绍几种常用的方式:用northwind库中的employees表作为用例表.1.between...and ...

  3. 来自中油瑞飞的SQL笔试题20131202

    1.有三张表,用户表,用户角色表,角色表, 使用sql显示如下内容: 用户ID,用户名,超级管理员,录入员,会计 也就是角色用逗号分隔. 解: 1.填充数据到表User select * from [ ...

  4. webstorm入门1-主题和配色

    1.引子 以前介绍过 Sublime text 3 系列的文章,着重介绍了 Sublime text 3 如何下载.安装.插件.配置等内容.Sublime text 3的轻量和富扩展,为前端开发带来了 ...

  5. winform基础——实现简易赈灾物资发放登记系统

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  6. 【翻译】ASP.NET Web API是什么?

    原文 [翻译]ASP.NET Web API是什么? 说明:随微软ASP.NET MVC 4一起发布的还有一个框架,叫做ASP.NET Web API.目前国内关注这项技术的人似乎还很少,这方面的文章 ...

  7. Jsp中使用数据库连接池.

    原文 Jsp中使用数据库连接池. 1. 在tomcat服务器目录下面的conf中找到一个叫Context.xml的配置文件,在其中加入以下代码 <Resource name="jdbc ...

  8. Delphi中使用GDI+进行绘图(2)

    2)使用IGDIPlus接口 (1)下载安装所需软件 可以在以下地址下载IGDI+最新的安装程序. http://www.mitov.com/products/igdi+ www.igdiplus.o ...

  9. Android应用开发学习笔记之绘图

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 一.绘图常用类介绍 在Android中绘图时,常用到的几个类是Paint.Canvas.Bitmap和Bitmapt ...

  10. 利用SQL语句实现分页

    1.概述 在网页中如果显示的数据太多就会占据过多的页面,而且显示速度也会很慢.为了控制每次在页面上显示数据的数量,就可以利用分页来显示数据. 2.技术要点 在SQL Server中要实现SQL分页,需 ...