POJ 3026-Borg Maze【最小生成树+bfs】

【代码超详解】POJ 3026 Borg Maze(优先队列 BFS + 最小生成树,0 ms) 一、题目描述 二、算法分析说明与代码编写指导 法一 对每个点 S 和 A 各做一次 BFS 求得单源最短路。用求得的全部结果建图,求解最小生成树。 法二 用一次带优先队列的 BFS 代替暴力 n 次 BFS。原理是: 从起点开始做 BFS。找到第一个 A(设到 S 的距离为 D)后,记这个 A 点到它本身距离为 0(到其它点的距离未知),压入优先队列 q。于是,接下来出队的总是 A 及其周... 阅读详情

写在前面

本来觉得这道题是一道挺好的最小生成树的题,但是当我提交后,一直WA,我开始怀疑自己,知道原因后我自闭了,最后原因竟是!!    (ノ=Д=)ノ┻━┻

题目描述

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 100 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 11=5+3+3.

Input

On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that 1 <= x,y <= 50. 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 100 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

2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  

Sample Output

8
11

题意

题意还挺难理解的,我读了好几遍才看懂 。

题意是给你一个y*x的迷宫,‘#’代表墙壁,‘A’代表外星人,‘S’是出发点,‘ ’(空格)是开放区域,这个人可以在‘S’和‘A’处分身成多个人,问你找到所有的外星人,最少要走几步。

例如第一个样例,这个人在‘S’处分身成两个,一个到达(2,2)这个‘A’点要走2步,一个到达(3,5)这个‘A’点要走4步,再从(3,5)这个‘A’点到达(2,4)这个‘A’点需要2步,一共8 = 2+4+2 步。

其实题意就是把‘S’和‘A’点看成一样的点,计算每两个点之间要走的最短步数,便形成了一个图,然后在图中找最小生成树就可以了。

思路

一般迷宫的题,都会想到深搜(dfs)和宽搜(bfs),在这我们用bfs,对每一个‘A’和‘S’点跑一遍bfs,这样就可以得到每两个点之间的最短距离,然后再跑一遍Kruskal就可以了。

最后别以为这样写完真的可以了!!这道题的数据在输入完x,y后,有莫名其妙的空格,所以,在输入完x,y后,需要用gets()把多余的空格去掉后,再输入迷宫,不然会一直WA!!!!       (ノ=Д=)ノ┻━┻

代码

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;

const int Max = 55;
char maze[Max][Max];
bool flag[Max][Max];
int po[Max][Max];


const int Maxe = 100005;
const int Maxn = 105;
struct edge{
    int from,to,val;
};
edge e[Maxe];
int par[Maxn];
int ct;

bool cmp(edge e1, edge e2)
{
    return e1.val<e2.val;
}
void Init()
{
    for(int i = 0; i < Maxn; i++)
        par[i] = i;
}
int Find(int x)
{
    if(x==par[x])
        return par[x];
    else
        return par[x] = Find(par[x]);
}
bool Union(int x, int y)
{
    x = Find(x);
    y = Find(y);
    if(x!=y)
    {
        par[y] = x;
        return true;
    }
    else
        return false;
}
int Kruskal()
{
    int res = 0;
    sort(e,e+ct,cmp);
    Init();
    for(int i = 0; i < ct; i++)
    {
        if(Union(e[i].from, e[i].to))
            res+=e[i].val;
    }
    return res;
}

struct node{
    int x,y,step;
    friend bool operator < (node n1, node n2)
    {
        return n1.step>n2.step;
    }
};

int n,m;
int cnt;
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};
priority_queue<node> q;

bool inmaze(int nx, int ny)
{
    return nx>=0&&nx<n&&ny>=0&&ny<m;
}
void bfs(int sx, int sy)
{
    node n1;
    n1.x = sx;
    n1.y = sy;
    n1.step = 0;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
            flag[i][j] = false;
    }
    flag[sx][sy] = true;
    q.push(n1);
    while(!q.empty())
    {
        n1 = q.top();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            int nx = n1.x+dx[i];
            int ny = n1.y+dy[i];
            if(inmaze(nx,ny)&&!flag[nx][ny])
            {
                node n2;
                n2.x = nx;
                n2.y = ny;
                n2.step = n1.step+1;
                if(maze[nx][ny]==' ')
                {
                    q.push(n2);
                    flag[nx][ny] = true;
                }
                else if(maze[nx][ny]=='A'||maze[nx][ny]=='S')
                {
                    q.push(n2);
                    flag[nx][ny] = true;
                    if(po[sx][sy]<po[nx][ny])
                    {
                        e[ct].from = po[sx][sy];
                        e[ct].to = po[nx][ny];
                        e[ct++].val = n2.step;
                    }
                }
            }
        }
    }
}
int main()
{
    int t;
    char s[100];
    scanf("%d", &t);
    while(t--)
    {
        cnt = 0;
        ct = 0;
        scanf("%d %d", &m, &n);
        gets(s);//吸收莫名其妙的空格
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                scanf("%c", &maze[i][j]);
                if(maze[i][j]=='S'||maze[i][j]=='A')
                {
                    po[i][j] = cnt++;
                }
            }
            getchar();
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(maze[i][j]=='S'||maze[i][j]=='A')
                {
                    bfs(i,j);
                }
            }
        }
        printf("%d\n", Kruskal());
    }
    return 0;
}

