Borg Maze---poj3026最小生成树+bfs
题目链接:http://poj.org/problem?id=3026
题意:可以理解为给你一个地图上面有S和A的表示一个一个的点,#表示墙,所以需要我们用bfs来计算各点之间的距离;
b[i][j] = p;代表map[i][j]的位置的点的编号为p;
D[i][j] 代表编号为i和j的距离;
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std; #define N 105
#define INF 0xfffffff
int dir[][] = { {, }, {-, }, {, }, {, -} };
int D[N][N], n, m, vis[N], used[N][N],b[N][N], dist[N];
char map[N][N]; struct node
{
int x, y, step;
} a[N]; void bfs(int s, int x, int y)
{
memset(used, , sizeof(used));
node p,q;
queue<node>Q;
p.x = x;
p.y = y;
p.step = ;
Q.push(p);
used[x][y] = ;
while(Q.size())
{
p = Q.front();
Q.pop();
if(map[p.x][p.y] >= 'A' && map[p.x][p.y] <= 'Z')
D[s][ b[p.x][p.y] ] = p.step;
for(int i=; i<; i++)
{
q.x = p.x + dir[i][];
q.y = p.y + dir[i][];
if(q.x<m && q.x>= && q.y>= && q.y<n && used[q.x][q.y]!= && map[q.x][q.y] != '#')
{
used[q.x][q.y] = ;
q.step = p.step + ;
Q.push(q);
}
}
}
} int Prim(int cnt)
{
int ans=;
vis[] = ;
for(int i=; i<=cnt; i++)
dist[i] = D[][i];
for(int i=; i<=cnt; i++)
{
int Min = INF, index = -;
for(int j=; j<=cnt; j++)
if(vis[j]== && Min > dist[j])
{
Min = dist[j];
index = j;
}
if(index==-)break;
ans += Min;
vis[index] = ;
for(int j=; j<=cnt; j++)
{
if(vis[j] == && dist[j] > D[index][j])
dist[j] = D[index][j];
}
}
return ans;
} int main()
{
int T, ans;
scanf("%d", &T);
while(T--)
{
memset(a, , sizeof(a));
memset(vis, , sizeof(vis)); memset(map, , sizeof(map));
for(int i=; i<N; i++)
{
dist[i] = INF;
for(int j=; j<N; j++)
D[i][j] = INF;
D[i][i] = ;
} int cnt = ;
scanf("%d%d ", &n,&m);
for(int i=; i<m; i++)
{
gets(map[i]);
for(int j=; j<n; j++)
{
if(map[i][j] <= 'Z' && map[i][j] >= 'A')
b[i][j] = cnt, cnt++;
}
}
for(int i=; i<m; i++)
{
for(int j=; j<n; j++)
{
if(map[i][j] <= 'Z' && map[i][j] >= 'A')
bfs(b[i][j], i, j);
}
}
ans = Prim(cnt-);
printf("%d\n", ans);
}
return ;
}
Borg Maze---poj3026最小生成树+bfs的更多相关文章
- poj 3026 Borg Maze(最小生成树+bfs)
题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
- 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...
- POJ 3026 Borg Maze 广搜(BFS)+最小生成树
题意:从S出发,去抓每一个A,求总路径最短长度.在S点和A点人可以分身成2人,不过一次只能让一个人走. 思路是先利用BFS求出各点之间的距离,建成图,再套用最小生成树模板. 一次性A了.不过觉得在判断 ...
- Borg Maze POJ - 3026 (BFS + 最小生成树)
题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...
- POJ - 3026 Borg Maze(最小生成树)
https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...
- Borg Maze - poj 3026(BFS + Kruskal 算法)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9821 Accepted: 3283 Description The B ...
- POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- POJ 3026 Borg Maze(Prim+bfs求各点间距离)
题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...
- J - Borg Maze
J - Borg Maze 思路:bfs+最小生成树. #include<queue> #include<cstdio> #include<cstring> #in ...
随机推荐
- 【django】Error: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试。
问题描述:启动django服务时出现“Error: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试.”的错误 问题原因:8000端口被占用了 解决办法:默认启动的 ...
- Nginx 72万连接性能测试(一)
转自:http://my.oschina.net/chenzhuo/blog/150200?p=2#comments 根据系统内存64G估算单台tengine做反向代理最高支持72万连接.为了验证达到 ...
- 《C++标准程序库》笔记之四
本篇博客笔记顺序大体按照<C++标准程序库(第1版)>各章节顺序编排. ---------------------------------------------------------- ...
- lua中的字符串操作(模式匹配)
(一). 模式匹配函数在string库中功能最强大的函数是:string.find(字符串查找)string.gsub(全局字符串替换)string.gfind(全局字符串查找)string.gmat ...
- 使用js是想防止表单重复提交的效果
直接上代码: <html> <head> <title>Form表单</title> <script type="text/javasc ...
- liunx trac 插件使用之GanttCalendarPlugin
http://trac-hacks.org/wiki/GanttCalendarPlugin官网上的说明很清楚,处理做几点提示,以做记录. 1.我的Trac版本是1.0.1 我使用了'B' Metho ...
- springbatch---->springbatch的使用(四)
这里我们重点学习一下springbatch里面的各种监听器的使用,以及job参数的传递.追求得到之日即其终止之时,寻觅的过程亦即失去的过程. springbatch的监听器 一.JOB LISTENE ...
- -webkit-transition -moz-transition transition
-webkit-transition -moz-transition transition 可以定义动画的变化时间曲线-webkit-transition-timing-function: ease ...
- [Error: Failed to find 'ANDROID_HOME' environment variable. Try setting setting it manually
7down voteaccepted I don't think its necessary to add everything into path.Just add the JAVA_HOME , ...
- LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)
题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...