题目:

题目链接:

http://poj.org/problem?id=3026

Borg Maze

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17462   Accepted: 5631

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

  1. 2
  2. 6 5
  3. #####
  4. #A#A##
  5. # # A#
  6. #S ##
  7. #####
  8. 7 7
  9. #####
  10. #AAA###
  11. # A#
  12. # S ###
  13. # #
  14. #AAA###
  15. #####

Sample Output

  1. 8
  2. 11

题目思路:

其实s 和 a是一样的 只要求出连接A和S的最小生成树就可以了

主要就是求每个点之间的距离就可以了

具体求法用 BFS遍历每一个点  用邻接表储存  用kruskal算法求最小生成树就可以了

比较坑的就是

一 题目样例里有莫名其妙的一串空格  在输入x y后有一大串空格  要读入并舍弃

二 数组要开大 n<50  我开55 就wa 开205就ac 。。。 迷。。。

AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
char pic[205][205];
int alien[205][205],allroad,vis[205][205],t,n,m,num,father[205],sum;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
struct r
{int a,b,dis;
}road[100000];
struct node
{int x,y,step;
};
bool cmp(struct r a,struct r b)
{return a.dis<=b.dis;
}
void bfs(int bx,int by)
{memset(vis,0,sizeof(vis));queue <node> q;while(!q.empty())q.pop();node b,now,next;b.x=bx,b.y=by,b.step=0;vis[bx][by]=1;q.push(b);int c=0;while(!q.empty()){now = q.front();q.pop();if(vis[now.x][now.y]==0 && alien[now.x][now.y]>alien[bx][by]){c++;allroad++;road[allroad].a=alien[bx][by];road[allroad].b=alien[now.x][now.y];road[allroad].dis=now.step;if(c==num-1)break;}for(int d=0;d<4;d++){next = now;next.x += dx[d];next.y += dy[d];if(next.x<0 || next.y<0 || next.x>=n || next.y>=m)continue;if(vis[next.x][next.y]==1 || pic[next.x][next.y]=='#')continue;next.step++;vis[next.x][next.y]=1;if(alien[next.x][next.y]>alien[bx][by]){c++;allroad++;road[allroad].a=alien[bx][by];road[allroad].b=alien[next.x][next.y];road[allroad].dis=next.step;if(c==num-1)break;}q.push(next);}if(c==num-1)break;}
}
int findd(int x);
int main()
{char temp[110];scanf("%d",&t);getchar();while(t--){scanf("%d %d",&m,&n);gets(temp);memset(alien,0,sizeof(alien));num=0;for(int i=0; i<n; i++){cin.getline(pic[i],m+1);for(int j=0; j<m; j++)if(pic[i][j]=='A' || pic[i][j]=='S'){num++;alien[i][j]=num;}}allroad=-1;int flag=0;int c=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(alien[i][j]>0){bfs(i,j);c++;}if(c==num-1){flag=1;break;}}if(flag == 1)break;}sort(road,road+allroad+1,cmp);for(int i=1;i<=num;i++)father[i]=i;c=0;sum=0;for(int i=0;i<=allroad;i++){int ha = findd(road[i].a);int hb = findd(road[i].b);if(ha != hb){c++;father[ha]=hb;sum += road[i].dis;if(c == num-1)break;}}printf("%d\n",sum);}return 0;
}
int findd(int x)
{int a=x;while(father[x]!=x)x=father[x];while(x!=father[x]){int z=a;a=father[a];father[z]=x;}return x;
}

转载于:https://www.cnblogs.com/20172674xi/p/9536190.html

POJ - 3026(BFS+最小生成树.krustal)相关推荐

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

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

  2. poj 3026 BorgMaze 最小生成树Kruskal、Prim(Prim VS报错待解决

    题意 以及 思路: 从S点有一伙人出发去消灭A点的敌人,在S点或者A点可以分裂成几个小队然后分别走,这样路径=总队路径+各个小队路径 问你怎样路径最短. 在一个y行 x列的迷宫中,有可行走的通路空格' ...

  3. 【POJ 3026】Borg Maze

    [POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值最小(最小生成树 BFS+最小生成 ...

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

    题目链接:http://poj.org/problem?id=3026. DescriptionThe Borg is an immensely powerful race of enhanced h ...

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

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

  6. 【POJ - 3026】Borg Maze(bfs预处理 + 最小生成树,建图)

    题干: The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the gala ...

  7. POJ - 1251(最小生成树.krustal)

    题目链接:http://poj.org/problem?id=1251 题目: Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total ...

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

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

  9. poj 3026 (最小生成树)

    题意:起点开始有超过100个人,总共不会超过100个外星人,问把所有的外星人都搜出来花的最小时间.一条路径上的时间跟人数是无关的,只跟路径长度有关. 思路:刚开始人都在起点,当派一定人数去最近的外星人 ...

最新文章

  1. python二叉搜索树建立_700. 二叉搜索树的搜索(Python)
  2. 012_HttpServletResponse响应乱码
  3. SAP 与 JSON 接口实施案例一
  4. linux命令(8)wc
  5. Apache下PHP Loaded Configuration File None 解决方法
  6. Solidity 简易教程0x001
  7. 什么是机器学习?有哪些分类?到底有什么用?终于有人讲明白了
  8. 前端开发 锚点链接 简单轮播图 0302
  9. 新买的服务器怎么安装Linux,买了新服务器,如何从头开始配置?Ubuntu系统安装+新建用户名+挂...
  10. 一种牛逼的网站分享插件实现
  11. Python(一)缺点
  12. Go语言的map以及sort
  13. 鸡兔同笼php编程,C语言鸡兔同笼问题
  14. 计算机在机械工程中的应用英语作文,关于机械工程英语作文
  15. angular 万年历_世界豪华手表
  16. B站傅希鸣-ElasticSearch学习笔记(ES 入门)
  17. 由计算机病毒引起的问题属于,多数情况下由计算机病毒程序引起的问题属于()故障。A.硬件B.软件C.操作D.电源...
  18. 图像分割—基于区域的图像分割
  19. 路由器开发整理(1)
  20. 内核tcp协议栈SACK的处理

热门文章

  1. java流式布局换行_自动换行的流式布局
  2. python重新启动整个脚本_每次对脚本进行更改时,都需要在终端中重新启动python...
  3. excel数据库_EXCEL憋出大招,逆袭大数据的黑马出现了
  4. SQLi LABS Less 25 联合注入+报错注入+布尔盲注
  5. mysql的命令行常用命令_MySQL Command Line[mysql命令行常用命令]
  6. 零基础能不能学习web前端开发?
  7. jmeter学习指南之源码导入 IntelliJ IDEA
  8. RHEL 7.2 + Oracle 12cR2 + RAC
  9. Kubernetes负载均衡器-traefik ingress安装
  10. filter和map的区别