Borg Maze

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12952   Accepted: 4227

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点出发,把图中所有A字符连通的最短路径 思路:因为连通所有字符,想到用Prim算法,构造最小生成树,但是我们需要各个点的距离关系
所以再用bfs求各个点的之间的距离。注意的是不要一个一个的求,否则很可能会超时,把一个点
到其他所有点的距离一次求完,也就是每一次都遍历整个图 代码如下:
 #include <iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxs = ;
char a[maxs][maxs];
struct Point
{
int col;
int row;
int step;
}node[maxs];
int col,row,nums;//nums需要被连通的所有点的个数
int edge[maxs][maxs];
int dir[][]={{-,},{,},{,-},{,}};//上下左右
bool judge(Point point)
{
if(point.col>&&point.col<=col&&point.row>&&point.row<=row&&a[point.row][point.col]!='#')
return true;
return false;
} void bfs(int i)
{
bool vis2[maxs][maxs];
int dist[maxs][maxs];//用来打表
memset(vis2,false,sizeof(vis2));
queue<Point> q;
node[i].step=;
q.push(node[i]);
vis2[node[i].row][node[i].col]=true;
Point cur,next;
while(!q.empty())
{
cur = q.front();
q.pop();
for(int k=;k<;k++)
{
next.row=cur.row+dir[k][];
next.col = cur.col+dir[k][];
if(!vis2[next.row][next.col]&&judge(next))
{
next.step=cur.step+;
vis2[next.row][next.col]=true;
q.push(next);
if(a[next.row][next.col]=='A')
dist[next.row][next.col]=next.step;
}
}
}
for(int j=;j<=nums;j++)
{
int d = dist[node[j].row][node[j].col];
edge[i][j]=d;
edge[j][i]=d;
}
}
int prim()
{
bool vis[maxs];
memset(vis,false,sizeof(vis));
vis[]=true;
int dist[maxs],ans=;
for(int i=;i<=nums;i++)
dist[i]=edge[][i];
for(int i=;i<=nums;i++)
{
int mins = INF,k=;
for(int j=;j<=nums;j++)
if(!vis[j]&&dist[j]<mins)
{
mins = dist[j];
k=j;
}
if(mins!=INF)
ans+=mins;
vis[k]=true;
for(int j=;j<=nums;j++)
if(!vis[j]&&dist[j]>edge[k][j])
dist[j]=edge[k][j];
}
return ans;
}
int main()
{
freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
nums=;
memset(node,,sizeof(node));
memset(a,,sizeof(a));
scanf("%d%d",&col,&row);
char s[];
for(int i=;i<=row;i++)
{
gets(s);
for(int j=;j<=col;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='S')
{
node[].row=i;node[].col=j;
}
else if(a[i][j]=='A')
{
node[++nums].row=i;node[nums].col=j;
}
}
}
for(int i=;i<=nums;i++)
{
edge[i][i]=;
bfs(i);
}
printf("%d\n",prim());
}
return ;
}
 

poj3026的更多相关文章

  1. POJ3026 最小生成树

    问题: POJ3026 分析: 采用BFS算出两两之间的距离,再用PRIM算法计算最小生成树. AC代码: //Memory: 220K Time: 32MS #include <iostrea ...

  2. POJ-3026 Borg Maze---BFS预处理+最小生成树

    题目链接: https://vjudge.net/problem/POJ-3026 题目大意: 在一个y行 x列的迷宫中,有可行走的通路空格' ',不可行走的墙'#',还有两种英文字母A和S,现在从S ...

  3. POJ-3026(图上的最小生成树+prim算法+gets函数使用)

    Borg Maze POJ-3026 一开始看到这题是没有思路的,看了题解才知道和最小生成树有关系. 题目的意思是每次走到一个A或者S就可以分为多个部分继续进行搜索.这里就可以看出是从该点分出去的不同 ...

  4. poj3026(bfs+prim)

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

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

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

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

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

  7. POJ3026(BFS + prim)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10554   Accepted: 3501 Descri ...

  8. 快速切题 poj3026

    感受到出题人深深的~恶意 这提醒人们以后...数字后面要用gets~不要getchar 此外..不要相信那个100? Borg Maze Time Limit: 1000MS   Memory Lim ...

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

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

随机推荐

  1. Golang之Mysql操作

    话说当年武大郎对着电脑一顿噼里啪啦,,,对mysql增删改查 增加insert package main import ( "fmt" "github.com/jmoir ...

  2. 20172325 2017-2018-2 《Java程序设计》第七周学习总结

    20172325 2017-2018-2 <Java程序设计>第七周学习总结 教材学习内容总结 1.创建子类 (1) 子类是父类更具体的版本,但子类的实例化不依赖于父类: (2) 继承有单 ...

  3. hook进程

    https://www.cnblogs.com/Leo_wl/p/3311279.html https://blog.csdn.net/u013761036/article/details/65465 ...

  4. Laravel 的 Events(事件) 及 Observers(观察者)

    你是否听说过单一职责原则(single responsibility principle)?我希望是的.它是程序设计的基本原则之一,它基本上的意思就是,一个类有且只有一个职责.换句话说,一个类必须且只 ...

  5. 检测空值,以及会不会出现mapping类型不一致的问题

    /// <summary> /// 检测空值,以及会不会出现mapping类型不一致的问题 /// </summary> /// <typeparam name=&quo ...

  6. 原生 JS 实现移动端 Touch 滑动反弹

    什么是 Touch滑动?就是类似于 PC端的滚动事件,但是在移动端是没有滚动事件的,所以就要用到 Touch事件结合 js去实现,效果如下: 1. 准备工作 什么是移动端的 Touch事件?在移动端 ...

  7. 2018.10.08 NOIP模拟 斐波那契(贪心+hash/map)

    传送门 签到题. 显然是可以贪心分组的,也就是尽量跟当前的分成一组. 这时我们需要判断a[l]+a[r],a[l+1]+a[r]...a[r−1]+a[r]a[l]+a[r],a[l+1]+a[r]. ...

  8. js级联出生日期

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. webuploader在ie7下的flash模式的使用

    webuploader在ie7下不能使用h5模式上传图片,只能使用flash模式. 但是出现了几个问题:(1)必须正确的引入.swf文件,才能使webuploader正常运行             ...

  10. 11) 生成可执行jar文件 maven-shade-plugin

    搜索 site:maven.apache.org maven-assembly-plugin http://maven.apache.org/plugins/maven-assembly-plugin ...