Borg Maze - poj 3026(BFS + Kruskal 算法)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9821 | Accepted: 3283 |
Description
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
Output
Sample Input
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8
11
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include <iostream>
using namespace std;
int map[][];
int vis[][];
int parent[];
int row, col;
int edge_num = ;
int point_num=;
struct edge {
int u, v, w;
} e[ * ];
struct points {
int u, v, d;
} point;
bool cmp(const edge e1,const edge e2){
return e1.w<e2.w;
}
int Kruskal(){
int mindis=;
for(int i=;i<=point_num;i++){
parent[i]=i;
}
sort(e,e+edge_num,cmp);
int pnum=;
for(int i=;i<edge_num&&pnum<point_num;i++){
int k,g;
int u=e[i].u;
int v=e[i].v;
for(k=parent[u];parent[k]!=k;k=parent[parent[k]]);
for(g=parent[v];parent[g]!=g;g=parent[parent[g]]); if(k!=g){
parent[g]=k;
mindis+=e[i].w;
pnum++;
}
}
return mindis;
}
void BFS(int x, int y) {
queue<points> p;
points s;
s.u = x;
s.v = y;
s.d = ;
p.push(s);
memset(vis, , * * sizeof(int));
vis[x][y] = ; while (!p.empty()) {
points tmp = p.front();
p.pop();
for (int i = -; i < ; i += ) {
points next;
next.u = tmp.u + i;
next.v = tmp.v;
next.d = tmp.d + ;
if (next.u >= && next.u < row) {
if (!vis[next.u][next.v]) {
int pos = map[next.u][next.v];
vis[next.u][next.v] = ;
if (pos >= )
p.push(next);
if (pos >= ) {
e[edge_num].u = map[x][y];
e[edge_num].v = pos;
e[edge_num].w = next.d;
edge_num++;
}
}
}
}
for (int i = -; i < ; i += ) {
points next;
next.u = tmp.u;
next.v = tmp.v + i;
next.d = tmp.d + ;
if (next.v >= && next.v < col) {
if (!vis[next.u][next.v]) {
int pos = map[next.u][next.v];
vis[next.u][next.v] = ;
if (pos >= )
p.push(next);
if (pos >= ) {
e[edge_num].u = map[x][y];
e[edge_num].v = pos;
e[edge_num].w = next.d;
edge_num++;
}
}
}
} }
}
int main() {
int n;
scanf("%d", &n);
char tmp[];
for (int i = ; i < n; i++) {
point_num=;
edge_num=;
scanf("%d %d", &col, &row);
gets(tmp); //坑【一串空格】
for (int j = ; j < row; j++) {
for (int k = ; k < col; k++) {
char c;
scanf("%c", &c);
if (c == ' ')
map[j][k] = ;
else if (c == '#')
map[j][k] = -;
else if (c == '\n')
k--;
else
map[j][k] = ++point_num;
} }
for (int j = ; j < row; j++) {
for (int k = ; k < col; k++) {
if (map[j][k] > ) {
BFS(j, k);
}
}
}
int mid=Kruskal();
printf("%d\n",mid);
}
return ;
}
Borg Maze - poj 3026(BFS + Kruskal 算法)的更多相关文章
- Borg Maze POJ - 3026 (BFS + 最小生成树)
题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...
- Borg Maze poj 3026
Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...
- J - Borg Maze - poj 3026(BFS+prim)
在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...
- (最小生成树) Borg Maze -- POJ -- 3026
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...
- poj 3026(BFS+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12032 Accepted: 3932 Descri ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
- POJ 3026 Borg Maze(Prim+bfs求各点间距离)
题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...
- poj 3026 Borg Maze(最小生成树+bfs)
题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...
随机推荐
- Jquery表单验证插件validate
写在前面: 在做一些添加功能的时候,表单的提交前的验证是必不可少的,jquery的validate插件就还可以,对于基本的需求已经够了.这里记录下基本的用法. 还是写个简单的demo吧 <htm ...
- iOS8下的远程推送
本篇文章主要介绍了"iOS8下的远程推送",主要涉及到方面的内容,对于IOS开发感兴趣的同学可以参考一下: 昨天做了一下远程推送,今天写下来,分享给需要的人.参考了很多篇文章,或许 ...
- PHP开发环境配置系列(四)-XAMPP常用信息
PHP开发环境配置系列(四)-XAMPP常用信息 博客分类: PHP开发环境配置系列 xamppphp 完成了前面三篇后(<PHP开发环境配置系列(一)-Apache无法启动(SSL冲突)> ...
- MathType requires a newer version of MT Extra等MathType问题的不兼容性解决方案
常见问题解决方法: 1.MathType 6.0与office 2007兼容问题 由于Office软件安装时默认是不安装公式编辑器的,在安装完MathType 6.0之后,需要将\MathType 6 ...
- numpy 多维数组的存取
多维数组的存取和一维数组类似,由于多维数组有多个轴,所以他的下标需要多个值来表示.这里讨论的主要是二维数组.二维数组0轴以行为单位,1轴以列为单位,存取数组使用元组作为下标,需要注意的是,python ...
- 阿里蚂蚁的前端ant-design
蚂蚁国内镜像: http://ant-design.gitee.io/components/date-picker-cn/ 阿里的设计平台:https://design.alipay.com/deve ...
- util.select.js
ylbtech-JavaScript-util: util.select.js 筛选工具 1.A,JS-效果图返回顶部 1.B,JS-Source Code(源代码)返回顶部 1.B.1, m.y ...
- 如何给JQ的ajax方法中的success()传入参数?
当时在使用JQuery提供的Ajax技术的时候,我有个需求,就是要给它请求成功后调用的success()方法传入参数: 所以,我就直接这样子写了: <script> function ge ...
- Ubuntu16.04下安装googlechrome flash 插件和安装网易云音乐
一.ubuntu 16.04 下安装完后发现 flash无法播放没有安装flash插件因为 Adobe Flash 不再支持 linux Google 便开发了PepperFlashPlayer来替代 ...
- iptables 的学习资源
慕课网:https://www.imooc.com/video/7617 马哥linux视频:http://edu.51cto.com//center/course/lesson/index?id=9 ...