(最小生成树) Borg Maze -- POJ -- 3026
链接:
http://poj.org/problem?id=3026
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/J
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10713 | Accepted: 3559 |
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<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int maxn = ;
const int oo = 0xfffffff; int dir[][] = { {,},{,},{-,},{,-} };
char G[maxn][maxn]; //保存地图
int D[maxn][maxn]; //记录两点间的距离
int use[maxn][maxn]; //标记地图
int Index[maxn][maxn]; //记录‘A’或者‘B’的编号
struct node{int x, y, step;}; void BFS(int k, int M,int N, int x, int y)
{
queue<node> Q;
node s;
s.x = x, s.y = y, s.step = ;
use[s.x][s.y] = k; Q.push(s); while(Q.size())
{
s = Q.front();Q.pop();
if(G[s.x][s.y]>='A' && G[s.x][s.y] <='Z')
D[k][ Index[s.x][s.y] ] = s.step; for(int i=; i<; i++)
{
node q = s;
q.x += dir[i][], q.y += dir[i][]; if(q.x>=&&q.x<M && q.y>=&&q.y<N && G[q.x][q.y]!='#' && use[q.x][q.y]!=k)
{
use[q.x][q.y] = k;
q.step += ;
Q.push(q);
}
}
}
}
int Prim(int N) //这里面的N代表编号最多到N
{
int i, dist[maxn], vis[maxn]={, };
int ans = , T=N-; for(i=; i<=N; i++)
dist[i] = D[][i]; while(T--)
{
int k=, mini = oo; for(i=; i<=N; i++)
{
if(!vis[i] && mini > dist[i])
mini = dist[i], k=i;
}
ans += mini;
vis[k] = true; for(i=; i<=N; i++)
if(!vis[i])dist[i] = min(dist[i], D[k][i]);
} return ans;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
int i, j, M, N, t=; scanf("%d%d ", &N, &M); for(i=; i<M; i++)
{
gets(G[i]);
for(j=; j<N; j++)
{
if(G[i][j]>='A' && G[i][j]<='Z')
Index[i][j] = t++;
use[i][j] = ;
}
} for(i=; i<M; i++)
for(j=; j<=N; j++)
{
if(G[i][j]>='A' && G[i][j]<='Z')
BFS(Index[i][j], M, N, i, j);
} int ans = Prim(t-); printf("%d\n", ans);
} return ;
}
(最小生成树) Borg Maze -- POJ -- 3026的更多相关文章
- 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 ...
- Borg Maze - poj 3026(BFS + Kruskal 算法)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9821 Accepted: 3283 Description The B ...
- J - Borg Maze - poj 3026(BFS+prim)
在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
随机推荐
- How to Pronounce WH Words — what, why, which
How to Pronounce WH Words — what, why, which Share Tweet Share Have you noticed that there are two d ...
- 【完结汇总】iKcamp出品基于Koa2搭建Node.js实战共十一堂课(含视频)
- Git reset head revert 回滚
Overview 涉及Git一些日常操作 :) 基础知识 <Pro Git>至少了解branch,commit的概念,及基本的原理 Git常用魔法 存档:master代码回滚方法 我是QA ...
- jQuery源码解读二(apply和call)
一.apply方法和call方法的用法: apply方法: 语法:apply(thisObj,[,argArray]) 定义:应用某一对象的一个方法,用另一个对象替换当前对象. 说明:如果argArr ...
- Animation.wrapMode循环模式
WrapMode.Default:从动画剪辑中读取循环模式(默认是Once). WrapMode.Once:当时间播放到末尾的时候停止动画的播放. WrapMode.Loop:当时间播放到末尾的时候重 ...
- jquery 动态添加的代码不能触发绑定事件
今天发现jQuery对动态添加的元素不触发事件,比如blur.click事件等 参考文章证明了我的结论,并给出了原因及解决方案 原因:程序找不到动态添加的节点. 解决方案:在绑定父元素后的子元素 $( ...
- [leetcode]392. Is Subsequence 验证子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- curl学习(实例不断总结)
1.先来一个简单的案例,请求http协议的网站 // 初始化一个 cURL 对象 $curl = curl_init(); // 设置你需要抓取的URL curl_setopt($curl, CURL ...
- linux的ssh服务
1.检查是否安装ssh > rpm -qa|grep ssh 2.安装ssh服务 > yum install ssh 配置 /etc/ssh/sshd_config 端口 22 3.启动s ...
- 有关gitlab的神秘操作.....version&&domain设置...
在使用gitlab的时候,如果服务器IP变动,之前的domain写入了配置文件了,如下路径: [root@gitlab-server ~]# vim /var/opt/gitlab/gitlab-ra ...