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'点可以继续分组,但是在路上不能分组 于是就是明显 ...
随机推荐
- Union与UnionAll
UNION指令的目的是将两个SQL语句的结果合并起来.从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION的一个限制是两个 ...
- POJ3261 Milk Patterns(二分+后缀数组)
题目求最长的重复k次可重叠子串. 与POJ1743同理. 二分枚举ans判定是否成立 height分组,如果大于等于ans的组里的个数大于等于k-1,这个ans就可行 #include<cstd ...
- [xsy2213]tower
题意:给一个地图,地图上的每个位置是空地或一个炮或一些敌人,给定每个炮的方向(上/下/左/右),每个炮只能打一个位置且炮弹轨迹不能相交,问最多打到多少敌人 原题貌似是TC SRM 627的题,题解在这 ...
- linux命令详解:basename命令
转:http://www.cnblogs.com/lwgdream/archive/2013/11/05/3407768.html 前言 bashname命令用于获取路径中的文件名或路径名(获取的时候 ...
- java实现随机中文
原文:http://blog.csdn.net/u013926110/article/details/44600601 public class CreateCheckCode { /** * 生成随 ...
- 阅读 Android源码的一些姿势
日常开发中怎么阅读源码 找到正确的源码 IDE 是日常经常用的东西,Eclipse 就不说了,直接从 Android Studio(基于 IntelliJ Community 版本改造)开始. 我们平 ...
- android 代码覆盖率
背景 项目使用的是small插件.一个app分为main和多个插件,为了统计插件的代码覆盖率. 1 修改插件 修改插件build.gradle buildTypes { release { ... } ...
- IIS Express HTTP 错误 500.22
错误描述: HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置. 最可能的原因: 此应用程序在 system.we ...
- hive bucket
转载:https://www.cnblogs.com/end/archive/2013/01/09/2852413.html hive中table可以拆分成partition,table和partit ...
- jconsole使用记录
jconsole/JVisualVM连接linux服务器查看JVM使用情况 现需要在本地电脑上查看服务器的tomcat的整体的运行状态,使用jconsole工具. JMX配置 拷贝$JAVA_HOME ...