Borg Maze

题目链接:

http://acm.hust.edu.cn/vjudge/contest/124434#problem/I

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

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


##题意:

题目真是难读,翻译一遍都没看懂...
大意就是要求最小距离把图中的所有A和S都联通.


##题解:

先用bfs处理出任意两点间的最短距离.
再求一遍最小生成树即可.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 550
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

struct node{

int left,right,cost;

}road[maxn*maxn];

int cmp(node x,node y) {return x.cost<y.cost;}

int p[maxn*maxn],m,n;

int find(int x) {return p[x]=(p[x]==x? x:find(p[x]));}

int kruskal()

{

int ans=0;

for(int i=1;i<=n;i++) p[i]=i;

sort(road+1,road+m+1,cmp);

for(int i=1;i<=m;i++)

{

int x=find(road[i].left);

int y=find(road[i].right);

if(x!=y)

{

ans+=road[i].cost;

p[x]=y;

}

}

return ans;

}

void add_road(int u,int v,int w) {

road[++m].left = u;

road[m].right = v;

road[m].cost = w;

}

int maps[maxn][maxn];

struct Node{

int x,y;

int step;

};

bool vis[maxn][maxn];

int dir[4][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}};

void bfs(Node start)

{

queue q;

while(!q.empty()) q.pop();

memset(vis, 0, sizeof(vis));

q.push(start); vis[start.x][start.y] = 1;

while(!q.empty()) {
Node cur = q.front(); q.pop();
Node next;
for(int i=0; i<4; i++) {
next.x = cur.x + dir[i][0];
next.y = cur.y + dir[i][1];
if(maps[next.x][next.y] == -1 || vis[next.x][next.y]) continue;
vis[next.x][next.y] = 1;
next.step = cur.step + 1;
q.push(next);
if(maps[next.x][next.y] == 0) continue;
add_road(maps[start.x][start.y], maps[next.x][next.y], next.step);
}
}

}

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
while(t--)
{
m = 0; n = 0;
memset(road,0,sizeof(road));
int a,b; cin >> b >> a; memset(maps, -1, sizeof(maps));
for(int i=1; i<=a; i++) { char tmp[maxn]; gets(tmp);
for(int j=1; j<=b; j++) {
char c; c = getchar();
if(c == '#') maps[i][j] = -1;
else if(c == ' ') maps[i][j] = 0;
else maps[i][j] = ++n;
}
} for(int i=1; i<=a; i++)
for(int j=1; j<=b; j++) if(maps[i][j] > 0){
Node cur; cur.x = i; cur.y = j; cur.step = 0;
bfs(cur);
} int ans = kruskal(); printf("%d\n", ans);
} return 0;

}

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

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

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

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

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

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

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

  4. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  5. POJ 3026 Borg Maze【BFS+最小生成树】

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  6. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

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

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6634   Accepted: 2240 Descrip ...

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

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

  9. POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16625   Accepted: 5383 Descri ...

随机推荐

  1. NDK(18)使用C++ STL

    1,在Application.mk 中使用 APP_STL := stlport_static 等. APP_ABI := x86 armeabi APP_PLATFORM := android-15 ...

  2. tc 2014 college tour 250 500

    题意: You are given a long long n. Return the largest divisor of n that is a perfect square. That is, ...

  3. uva11181Probability|Given

    枚举,条件概率. 2^20次方等于100w,是大约可以没准还能过的. 二进制枚举时,如果买东西的人恰好为r个,设概率为p,就将sum[i]+=p(sum[i]为r个人买东西时第i个人买东西的概率),t ...

  4. 两个简单的python文件,实现删除本地文件夹和mongodb数据库的内容

    删除本地文件夹: import os , string , datetime ; str = '/home/niuguoqin/tmp/tomcat/'; b = (datetime.datetime ...

  5. 通过对源代码的反向工程学习CoreData架构

    在本文开始,先给出反向工程后的结果: 不过需要注意,三个实例的指针都被同一个实例拥有,比如三个指针都位于appDelegate. 在AppDelegate类中定义了下面三个属性: @property ...

  6. 实现推送功能APP端需要完成的工作

    推送功能简介 实现推送的流程如下: 从APP注册推送功能,到APNS服务器发送推送消息给设备,有五个步骤. 一旦推送注册完成,应用自身的服务器以provider的身份提供推送. APP端实现 在代码方 ...

  7. 转载RabbitMQ入门(4)--路由

    路由 (使用Java客户端) 在先前的指南中,我们建立了一个简单的日志系统.我们可以将我们的日志信息广播到多个接收者. 在这部分的指南中,我们将要往其中添加一个功能-让仅仅订阅一个消息的子集成为可能. ...

  8. Java中的面向接口编程

    面向接口编程是很多软件架构设计理论都倡导的编程方式,学习Java自然少不了这一部分,下面是我在学习过程中整理出来的关于如何在Java中实现面向接口编程的知识.分享出来,有不对之处还请大家指正. 接口体 ...

  9. function 中定义函数的默认返回值,

    result有默认值的int类型的为0,string类型的为‘’,tobject类型的为nil等等

  10. excel用vlookup,lookup时查询数据需要排序

    如题,排出来的数据不对,之前没有是看清楚..所以记下来,日是后要小心点....