Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7844   Accepted: 2623

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
#include<queue>
#include<cstdio>
#define INF 1<<30
#include<cstring>
#include<algorithm>
using namespace std; int n,m,ans,cnt;
struct Node
{
int x,y,step;
};
Node node[];
int row,line;
int dist[];
char Map[][];
bool vis[][],vist[]; //邻接矩阵存储
int mark[][],dis[][]; //给每个字母一个编号
int dir[][]={-,, ,, ,, ,-}; void bfs(int index)
{
queue<Node> que;
memset(vis, false, sizeof(vis));
Node now, next;
node[index].step = ;
que.push(node[index]);
vis[node[index].x][node[index].y] = true;
while(!que.empty())
{
now = que.front();
que.pop();
int x = now.x, y = now.y;
for(int i = ; i < ; ++i)
{
int tx = x + dir[i][], ty = y +dir[i][];
if(vis[tx][ty] == false && Map[tx][ty] != '#') //如果这一步可以走
{
next.x = tx;
next.y = ty;
vis[tx][ty] = true;
next.step = now.step + ;
que.push(next);
if(Map[next.x][next.y] == 'A' || Map[next.x][next.y] == 'S')
dis[ mark[ node[index].x ][ node[index].y ] ][ mark[next.x][next.y] ] = next.step;
} }
}
} int prim()
{
for(int i = ; i < cnt; ++i)
{
dist[i] = INF;
vist[i] = false;
}
dist[] = ;
while()
{
int min = INF, now = -;
for(int i = ; i < cnt; ++i)
{
if(min > dist[i] && vist[i] == false)
{
min = dist[i];
now = i;
}
}
if(now == -)
return ans;
ans += min;
vist[now] = true;
for(int i = ; i < cnt; ++i)
if(vist[i] == false && dist[i] > dis[now][i])
dist[i] = dis[now][i];
}
return ans;
} int main()
{
int n_case;
scanf("%d", &n_case);
while(n_case--)
{
cnt = ans = ;
memset(mark, , sizeof(mark));
scanf("%d%d", &row, &line);
char ch;
while(ch = getchar(), ch != '\n');
for(int i = ; i < line; ++i)
{
for(int j = ; j < row; ++j)
{
Map[i][j] = getchar();
if(Map[i][j] == 'A' || Map[i][j] == 'S')
{
mark[i][j] = cnt;
node[cnt].x = i;
node[cnt++].y = j;
}
}
while(ch = getchar(), ch != '\n');
}
for(int i = ; i < cnt; ++i)
bfs(i);
prim();
printf("%d\n", ans);
}
return ;
}

图论 --- BFS + MST的更多相关文章

  1. 数据结构之 图论---bfs(邻接表)

    数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索( ...

  2. Borg Maze(BFS+MST)

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

  3. CF986A Fair【图论/BFS】

    [题意]: 有些公司将在Byteland举办公平的会议.Byteland的n个城镇,m条两镇之间的双向道路.当然,你可以使用道路从任一个城镇到达任何城镇. 有k种商品产自Byteland,并且每个城镇 ...

  4. 图论-BFS解无权有向图最短路径距离

    概述 本篇博客主要内容: 对广度优先搜索算法(Breadth-First-Search)进行介绍: 介绍用邻接表的存储结构实现一个图(附C++实现源代码): 介绍用BFS算法求解无权有向图(附C++实 ...

  5. 图论--BFS总结

    1.关于BFS的Key_word: ①hash或状态压缩记录状态  ②状态剪枝 ③反向BFS ④双向BFS ⑤特殊初始化VIS数组 ⑥动态图的搜索 ⑦优先队列优化搜索 ⑧数位搜索 下面是一一讲解: 1 ...

  6. 算法基础⑦搜索与图论--BFS(宽度优先搜索)

    宽度优先搜索(BFS) #include<cstdio> #include<cstring> #include<iostream> #include<algo ...

  7. hihocoder-1389&&2016北京网赛07 Sewage Treatment(二分+网络流)

    题目链接: Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people could ...

  8. CodeForces Round #285 Div.2

    C.Misha and Forest (图论 BFS) 比赛进行了一半才想起来有场CF没打,=_=|| 前两道题快速切掉,C题一直卡没什么好的思路 憋了几天,忍不住偷偷瞄了一下别人AC的代码,发现我题 ...

  9. Adjacency matrix based Graph

    Interface AddVertex(T data) AddEdge(int from, int to) DFS BFS MST TopSort PrintGraph using System; u ...

随机推荐

  1. [转]QT中的D指针与Q指针

    Qt为了使其动态库最大程度上实现二进制兼容,引入了d指针的概念. 那么为什么d指针能实现二进制兼容呢? 为了回答这个问题,首先弄清楚什么是二进制兼容? 所谓二进制兼容动态库,指的是一个在老版本库下运行 ...

  2. 各主流摄像头的rtsp地址格式

    海康威视rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream说明:username: 用户名.例 ...

  3. 3 Linux文件基本属性

    Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为保护系统的安全性,Linux系统对不同的用访问的同意文件(包括目录文件)的权限做了不同的规定eg: [root@www ...

  4. logger(一)slf4j简介及其实现原理

    一.slf4j简介 slf4j(Simple logging facade for Java)是对所有日志框架制定的一种规范.标准.接口,并不是一个框架的具体的实现,因为接口并不能独立使用,需要和具体 ...

  5. 【前端_React】npm常用命令

    安装模块(包): //全局安装 $ npm install 模块名 -g //本地安装 $ npm install 模块名 //一次性安装多个 $ npm install 模块1 模块2 模块n -- ...

  6. 让天堂的归天堂,让尘土的归尘土——谈Linux的总线、设备、驱动模型

    本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者: 宋宝华 来源: 微信公众号linux阅码场(id: linuxdev) 公元1951年5月15日的国会听证上, ...

  7. vuex相关

    作用:对项目里多个组件的共享状态进行集中式管理(读/写) .

  8. Windows下Redis安装配置和使用注意事项

    Windows下Redis安装配置和使用注意事项 一:下载 下载地址: https://github.com/microsoftarchive/redis/releases 文件介绍: 本文以3.2. ...

  9. 201871010102-《面向对象程序设计(java)》第6-7周学习总结

    博文正文开头:(2分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/ ...

  10. java.util.ConcurrentModificationException异常;java.util.ConcurrentModificationException实战

    写代码遇到这个问题,很多博客文章都是在反复的强调理论,而没有对应的实例,所以这里从实例出发,后研究理论: 一.错误产生情况 1 .字符型 (1)添加 public static void main(S ...