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

时间:2022-08-25 20:43:09

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

题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地。求从S出发,经过所有A所需要的最短路。你有一个特殊能力,当走到S或A时可以分身出任意多个人一起走。计算路程时就是所有人的总路程之和。

题解:想一下,是裸的最短路套上bfs。

  先暴力bfs出各个点之间的距离,存边

  然后kruskal

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<vector>
#include<string.h>
#include<set>
#include<string>
using namespace std;
const int maxn = 3e4;;
int len1, len2, p[maxn], ans;
set<int> s;
struct edge {
int to, from, w;
edge(int to=, int from=, int w=) :to(to), from(from), w(w) {}
};
vector<edge> e;
bool cmp(edge a, edge b) {
return a.w < b.w;
}
int f[maxn];
int find(int x) {
return f[x] == x ? x : f[x] = find(f[x]);
}
void un(int x, int y) {
int u = find(x), v= find(y);
f[u] = v;
}
bool same(int x, int y) {
return find(x) == find(y);
}
string map[];
int id[][];
int vis[][];
int dir[][] = { , ,,, ,-, -, };
struct node {
int x, y, t;
node(int x=, int y=, int t = ) :x(x), y(y), t(t) {}
};
void bfs(int x, int y) {
memset(vis, , sizeof(vis));
queue<node> Q;
Q.push(node(x, y, )); vis[x][y] = ;
while (!Q.empty()) {
node now = Q.front();
Q.pop();
for (int i = ; i < ; i++) {
int dx = now.x + dir[i][];
int dy = now.y + dir[i][];
if (map[dx][dy] == '#'||vis[dx][dy]) continue;
if (map[dx][dy] == 'A')e.push_back(edge(id[x][y], id[dx][dy], now.t+));
Q.push(node(dx, dy, now.t + ));
vis[dx][dy] = ; }
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
scanf("%d %d\n", &n,&m);
for (int i = ; i < m; i++) {
getline(cin, map[i]);
}
memset(id, , sizeof(id));
e.clear();
int idx = ;
for(int i=;i<m;i++)
for (int j = ; j < n; j++) {
if (map[i][j] == 'S')map[i][j] = 'A';
if (map[i][j] == 'A' ) {
id[i][j] = idx++;
}
}
for (int i = ; i<m; i++)
for (int j = ; j < n; j++) {
if(id[i][j])bfs(i, j);
}
for (int i = ; i < idx; i++) { f[i] = i; }
sort(e.begin(), e.end(),cmp);
int res = ;
for (int i = ; i < e.size(); i++) {
if (same(e[i].to, e[i].from)) continue;
un(e[i].to, e[i].from);
res += e[i].w;
}
cout << res << endl; }
system("pause");
}

POJ - 3026 Borg Maze bfs+最小生成树。的更多相关文章

  1. poj 3026 Borg Maze &lpar;bfs &plus; 最小生成树&rpar;

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

  2. poj 3026 Borg Maze &lpar;BFS &plus; Prim&rpar;

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  3. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

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

    Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...

  5. poj 3026 Borg Maze(最小生成树&plus;bfs)

    题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...

  6. poj 3026 Borg Maze bfs建图&plus;最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  7. POJ 3026 Borg Maze bfs&plus;Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

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

    https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...

  9. POJ 3026 Borg Maze【BFS&plus;最小生成树】

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

随机推荐

  1. windows

    1.拷贝远程文件 net use \\10.130.80.62\ipc$ 密码 /user:用户名 xcopy "\\10.130.80.62\G$\yt\apache-tomcat-7.0 ...

  2. Visual Studio各版本工程文件之间的转换

    转载于:http://www.cnblogs.com/jmliao/p/5594179.html 由于VS版本比较多,低版本无法直接打开高版本的工程文件,通过对工程文件进行一些修改可以解决这些问题. ...

  3. 深入浅出 RPC - 浅出篇&plus;深入篇

    摘自: http://blog.csdn.net/mindfloating/article/details/39473807 近几年的项目中,服务化和微服务化渐渐成为中大型分布式系统架构的主流方式,而 ...

  4. 【一】php 操作符

    1.php单引号和双引号的区别 单引号和双引号都能表示字符串,但是单引号不能识别里面带有转义字符'/'和变量的字符串,所以需要""去表示这种字符串.或者使用<<< ...

  5. ios中xib的使用介绍

    ios中Xib的使用 ios中xib的使用 Nib files are the quintessential(典型的) resource type used to create iOS and Mac ...

  6. 可爱的 Python &colon; Python中函数式编程,第一部分

    英文原文:Charming Python: Functional programming in Python, Part 1 摘要:虽然人们总把Python当作过程化的,面向对象的语言,但是他实际上包 ...

  7. Java ClassLoader 原理分析

    一.ClassLoader(类加载器)的作用 如果一个程序包含不止一个class文件,那么当程序启动时,带有main方法的类的class文件作为程序入口先被JVM加载,然后根据程序调用的需要,再逐步进 ...

  8. 最小的 Velocity 教程

    工作以后,我越来越能体会到80/20法则的强大. 这是一个不可否认的事实,常用 20% 的技术可以解决工作中 80% 的场景. 所以我希望能介绍给你 Velocity 技术 20%,帮助你胜任 80% ...

  9. Linux 显示文本指定行内容

    主要采用sed.head和tail命令 如果文本中使用了 \n 这类符号,cat命令会把它当成换行符,结果会出错 $ sed -n "10p" move.sh   # 显示第10行 ...

  10. Robot Framework自动化测试Selenium2Library库详细用法

    一.浏览器驱动 通过不同的浏览器执行脚本. Open Browser Htpp://www.xxx.com chrome 浏览器对应的关键字: firefox FireFox ff internete ...