如有错误请指明~   ฅ●ω●ฅ

如何用Megatron-LM搭建多卡集群?手把手教你配置分布式训练环境 本文提供了一份详细的Megatron-LM多卡集群搭建实战指南。从硬件兼容性检查、软件栈版本对齐,到分布式并行策略(数据并行、张量并行、流水线并行)的配置与多节点启动脚本编写,手把手教你配置稳定高效的分布式训练环境,并分享了关键的排错与性能调优经验。 阅读详情

相关推荐

基于STM32F407与AD9910/AD9959的高频可调DDS信号源设计与实战

本文详细介绍了基于STM32F407微控制器与AD9910/AD9959高性能DDS芯片的高频可调信号源设计与实现。文章从DDS原理入手,阐述了双芯片方案的优势,并重点分享了高频电路设计中的电源、PCB布局布线等关键硬件设计技巧与避坑指南。同时,提供了通过SPI配置DDS芯片及利用Matlab生成任意波形的软件驱动方法,旨在帮助开发者打造一个覆盖10Hz至4200MHz的实用信号发生器。

weixin_36184718的博客 120

Borg Maze (最小生成树+bfs)

    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 civilizati...

Z2Min_ 725

STM32使用LORA模块通信

目录 一、模块使用方法 1、模块简介 2、硬件及功能 3、传输方式 二、模块上手 1、连接 2、编写代码 usart3.h usart3.c lora.h lora.c 一、模块使用方法 本文以正点原子ATK-LORA-01模块为例进行介绍。 1、模块简介 ATK-LORA-01-V3.0(V3.0是版本号,下面均以ATK-LORA-01表示该产品)是ALIENTEK推出的一款体积小、微功率、低功耗、高性能远距离LORA无线串口模块。模块设计是采用高效的IS...

qq_54706971的博客 3万+

Borg Maze

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9971   Accepted: 3347 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of

绝尘 1023

网络变压器等效电路及网络变压器中间抽头作用详解

       华强盛电子导读:要详细了解网络变压器周边EMC管控及网络变压器BOB-SMITH电路的连接原理,首先我们对网络变压器的认识及其电路各参数影响都要有个简要的认识,本文,对非理想网络变压器等效电路做一个详细的分析,其中重点讨论一下网络变压器中间抽头的作用其影响                          网络变压器等效电路及                        ...

网络变压器专业资料 6005

别再手动写ZPL了!用C#和斑马官方SDK搞定ZT410 RFID打印机(附中文乱码解决方案)

本文详细介绍了如何使用C#和斑马官方SDK驱动ZT410 RFID打印机,解决中文乱码和RFID数据写入等常见问题。通过实战指南,开发者可以快速掌握从基础连接到高级调试的全流程,提升打印效率和可靠性。特别适用于物流、零售和资产管理领域的应用。

weixin_29267307的博客 224

Borg Maze (BFS+最小生成树)(解题报告)

Borg Maze 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 collectiv

weixin_52879528的博客 262

JavaFx之集成FontAwesomeFX与JFoenix(十五)

本文详细介绍了如何在JavaFx项目中同时集成JFoenix和FontAwesomeFX,以创建现代化、美观的桌面应用界面。通过Maven依赖配置、动态代码创建和FXML静态声明两种方式,演示了如何将JFoenix的Material Design控件与FontAwesomeFX的丰富矢量图标完美结合,并提供了实战案例展示如何构建一个功能完善的现代化工具栏。

pepsi的博客 437

POJ3026(最小生成树)

POJ3026(最小生成树) Borg Maze 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 individua

shui2104的博客 282

SAP S4业务伙伴(BP)实战指南:从角色分配到数据同步的完整流程

本文详细解析了SAP S4业务伙伴(BP)功能,从基础概念到实战操作,涵盖角色分配、数据同步及常见问题排查。通过BP机制,企业可高效管理客户、供应商等多重身份,实现数据共享和动态切换,显著提升主数据管理效率。特别适合SAP实施顾问和ERP管理员参考。

y2z3a4b5c的博客 868

MAVLink协议详解

MAVLink是为微型飞行器MAV(Micro Air Vehicle)设计的(LGPL)开源的通讯协议。是无人飞行器和地面站(Ground Control Station ,GCS)之间,以及无人飞行器之间通讯常用的协议。APM、PIXHAWK飞控,Mission Planner、QGroundControl地面站均使用了MAVLink协议进行通讯。 MAVLink源码下载地址(现已更新至v2.0):https://github.com/mavlink/qgroundcontrol   用户手册:ht

程序猿老樊的博客 7704

Borg Maze--Prim+BFS

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12531   Accepted: 4105 Description The Borg is an immensely powerful race of enhanced humanoids f

