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'点可以继续分组,但是在路上不能分组 于是就是明显 ...
随机推荐
- 新博客:11101001.com
开了一个新blog 但还是会用这个写博客 新博客地址11101001.com
- [转]C++函数模板与模板函数
1.函数模板的声明和模板函数的生成 1.1函数模板的声明 函数模板可以用来创建一个通用的函数,以支持多种不同的形参,避免重载函数的函数体重复设计.它的最大特点是把函数使用的数据类型作为参数. ...
- VMware报错“原因: 未能锁定文件”,打开失败
原文:http://jingyan.baidu.com/article/425e69e6bf64dbbe15fc16fe.html VMware打开复制的虚拟机,报错“原因: 未能锁定文件”,打开失败 ...
- ubuntu14.04安装 chrome
安装谷歌浏览器,只需要三行代码: 打开终端,输入 cd /tmp 对于谷歌Chrome32位版本,使用如下链接: wget https://dl.google.com/linux/direct/goo ...
- api.js
ylbtech-JavaScript-util: api.js API 代理接口 1.A,JS-效果图返回顶部 1.B,JS-Source Code(源代码)返回顶部 1.B.1, m.yinta ...
- WIN7无法卸载掉中文繁体注音输入法
WIN7无法卸载掉中文繁体注音输入法 不知何时系统里被自动安装了个中文繁体的注音输入法,每次启动都会替换默认的简体搜狗拼音,而且最要命的是在输入法选择栏里面没有出现这个繁体的输入法,而任务栏里却总是有 ...
- Linux内核锁与中断处理
Linux内核锁 在Linux内核里面,一般采用了如下几种锁的机制,来保证多线程的同步与互斥: (1)原子操作 atomic_t v: void atomic_set(atomic_t *v, int ...
- python下性能提示
性能提示 3.1 嵌套if/else结构比一系列单选if结构块,因为只要有一个条件满足,其余测试就会终止. 3.2 在嵌套if/else结构中,把最可能成立的条件放在该嵌套结构的开始处.和把不常见的条 ...
- Python 面向对象二(转载)
来源:www.cnblogs.com/wupeiqi/p/4766801.html 三.类成员的修饰符 类的所有成员在上一步骤中已经做了详细的介绍,对于每一个类的成员而言都有两种形式: 1.公有成员, ...
- 2017.8.4 Creating Server TCP listening socket *:6379: bind: No such file or directory
启动redis时出现如下错误: 解决办法:按顺序输入如下命令就可以连接成功. 1. redis-cli.exe 2. shutdown 3. exit 4. redis-server.exe 参考来 ...