Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9718   Accepted: 3263

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

注意注意,本题两大神坑

1   数组题里面说的是50,其实开100都是Wa,我开到300就AC了

2 再输入完行与列后,不可以用getchar()在进行输入空行,,,,,,必须用gets(str[0]);

本题详解

在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。

根据题意的“分离”规则,重复走过的路不再计算

因此当使用prim算法求L的长度时,根据算法的特征恰好不用考虑这个问题(源点合并很好地解决了这个问题),L就是最少生成树的总权值W

由于使用prim算法求在最小生成树,因此无论哪个点做起点都是一样的,(通常选取第一个点),因此起点不是S也没有关系

所以所有的A和S都可以一视同仁,看成一模一样的顶点就可以了

最后要注意的就是 字符的输入

cin不读入空字符(包括 空格,换行等)

gets读入空格,但不读入换行符)

剩下的问题关键就是处理 任意两字母间的最短距离,由于存在了“墙#” ,这个距离不可能单纯地利用坐标加减去计算,必须额外考虑,推荐用BFS(广搜、宽搜),这是本题的唯一难点,因为prim根本直接套用就可以了

任意两字母间的最短距离 时不能直接用BFS求,

1、必须先把矩阵中每一个允许通行的格看做一个结点(就是在矩阵内所有非#的格都作为图M的一个顶点),对每一个结点i,分别用BFS求出它到其他所有结点的权值(包括其本身,为0),构造结点图M;

2、然后再加一个判断条件,从图M中抽取以字母为顶点的图,进而构造字母图N

这个判定条件就是当结点图M中的某点j为字母时,把i到j的权值再复制(不是抽离)出来,记录到字母图N的邻接矩阵中

3、剩下的就是对字母图N求最小生成树了

 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
char str[][];
int vis[],tvis[][],dis[],tdis[][];
int point[][];
int map[][];
struct node{
int x;
int y;
};
int tnext[][]={,,,,-,,,-};
int ans,x,y;
int prim(int u){
int sum=;
for(int i=;i<=ans;i++)
dis[i]=map[u][i];
vis[u]=;
for(int k=;k<ans;k++){
int tmin=;
int temp;
for(int j=;j<=ans;j++){
if(dis[j]<tmin&&!vis[j]){
tmin=dis[j];
temp=j;
}
}
sum+=tmin;
vis[temp]=;
for(int i=;i<=ans;i++){
if(dis[i]>map[temp][i]&&!vis[i])
dis[i]=map[temp][i];
} }
return sum;
}
void bfs(int tx,int ty){
memset(tvis,,sizeof(tvis));
memset(tdis,,sizeof(tdis));
queue<node>q;
node temp,next;
temp.x=tx;
temp.y=ty;
q.push(temp);
tvis[tx][ty]=;
int xx,yy;
while(!q.empty()){
temp=q.front();
q.pop();
if(point[temp.x][temp.y]){
map[point[tx][ty]][point[temp.x][temp.y]]=tdis[temp.x][temp.y];
} for(int k=;k<;k++){
next.x=xx=temp.x+tnext[k][];
next.y=yy=temp.y+tnext[k][];
if(xx>=&&xx<=x&&yy>=&&yy<=y&&!tvis[xx][yy]&&str[xx][yy]!='#'){
tdis[xx][yy]=tdis[temp.x][temp.y]+;
tvis[xx][yy]=;
q.push(next);
}
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
memset(point,,sizeof(point));
memset(str,,sizeof(str));
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
memset(map,,sizeof(map)); ans=; scanf("%d%d",&y,&x);
gets(str[]);
for(int i=;i<=x;i++){
gets(str[i]);
for(int j=;j<y;j++){
if(str[i][j]=='S'||str[i][j]=='A'){
point[i][j]=++ans;
}
}
}
for(int i=;i<=x;i++){
for(int j=;j<=y;j++){
if(point[i][j])
bfs(i,j);
}
}
printf("%d\n",prim());
}
return ;
}

poj 3026 bfs+prim Borg Maze的更多相关文章

  1. poj 3026(BFS+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12032   Accepted: 3932 Descri ...

  2. Borg Maze - poj 3026(BFS + Kruskal 算法)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 Description The B ...

  3. Borg Maze POJ - 3026 (BFS + 最小生成树)

    题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...

  4. 最小生成树+BFS J - Borg Maze

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

  5. poj 3026 Borg Maze (BFS + Prim)

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

  6. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

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

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

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

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

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

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

随机推荐

  1. 由DataGridTextColumn不能获取到父级DataContext引发的思考

    在项目中使用DataGrid需要根据业务动态隐藏某些列,思路都是给DataGrid中列的Visibility属性绑定值来实现(项目使用MVVM),如下 <DataGridTextColumn H ...

  2. C头文件之<cstring>

    (string.h) 这个文件夹主要是定义了几个对字符串和数组进行操作的函数.功能很强大.下面是重要函数: strcpy.strncpy strcpy,strncpy 这两个函数是对字符串的复制,很常 ...

  3. iOS边练边学--父子控件之作为导航控制器的子类产生的问题以及网易新闻练习

    一.导航控制器的子类 作为导航控制器的子类,并且是导航控制器子类中的第一个,系统会默认给子控件添加EdgeInsert属性,把导航栏的宽度挤出来.但是系统只会默认修改第一个. 解决办法1:系统帮忙给第 ...

  4. 【长期更新】--神犇的BLOGS(各种高端讲解)

    KMP字符串匹配算法: http://kb.cnblogs.com/page/176818/ http://blog.csdn.net/yutianzuijin/article/details/119 ...

  5. IIS7部署项目时提示:"错误消息 401.2。: 未经授权: 服务器配置导致登录失败。"的解决办法

    这个错误的定位:你的站点使用了Forms验证,而且在部署在生产环境的时候,设置错误,或者注释了. 解决方法如下: 1.检查Forms配置是否屏蔽. 2.有权限访问的资源是否已经开发. 基本就围绕以上两 ...

  6. ECSHOP Inject PHPCode Into \library\myship.php Via \admin\template.php && \includes\cls_template.php Vul Tag_PHP_Code Execute Getshell

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 PHP语言作为开源社区的一员,提供了各种模板引擎,如FastTemplate,Sm ...

  7. getchar() 和 scanf("%c")的区别

    getchar()和scanf("%c")的功能都是从STDIN读一个字符,单论功能两者没有区别. 但两者的返回值是有区别的: -------------------------- ...

  8. Codeforces 1C Ancient Berland Circus

    传送门 题意 给出一正多边形三顶点的坐标,求此正多边形的面积最小值. 分析 为了叙述方便,定义正多边形的单位圆心角u为正多边形的某条边对其外接圆的圆心角(即外接圆的某条弦所对的圆心角). (1)多边形 ...

  9. what linux java cpu 100% ?

    1.用top找到最耗资源的进程id [ bin]# toptop - 16:56:14 up 119 days, 6:17, 7 users, load average: 2.04, 2.07, 2. ...

  10. JavaScript的apply和call方法及其区别

    参考资料: http://blog.csdn.net/myhahaxiao/article/details/6952321 apply和call能“劫持”其他对象的方法来执行,其形参如下: apply ...