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. MacOs执行SQL出错(mysql)

    上次修改过root密码之后,刚启动,执行sql就报错了. 错误是: Reset MySQL root password using ALTER USER statement after install ...

  2. 怎样完整地离线更新并升级基于 Debian 的操作系统

    不久之前我已经向你展示了如何在任意离线的 Ubuntu 和 Arch Linux 操作系统上安装软件. 今天,我们将会看看如何完整地离线更新并升级基于 Debian 的操作系统. 和之前所述方法的不同 ...

  3. Vue-cli 配置开发环境让测试服务器监听所有IP

    //config/inex.js // Various Dev Server settingshost: '0.0.0.0', // can be overwritten by process.env ...

  4. [SoapUI] Datasink怎么显示每次循环的结果

    https://www.soapui.org/reporting/the-report-datasink.html

  5. Jmeter If Controller中设置多个条件用“与”进行连接

    "${noteID}"!="NOT FOUND" && "${securitiesId}"!="0P00011FQ ...

  6. 05 数据库入门学习-正则表达式、用户管理、pymysql模块

    一.正则表达式 正则表达式用于模糊查询,模糊查询已经讲过了 like 仅支持 % 和 _ 远没有正则表达式灵活当然绝大多数情况下 like足够使用 #语法 select *from table whe ...

  7. 2018.09.13 poj2728Desert King(最优比率生成树)

    传送门 01分数规划经典题. 不过用krsukal会T掉. 这题用prim反而更快(毕竟是完全图) 因此直接二分+最小生成树搞定. 代码: #include<iostream> #incl ...

  8. hdu - 1072(dfs剪枝或bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...

  9. Part 1 - Getting Started(1-3)

    https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-beginners-guide-to-django-part-1. ...

  10. Linux Vim替换字符串的一些方法小结

    使用Linux环境 进行开发工作的程序猿经常有编辑器之争,是vim牛还是emacs棒.二者都是程序猿的开发神器,不管用好哪一个都会使你的工作事半功倍. 本文重点介绍 Vim的替换字符串方法技巧,这些方 ...