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 及其周... 阅读详情

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7226 Accepted: 2429

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


==========================

只能在S或A处分裂,满足最小生成树,对每个点都bfs,求出各个点之间的距离,再求最小生成树


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
const int INF=0x3f3f3f3f;
int dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
int dis[111][111],vis[111][111],mark[111][111];
int m,n,cnt;
char map[111][111];

struct point
{
    int x,y,step,id;
}a[333],p,temp;

bool check(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=m) return 1;
    else return 0;
}

void bfs(point start)
{
    int num=0;
    queue<point>que;
    p.x=start.x;p.y=start.y;p.step=0;
    que.push(p);
    vis[p.x][p.y]=1;
    while(!que.empty())
    {
        temp=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            p.x=temp.x+dir[i][0];
            p.y=temp.y+dir[i][1];
            p.step=temp.step+1;
            if(!check(p.x,p.y)||map[p.x][p.y]=='#'||vis[p.x][p.y]) continue;
            que.push(p);
            vis[p.x][p.y]=1;
            if((map[p.x][p.y]=='A'||map[p.x][p.y]=='S'))
            {
                if(dis[ mark[p.x][p.y] ][start.id]==INF)
                    dis[ mark[p.x][p.y ]][start.id]=dis[start.id][ mark[p.x][p.y] ]=p.step;
                num++;
            }
            if(num==cnt-1) return;
        }
    }
}

int prim(int s)
{
    int res=0,vis[111],d[111];
    for(int i=1; i<=cnt; i++)
    {
        d[i]=INF;
        vis[i]=false;
    }
    d[s]=0;
    while(1)
    {
        int u=0,mn=INF;
        for (int i=1;i<=cnt;i++)
        {
            if (!vis[i]&&d[i]<mn)
            {
                mn=d[i];
                u=i;
            }
        }
        if(u==0) break;
        vis[u]=true;
        res+=mn;
        //cout<<"u="<<u<<" res="<<res<<" "<<endl;
        for (int i=1;i<=cnt;i++)
        {
            if (d[i]>dis[u][i]&&u!=i)
                d[i]=dis[u][i];
        }
    }
    return res;
}

int main()
{
    int T;char str[1111];
    scanf("%d",&T);
    while(T--)
    {
        memset(dis,INF,sizeof(dis));
        memset(mark,0,sizeof(mark));
        scanf("%d%d",&m,&n);
        gets(str);
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            gets(map[i]+1);
            for(int j=1;j<=m;j++)
            {
                if(map[i][j]=='S'||map[i][j]=='A')
                {
                    a[++cnt].id=cnt;
                    a[cnt].x=i;
                    a[cnt].y=j;
                    mark[i][j]=cnt;
                }
            }
        }
        for(int i=1;i<=cnt;i++)
            dis[i][i]=0;
        for(int i=1;i<=cnt;i++)
        {
            memset(vis,0,sizeof(vis));
            bfs(a[i]);
        }
        /*for(int i=1;i<=cnt;i++)
        {
            for(int j=1;j<=cnt;j++)
                cout<<dis[i][j]<<" ";
            cout<<endl;
        }*/
        printf("%d\n",prim(1));
    }
    return 0;
}


POJ3026-Borg Maze【BFS+Prim】 北大POJ3026-Borg Maze【BFS+Prim】 解题报告+AC代码 立即下载

相关推荐

无人车路径规划方法汇总

无人车路径规划方法汇总

weixin_44380244的博客 2558

poj 3026 Borg Maze

http://poj.org/problem?id=3026 题意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度; 思路:用bfs找出每两个点之间的距离,然后用prim求出最小生成树;思路是参考网上的; 代码: View Code #include <ios...

weixin_34416754的博客 126

Benchmark系列——基本概念和分类

本文主要介绍了Benchmark的基本概念和分类情况。

Bluetangos的博客 8438

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

手把手教你用EGM96模型校正DEM数据(含在线工具推荐)

本文详细介绍了如何使用EGM96地球重力场模型校正DEM数据中的高程偏差。通过对比WGS84椭球面与大地水准面的差异,解释了校正原理,并提供了从在线工具快速验证到Python脚本批量处理的全流程实战指南,帮助GIS开发者和数据分析师确保高程数据基准一致,提升空间分析精度。

ttt77的博客 1007

Apollo学习笔记(4)坐标系

百度Apollo项目用到了多种坐标系,其中帮助文档提及的坐标系包括: 全球地理坐标系(The Global Geographic coordinate system ) 局部坐标系—东-北-天坐标(The Local Frame – East-North-Up,ENU) 车身坐标系—右-前-天坐标(The Vehicle Frame —Right-Forward-Up,RFU) 车身坐标系—前-左-天坐标(The Vehicle Frame —Front-Left-Up,FLU) 还有一种Frenet坐标

qq_24649627的博客 5798

POJ - 3026 Borg Maze

https://vjudge.net/problem/POJ-3026 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<queue> using namespace std; const int N=60,M=120; int x,y,cnt,ans; char str[N][N]; int e[M][M],g[M][M],dis

斯文~ 254

某物data魔改与变种算法还原(含加解密Py纯算)

某物data加解密、设备指纹魔改、变异、自定义加密分析与纯算还原

吴秋霖的博客 1281

穿越火线 CF 错误代码3025-6 进不去游戏案例!

