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'点可以继续分组,但是在路上不能分组 于是就是明显 ...
随机推荐
- Nginx的proxy_pass及upstream的小型负载均衡
proxy_pass Nginx的proxy_pass将请求代理到其他的后端服务器.例如 listen 9999; server_name wyc.com; location /test/aaa { ...
- 六. 异常处理4.try和catch的使用
尽管由Java运行时系统提供的默认异常处理程序对于调试是很有用的,但通常你希望自己处理异常.这样做有两个好处.第一,它允许你修正错误.第二,它防止程序自动终止.大多数用户对于在程序终止运行和在无论何时 ...
- 软路由OpenWrt教程收集(插件开发教程,opkg安装软件教程)
说明:现在几乎所有家庭级别的路由器都是基于OpenWrt进行衍生搭建的. https://openwrt.io/(极路由HiWifi创建的开源站点,极路由系统有这个衍生而来) http://www.o ...
- Java 并发工具包 java.util.concurrent 用户指南(转)
本文转自http://blog.csdn.net/defonds/article/details/44021605/ 感谢作者 1. java.util.concurrent - Java 并发工具包 ...
- JAVA常见算法题(二十四)
package com.xiaowu.demo; //一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. public class Demo24 { public ...
- CKEditor+SWFUpload实现功能较为强大的编辑器(二)---SWFUpload配置
在前面配置完CKEditor之后,就可以拥有一个功能挺强大的编辑器了 但是现在还不够,还要能够在发表文字中插入自己电脑上的图片 CKEditor自己好像有这个功能,但是实在是...没法说,很难用(这是 ...
- input 中 datetime-local 方法
<input type=" datetime-local "> 这个标签是H5新增的对象方法 能把现有的时间赋值给它 但是注意:必须是 yyyy-MM-ddTHH: ...
- 转:集成平台 jira 的使用与方案
http://wiki.csdn.net/pages/viewpage.action?pageId=1868089 作者:Martin Seibert SEIBERT MEDIA 首席执行官. 原文地 ...
- debug模式下dlgdata.cpp line 43 断言失败
我在VC6下显示Line 43, Line 624行失败 网上有Line 40行猜测是其他版本 运行程序出错,定位如下: HWND CDataExchange::PrepareCtrl(int nID ...
- Windows服务器SYSTEM权限Webshell无法添加3389账户情况突破总结
转自:http://bbs.blackbap.org/thread-2331-1-1.html 近好多Silic的朋友在Windows下SYSTEM权限的php webshell下添加账户,但是却无法 ...