bless295的博客 344

基于Simulink的永磁同步电机容错控制(缺相容错)

本文基于Simulink平台,详细介绍了永磁同步电机(PMSM)缺相容错控制的仿真实现方法。针对逆变器开路故障导致的缺相问题,提出"检测-重构-补偿"三阶段控制策略:采用电流阈值法快速检测故障(<5ms),重构αβ坐标系实现两相矢量控制,并通过谐波电流注入抑制转矩脉动(<8%)。仿真结果表明,该方法能有效维持故障后电机运行,转速波动<3%,满足电动汽车等关键应用的可靠性需求。文章包含完整的Simulink建模步骤、MATLAB函数代码及参数设置,为工程实践提供了可复现的

xiaoheshang_123的博客 146

POJ 3026 Borg Maze最小生成树+BFS

POJ 3026 Borg Maze 参考博客:https://blog.youkuaiyun.com/u014422052/article/details/40210399 一开始拿到题完全看不懂…… 好不容易理解了题意发现做不了,想单用bfs的话得每次判断两个方向,实在太复杂就不想下去了 看了大神操作:发现原来这还是最小生成树的拓展 需要把每个点都连起来,但是如果不能分裂(变方向)的话,就得多算很多重复的...

想要一只银渐层 228

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

凡人修c传(十三)POJ 3026 Borg Maze(bfs+最小生成树)

每日一水

kyrietheshy的博客 118

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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

链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#problem/J Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Su

free斩 2731

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

poj 3026 Borg Maze 最小生成树+bfs

poj 3026 Borg Maze http://poj.org/problem?id=3026   从S点开始,可以出发任意多人开始走,走的相同的路只算一个人的路程,人只能在A点与S点处分开,求S点开始遍历所有A点所经过的最小路程   注意m与n后可能有空格,不能用getchar,要用gets………………     #include #include #include

倩 440

POJ3026 Borg Maze【BFS+最小生成树】

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 collec

胡小涛的博客 435

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

题目链接:http://poj.org/problem?id=3026

scf0920 1116

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

题目链接: http://poj.org/problem?id=3026 题意:一对警察(S)抓捕外星人(A),‘#’代表墙,‘_’(空格)代表可以通过的路,从S处出发,问最少走多长的路可以把外星人全部KO了!注意:当他们在S处或是当抓捕到某个外星人以后,可以分开成几个队伍向周围继续抓捕,总的路程等于每支队伍的总和! #include <cstdio> #inclu...

ailincheng0815的博客 150

Borg Maze POJ - 3026 BFS+最小生成树

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 civ...

李兆龙的博客 476

POJ3026 Borg Maze BFS+最小生成树

这道题其实不难,思路就是将走过的路用直线连起来,就立刻想到了构造一颗树,而树的边权是任意两点间的最短距离,需要用bfs搜索,最后用最小生成树模板套一下就行了。但我主要遇到了三个问题,做了很久才AC: 1. 样例研究了很久没看懂,尤其是第一个,直到突然发现只有在S点和A点处才能“分队”,而不是任何位置。 2. 写bfs的时候,我一开始写的是O(n^2)的复杂度,即任意两点之间都用一次bfs,判TLE了。。然后看了别人的博客,发现只用O(n)的复杂度即可,即每个点进行一次bfs,搜索它到其余各点的最短路径;

A confused programmer 326

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

题目链接:点击打开链接 题意: 给你一个地图,地图中A代表外星人,S代表出发点,从S点出发,每遇到一个A点便可分裂成多个。 求把所有A都吃完需要多少步。 刚开始没读好题,我以为S点随时都能分裂,头疼了好一会。 思路: 其实A和S是一样的,本题的本质就是求连接A和S的最小生成树。无非麻烦的一点就是各个结点之间的权值。 这里采取的办法是,枚举每一个结点进行一遍BFS,求该

JOKER_SAMA的博客 1758

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

题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显的最小生成树,点与点的距离要用bfs或者dfs求一下。这题bfs要稍微优化一下。不能下暴力的bfs就是两点两点之间 bfs这样会有很多重复的查询会超时,所以要一次性的bfs找到一个点就...

awow80285的博客 134

POJ 3026 Borg Maze bfs + 最小生成树

题意:说有一个迷宫,里面有一些外星人,外星人用字母A表示,#表示墙,不能走,空格可以走。从起点‘S’出发。在起点和A处可以分叉,问找到所有的外星人的最短路径是多少。 思路:此题其实不是太难了,可以先用bfs搜索图,然后建边,求出一点到另一点的距离,然后求最小生成树即可。最小生成树用prime和kruskal均可。关键是这道题输入需要注意。首先先输入的是列,然后是行。其次是输入列和行的后面有可

u013948187的专栏 494
上一篇: POJ 2349-Arctic Network【最小生成树】
下一篇: POJ 1679-The Unique MST【最小生成树的唯一性】
include_peng
博客等级 码龄9年 1粉丝 9原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值