一电竞酒店,2020年6月份新装的联通50M固定ip光纤。大约在10月份的时候出现穿越火线进不去游戏,提示“网络异常,与服务器断开连接。(3025_6)” 出现整个问题以后,我第一时间在网维大师群里询问,有大佬告知遇到过光电转换器故障导致整个错误,可以换光电试试看。 于是我带上笔记本,去现场笔记本直接连接到光电转换器测试,还是相同的错误代码,进不去游戏。由此可以断定故障在联通线路上。 我电话联系装维 人员,他们说没遇到整个情况,需要问一下上级。我给酒店的解决方式是先用腾讯加速器,这样可...

ghtayy的博客 3万+

【WRF数据介绍第二期】气象驱动场数据介绍及下载

【WRF数据介绍第二期】气象驱动场数据介绍及下载

WW、forever的博客 2160

第二节:Vben Admin 登录逻辑梳理和对接后端准备

第一节,我们已经配置了前端环境,运行起来了我们的Vben Admin的项目。本节内容,我们先熟悉下项目的目录和文件,然后开始准备对接后端程序路由在 src/router页面在 sr/views本节前端登录逻辑梳理完成了,后端只要在/api/login 接口编写返回逻辑,前后段就可以打通了,具体详情我们下一节介绍。

cuiwin的专栏 5084

POJ 3026 Borg Maze //qdu_cxsys_最小生成树

Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description The Borg is an immensely powerful race of enhanced humanoids from the d

jerans的博客 333

RK35X8调试YT8531单网口千兆以太网PHY芯片,修复ping不通或者IP地址丢失的问题

RK35X8上调试YT8531以太网PHY

荻夜的博客 3976

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

Borg Maze DescriptionThe 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 th...

weixin_30869099的博客 105

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

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

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

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

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

POJ 3026 解题报告

这道题如果看出来是最小生成树之后就很好做了。求出所有alien(包括起点)之间的最短距离(我是用的bfs),然后求包含所有alien的最小生成树的距离之和就好了。 输入是个麻烦的地方。我用的是fgets,需要多分配2位(即52),因为得给\n和'\0'留位置。 这里仍然用的是之前kruskal的代码。构图需要点代码。 thestoryofsnow 3026 Accepte

thestoryofsnow的专栏 498

POJ 3026-Borg Maze

题目链接:Borg Maze 日,终于过了,对这题,无力多说,没想到还有那样的数据,不搜题解,我八辈子也过不了,最小生成树的最后一题! 题意:就是 S 是初始点,A是外星人,构造一条路线使他们都能到达,且路线长度最短; 思路:1.求距离->(BFS)  2. 构图(小数据:邻接矩阵) 3.建树(Prim) 水题,难的是 POJ后台。 没看别人题解,不知道加gets()

NotPerfect-EOF 1542

POJ_3026_BFS+prim+建图

POJ 3026 题意:求出图中连接S和A在图中的最短距离。 样例的意思是S出发步数为0. 难点: 在读入时用map<pair<int,int>,int>将坐标上的每一个点按顺序从1离散出来. 通过bfs建图.一开始我以为单点bfs,后来才用多点bfs建图.能使所有点之间边都存在 prim求最短路. 题目数据读入有空格,也要读(首先就写读入数据) #include ...

_llc的博客 196

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

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

写在前面 本来觉得这道题是一道挺好的最小生成树的题,但是当我提交后,一直WA,我开始怀疑自己,知道原因后我自闭了,最后原因竟是!!    (ノ=Д=)ノ┻━┻ 题目描述 The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg col...

include_peng的博客 326

POJ3026-Borg Maze(Kruskal+bfs)

题目描述 Input Output Sample Input 2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### ##### Sample Output 8 11 题意 t组提问,每组n*m个点,S代表起点,A代表外星人,#表示边界,搜索到外星人后可以继续移动,求最小移动的步数 思路 最小生成树+BFS,BFS求每个点到其它点最短距离(因为有墙,不能直接用坐标求

qq_53485165的博客 226

POJ3026

Problem: Borg Maze Description: 给你一个迷宫,和一个起点,很多个终点,现在从起点发出了一波机器人,这些机器人通过移动可以到达终点,如果几个机器人一起移动,那么只算一个机器人移动的距离。这些机器人可以在一起移动也可以分开。问最少要移动多少步才能使得所有的终点都有机器人到达。 Solution: 其实一看到这个题我也是没有想法的,后来看到别人的最小生成树突然就明白了。

FlushHip 907

poj3026 这道题要注意输入w, h之后的空格 BFS+prime

其实做完这道题整个人都不好了,感觉自己的BFS写得还好,应该不会错。结果WA了2次,看Disscus发现可能数组要开大点,结果还是WA。 然后看小优的博客,发现她也说道空格的事情,我的VS2013支持gets_s(),所以就把getchar()改成gets_s(temp),于是AC乎。 //poj3026 分小组搜索外星人,在起点或者搜到一个外星人就可以发生分组 //其实还是一个MST问题 只

chchlh的专栏 1038

POJ_3026_Borg Maze_最小生成树

打ACM就像是表白,像我这样的人ye

_Aka 353

poj3026 - Borg Maze (最短路)

题目链接:http://poj.org/problem?id=3026 题目大意:给出一个二维字符串,里面有#代表不能走,' '空格代表可以走,然后还有'A'和'S'代表人,问从S到达所有'A'的最小通路,就是最小生成树(意思就是s,a全部联通的总长度, s和a是一样的  没有区别) 解题思路:因为说了上面的A不会超过100个,所以开个102的数组够了,用BFS找出任意两点相距的最小距

qq_36782366的博客 291
上一篇: POJ 3264 - Balanced Lineup(线段树)
下一篇: POJ 3461 - Oulipo(KMP)
coco430
博客等级 码龄14年 9粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值