Borg Maze

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18644   Accepted: 5990

题目链接http://poj.org/problem?id=3026

Description:

The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated subspace network that insures each member is given constant supervision and guidance.

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:

On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that 1 <= x,y <= 50. After this, y lines follow, each which x characters. For each character, a space `` '' stands for an open space, a hash mark ``#'' stands for an obstructing wall, the capital letter ``A'' stand for an alien, and the capital letter ``S'' stands for the start of the search. The perimeter of the maze is always closed, i.e., there is no way to get out from the coordinate of the ``S''. At most 100 aliens are present in the maze, and everyone is reachable.

Output:

For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.

Sample Input:

2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####

Sample Output:

8
11

题意:

从S点出发,目的是要到达所有外星人的位置。然后在起点或者有外星人的点,可以进行“分组”,也就是进行多条路径搜索。

最后的总代价定义为所有组行走的步数之和。比如这个组一开始走了5步,然后分为两组,各走三步,最终总代价为11步。

问的就是所有外星人位置被到达后的最小总代价为多少。

题解:

这个看似是搜索...也很像是搜索。如果不是在最小生成树专题里面,我估计也不会想到最小生成树。

其实这个题如果能够想到最小生成树就简单了,最终的目的转化为S以及所有的A都连通嘛,并且最小代价。

那么就直接先bfs求出两点间的最短距离,然后根据距离信息建图,跑最小生成树就行了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
int t,n,m;
int num[N][N],d[N][N];
char mp[N][N];
int dx[]={,-,,},dy[]={,,,-};
struct Edge{
int u,v,w;
bool operator < (const Edge &A)const{
return w<A.w;
}
}e[N*N<<];
struct node{
int x,y;
};
int f[N*N];
int tot,cnt;
int find(int x){
return f[x]==x?f[x]:f[x]=find(f[x]);
}
int Kruskal(){
int ans=;
for(int i=;i<=;i++) f[i]=i;
for(int i=;i<=tot;i++){
int fx=find(e[i].u),fy=find(e[i].v);
if(fx==fy) continue ;
f[fx]=fy;
ans+=e[i].w;
}
return ans ;
}
bool ok(int x,int y){
return x>= && x<=n && y>= && y<=m && mp[x][y]!='#';
}
void bfs(int sx,int sy){
queue <node> q;
memset(d,INF,sizeof(d));d[sx][sy]=;
node now;
now.x=sx;now.y=sy;
q.push(now);
while(!q.empty()){
node cur = q.front();q.pop();
for(int i=;i<;i++){
int x=cur.x+dx[i],y=cur.y+dy[i];
if(!ok(x,y)||d[x][y]<=d[cur.x][cur.y]+) continue ;
d[x][y]=d[cur.x][cur.y]+;
now.x=x;now.y=y;
q.push(now);
if(mp[x][y]=='A'||mp[x][y]=='S'){
e[++tot].u=num[sx][sy];e[tot].v=num[x][y];e[tot].w=d[x][y];
}
}
}
}
int main(){
cin>>t;
while(t--){
scanf("%d%d",&m,&n);
tot = cnt = ;
char c;
while(){
scanf("%c",&c);
if(c!=' ') break ;
}
int first=;
memset(num,,sizeof(num));
for(int i=;i<=n;i++){
getchar();
first=;
for(int j=;j<=m;j++){
scanf("%c",&mp[i][j]);
if(mp[i][j]=='S'||mp[i][j]=='A') num[i][j]=++cnt;
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(num[i][j]) bfs(i,j);
}
}
sort(e+,e+tot+);
int ans = Kruskal();
cout<<ans<<endl;
}
return ;
}

POJ3026:Borg Maze (最小生成树)的更多相关文章

  1. POJ3026 Borg Maze(最小生成树)

    题目链接. 题目大意: 任意两点(点表示字母)可以连线,求使所有点连通,且权值和最小. 分析: 第一感觉使3维的BFS.但写着写着,发现不对. 应当用最小生成树解法.把每个字母(即A,或S)看成一个结 ...

  2. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

  3. poj 3026 Borg Maze 最小生成树 + 广搜

    点击打开链接 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7097   Accepted: 2389 ...

  4. POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14165   Accepted: 4619 Descri ...

  5. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

  6. POJ3026 Borg Maze(Prim)(BFS)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12729   Accepted: 4153 Descri ...

  7. POJ3026 Borg Maze(bfs求边+最小生成树)

    Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...

  8. (POJ 3026) Borg Maze 最小生成树+bfs

    题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced ...

  9. poj 3026 Borg Maze (最小生成树+bfs)

    有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...

随机推荐

  1. 【转】: 《江湖X》开发笔谈 - 热更新框架

    前言 大家好,我们这期继续借着我们工作室正在运营的在线游戏<江湖X>来谈一下热更新机制以及我们的理解和解决方案.这里先简单的介绍一下热更新的概念,熟悉这部分的朋友可以跳过,直接看我们的方案 ...

  2. Leetcode - 461. Hamming Distance n&=(n-1) (C++)

    1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...

  3. Alpha阶段中间产物

    空天猎功能说明书:https://git.coding.net/liusx0303/Plane.git 空天猎代码控制:https://coding.net/u/MR__Chen/p/SkyHunte ...

  4. Daily Scrum 11

    今天我们小组开会内容分为以下部分: part 1: 针对学长的搜索算法进行优化,每人发表自己的看法; part 2:对积分系统.防滥用.搜索算法优化部分代码任务的讨论和分工: part 3:进行明日的 ...

  5. EasyJSWebView原理分析

    概述 在iOS6之前,native只能调用webiew里的js代码,官方没有提供js调用native方法的接口.到了iOS7,官方提供了JSContext用来与js交互,native和js可以双向调用 ...

  6. lintcode-178-图是否是树

    178-图是否是树 给出 n 个节点,标号分别从 0 到 n - 1 并且给出一个 无向 边的列表 (给出每条边的两个顶点), 写一个函数去判断这张`无向`图是否是一棵树 注意事项 你可以假设我们不会 ...

  7. FromHandle临时对象一探究竟

    我们在调用CWnd::GetDlgItem()函数时,MSDN告诉我们:The returned pointer may be temporary and should not be stored f ...

  8. iOS 出现错误reason: image not found的解决方案

    在制作framework时遇到真机运行时导致的reason: image not found允许崩溃的问题,下面是我的解决方案: 首先我们分析一下出现这种情况的原因,原因就是framework找不到镜 ...

  9. html 怎么去掉网页的滚动条

    <style type="text/css"> body{ overflow:scroll; overflow-x:hidden; } </style> 这 ...

  10. Android 多屏幕适配 dp和px的关系 最好用dp

    Android 多屏幕适配 dp和px的关系 一直以来别人经常问我,android的多屏幕适配到底是怎么弄,我也不知道如何讲解清楚,或许自己也是挺迷糊. 以下得出的结论主要是结合官方文档进行分析的ht ...