POJ - 3026 Borg Maze bfs+最小生成树。
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+最小生成树。的更多相关文章
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
- poj 3026 Borg Maze(最小生成树+bfs)
题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...
- poj 3026 Borg Maze bfs建图+最小生成树
题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...
- POJ 3026 Borg Maze bfs+Kruskal
题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...
- POJ - 3026 Borg Maze(最小生成树)
https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
随机推荐
- SpringBoot------全局异常捕获
1.添加异常类 package myshop.config; import javax.servlet.http.HttpServletRequest; import org.springframew ...
- VMware Playerでの仮想マシン起動エラー
Windows Updateすると.翌日VMware Playerの仮想マシン起動時に 「この仮想マシンを構成済み設定でパワーオンするのに十分な物理メモリがありません.」 のエラーとなることが時々あり ...
- SpringMVC由浅入深day02_4springmvc校验
4 springmvc校验 4.1 校验Validation理解 b/s系统中对http请求数据的校验多数在客户端进行,这也是出于简单及用户体验性上考虑,但是在一些安全性要求高的系统中服务端校验是不可 ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 多版本
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 多版本 当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间 ...
- SpringMVC -- 梗概--源码--贰--异常管理
附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...
- Go之Channel详解
channel取数据时未被赋值将被阻塞,可以使用这一机制来完成事件的监听 wg := new(sync.WaitGroup) wg.Add(2) event := make(chan bool) // ...
- codeforces水题100道 第十四题 Codeforces Round #321 (Div. 2) A. Kefa and First Steps (brute force)
题目链接:http://www.codeforces.com/problemset/problem/580/A题意:求最长连续非降子序列的长度.C++代码: #include <iostream ...
- 节日换肤通用技术方案__iOS端实现
一.问题的提出 不知道大家有没有发现, 元旦期间, 很多APP界面里的图标都换成了具有节日气氛的样式, 而在过了元旦节之后, 这些图标又悄无声息的变回了本来的面貌. 这些具有短暂生命周期.而又必须在固 ...
- VC利用调试寄存器实现硬件断点源码
[文章标题]:VC利用调试寄存器实现硬件断点源码 [文章作者]:yhswwr(SilenceRet) [作者QQ]:3412259 [编写语言]:C++ [使用工具]:VS2008.VC++9 [本文 ...
- 【Python3】端口占用监测的程序
#!/usr/bin/env python # -*- coding:utf- -*- #python端口占用监测的程序,该程序可以监测指定IP的端口是否被占用 import socket,time, ...