链接:



Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6905   Accepted: 2315

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

Source


题意:

从 S 出发,去抓每一个 A ,求最小的总路径长度
            空格是可以走的地方,# 不可以走,四周都是 #
            注意:只可以在 S 或 A 的地方分开走, 空格区域不可拆分

               

算法:最小生成树+BFS


思路:

建图转换成最小生成树,然后直接套用Prime或者Kruskal模板

   由于只可以在 S 和 A 部分分开走 , 那么对于 S 和 A 都是一样的,都把它们当成是最小生成树的点
           建图时先用 BFS 遍历 S 和每一个 A 找到它们与其他的点的最短距离,再套用模板就好了

PS:遇到一个点【S 或 A】就遍历一遍 BFS 其实如果它之前有点被bfs 过,那么会重复求了路径,浪费了时间,看着很不和谐
        不过对于这题也没什么好方法解决了Orz。。。

坑:

输入列和行后有很多空格不能用getchar()  WA了三次,看了讨论区才知道, 用 gets() 就过了

Kruskal:

3026 Accepted 352K 63MS C++ 2634B 2013-07-31 14:44:02

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = 50+10;
const int maxp = 100+10;
int n, m; int map[maxn][maxn]; //输入的图
int w[maxp][maxp]; //路径
int v[maxn][maxn]; // 标记是否访问
int p[maxp]; // 父亲节点 int dir[4][2] = {0,1, 1,0, 0,-1, -1,0}; // 四个方向 struct Edge{
int u,v;
int w;
}edge[maxp*maxp]; struct Point{
int x,y;
int step;
}; void bfs(int x, int y) // 遍历第 map[x][y] 个点
{
Point point;
point.x = x; point.y = y; point.step = 0; //自己到自己距离为 0 queue<Point> q;
q.push(point); //起点入队 memset(v, 0, sizeof(v));
v[x][y] = 1; //标记访问
int num = 1; //已经找的点数 while(!q.empty())
{
Point now = q.front(); //取队首
q.pop(); // 出队
Point next; //找下一个点 for(int i = 0; i < 4; i++) // 遍历四个方向找下一个点
{
next.x = now.x+dir[i][0];
next.y = now.y+dir[i][1];
next.step = now.step+1; // 步数+1 //如果可以走, 并且没有被访问过
if(map[next.x][next.y] >= 0 && !v[next.x][next.y])
{
q.push(next); //入队
v[next.x][next.y] = 1; // 标记被访问 if(map[next.x][next.y] > 0) // 如果是要找的点
{//建图
edge[m].u = map[x][y];
edge[m].v = map[next.x][next.y];
edge[m++].w = next.step;
num++;
if(num == n) return; //所有的点都找完了
}
}
}
}
return;
} bool cmp(Edge a, Edge b)
{
return a.w < b.w;
} int find(int x)
{
return x == p[x] ? x : p[x] = find(p[x]);
} int Kruskal()
{
int ans = 0;
for(int i = 1; i <= n; i++) p[i] = i;
sort(edge, edge+m, cmp); for(int i = 0; i < m; i++)
{
int u = find(edge[i].u);
int v = find(edge[i].v); if(u != v)
{
p[v] = u;
ans += edge[i].w;
}
} return ans;
} int main()
{
int T;
int row,col;
char tmp[maxn];
char c;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &col,&row);
gets(tmp);//坑【一串空格】 n = m = 0; for(int i = 1; i <= row; i++)
{
for(int j = 1; j <= col; j++)
{
scanf("%c", &c);
if(c == '#') map[i][j] = -1;
else if(c == ' ') map[i][j] = 0;
else map[i][j] = ++n;
}
getchar();
} for(int i = 0; i <= row; i++)
{
for(int j = 0; j <= col; j++)
if(map[i][j] > 0)
bfs(i,j);
} int ans = Kruskal();
printf("%d\n", ans);
}
return 0;
}


Prime:

3026 Accepted 264K 63MS C++ 2543B 2013-07-31 14:44:34


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = 50+10;
const int maxp = 110;
const int INF = maxn*maxn;
int n; int map[maxn][maxn];
int w[maxp][maxp];
int d[maxp];
int vis[maxp];
int v[maxn][maxn]; struct Point{
int x,y;
int step;
}; int dir[4][2] = {0,1, 1,0, 0,-1, -1,0}; void bfs(int x, int y)
{
Point point;
point.x = x; point.y = y;
point.step = 0; memset(v,0,sizeof(v));
queue<Point> q;
q.push(point);
v[x][y] = 1;
int num = 1; while(!q.empty())
{
Point now = q.front();
q.pop(); Point next;
for(int i = 0; i < 4; i++)
{
next.x = now.x+dir[i][0];
next.y = now.y+dir[i][1]; if(map[next.x][next.y] >= 0 && !v[next.x][next.y])
{
next.step = now.step+1;
v[next.x][next.y] = 1;
q.push(next);
if(map[next.x][next.y] > 0)
{
int u = map[x][y];
int v = map[next.x][next.y];
w[u][v] = next.step;
num++;
if(num == n) return;
}
}
}
}
return;
} int Prime()
{
int ans = 0;
for(int i = 1; i <= n; i++) d[i] = INF;
d[1] = 0;
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++)
{
int x, m = INF;
for(int y = 1; y <= n; y++) if(!vis[y] && d[y] <= m) m = d[x=y];
vis[x] = 1; ans += d[x];
for(int y = 1; y <= n; y++) if(!vis[y])
d[y] = min(d[y], w[x][y]);
}
return ans;
} int main()
{
int T;
int row, col;
scanf("%d", &T);
while(T--)
{
n = 0;
char c;
scanf("%d%d", &col,&row);
char tmp[51];
gets(tmp);
for(int i = 1; i <= row; i++)
{
for(int j = 1; j <= col; j++)
{
scanf("%c", &c);
if(c == '#') map[i][j] = -1;
else if(c == ' ') map[i][j] = 0;
else map[i][j] = ++n;
}
getchar();
} for(int i = 1; i <= row; i++)
{
for(int j = 1; j <= col; j++)
if(map[i][j] > 0)
bfs(i,j);
} int ans = Prime();
printf("%d \n", ans);
}
return 0;
}














POJ 3026 Borg Maze【BFS+最小生成树】的更多相关文章

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

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

  2. POJ - 3026 Borg Maze bfs+最小生成树。

    http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...

  3. poj 3026 Borg Maze (BFS + Prim)

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

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

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

  5. POJ 3026 Borg Maze (最小生成树)

    Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...

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

    题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...

  7. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  8. POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

  9. POJ - 3026 Borg Maze(最小生成树)

    https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...

随机推荐

  1. 如何将微信小程序页面内容充满整个屏幕

    修改该页面的wxss文件 /* pages/weather/weather.wxss */ .weather{ position: fixed; height: 100%; width: 100%; ...

  2. POJO百度百科

    POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称. 使用POJO名称是为了避免和EJB混淆起来, ...

  3. jQuery (样式篇)

    1.$(document).ready 的作用是等页面的文档(document)中的节点都加载完毕后,再执行后续的代码,因为我们在执行代码的时候,可能会依赖页面的某一个元素,我们要确保这个元素真正的的 ...

  4. 怎样写APP计划书-20150313早读课

    我们每天都会收到拥有APP创意的人们的电话和邮件,他们想知道把这样的APP做出来需要多少钱.在Calvium,我们尽可能帮助他们,但有时候 做这样的报价真的很难.询问一款APP的价值,就和询问一条绳子 ...

  5. kinect脸部三维数据特征点标签语义具体说明

    非常多零零碎碎的事情,导致非常久没写blog了.face animation的demo做完了也快一个月了.是时候总结总结了. Kinect获得的标识点共用121个.其给的sdk里面也给出了响应的标签. ...

  6. 每日一个机器学习算法——k近邻分类

    K近邻很简单. 简而言之,对于未知类的样本,按照某种计算距离找出它在训练集中的k个最近邻,如果k个近邻中多数样本属于哪个类别,就将它判决为那一个类别. 由于采用k投票机制,所以能够减小噪声的影响. 由 ...

  7. SSH——增删改的实现一

    在上一节介绍了关于BOS项目底层的查询操作,接下来介绍一下curd里的其他三项操作步骤 一. 取派员添加 利用easyui在staff.jsp页面里构造添加页面(相关JavaBean创建步骤省略) & ...

  8. MATLAB 的字符串分析

    MATLAB的字符串分析. 字符串实际上是指1Xn 的字符数组. MATLAB软件具有强大的字符串处理功能,提供了很多的字符或字符串处理函数,包括字符串的创建.字符串的属性.比较.查找以及字符串的转换 ...

  9. CentOS系统时间与网络同步

    新装的CentOS系统server可能设置了错误的,须要调整时区并调整时间.例如以下是CentOS系统使用NTP来从一个时间server同步: 第一步: 把当前时区调整为上海就是+8区,想改其它时区也 ...

  10. 手动grub引导redhat

    grub是redhat默认的引导程序,在安装redhat时会提示是否安装bootloader,但自己手贱选择不安装,待系统重启时就是grub命令行界面,不能直接进系统.瞬时感觉麻烦大了,只能手动输入咯 ...