图论 --- BFS + MST
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7844 | Accepted: 2623 |
Description
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
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
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的更多相关文章
- 数据结构之 图论---bfs(邻接表)
数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索( ...
- Borg Maze(BFS+MST)
Borg Maze http://poj.org/problem?id=3026 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- CF986A Fair【图论/BFS】
[题意]: 有些公司将在Byteland举办公平的会议.Byteland的n个城镇,m条两镇之间的双向道路.当然,你可以使用道路从任一个城镇到达任何城镇. 有k种商品产自Byteland,并且每个城镇 ...
- 图论-BFS解无权有向图最短路径距离
概述 本篇博客主要内容: 对广度优先搜索算法(Breadth-First-Search)进行介绍: 介绍用邻接表的存储结构实现一个图(附C++实现源代码): 介绍用BFS算法求解无权有向图(附C++实 ...
- 图论--BFS总结
1.关于BFS的Key_word: ①hash或状态压缩记录状态 ②状态剪枝 ③反向BFS ④双向BFS ⑤特殊初始化VIS数组 ⑥动态图的搜索 ⑦优先队列优化搜索 ⑧数位搜索 下面是一一讲解: 1 ...
- 算法基础⑦搜索与图论--BFS(宽度优先搜索)
宽度优先搜索(BFS) #include<cstdio> #include<cstring> #include<iostream> #include<algo ...
- hihocoder-1389&&2016北京网赛07 Sewage Treatment(二分+网络流)
题目链接: Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people could ...
- CodeForces Round #285 Div.2
C.Misha and Forest (图论 BFS) 比赛进行了一半才想起来有场CF没打,=_=|| 前两道题快速切掉,C题一直卡没什么好的思路 憋了几天,忍不住偷偷瞄了一下别人AC的代码,发现我题 ...
- Adjacency matrix based Graph
Interface AddVertex(T data) AddEdge(int from, int to) DFS BFS MST TopSort PrintGraph using System; u ...
随机推荐
- css层叠规则(层叠样式表)
CSS层叠规则: 1.找出所有相关的规则,这些规则都包含与一个给定元素匹配的选择器. 2.按权重(!important)和来源对应用到给定元素的所有声明进行排序. 3.按特殊性对应用到给定元素的所有声 ...
- Docker 的操作命令记录
docker ps:列出正在运行的 container docker ps -a:列出所有的 container docker rm [containerid]:移除 container(可并列多个, ...
- CTF-代码审计(3)..实验吧——你真的会PHP吗
连接:http://ctf5.shiyanbar.com/web/PHP/index.php 根据题目应该就是代码审计得题,进去就是 日常工具扫一下,御剑和dirsearch.py 无果 抓包,发现返 ...
- Github标星过万,Python新手100天学习计划。
大数据文摘编辑部出品 作为目前最火也是最实用的编程语言,Python不仅是新手入门程序界的首选,也逐渐成为了从大厂到小厂,招牌需求list的必要一条. 当然,学Python这件事情,你可能也和文摘菌一 ...
- Oracle分析函数FIRST_VALUE、LAST_VALUE
FIRST_VALUE.LAST_VALUE分析函数可以按照特定分组和排序取出组内首尾值,语法 FIRST_VALUE { (expr) [ {RESPECT | IGNORE} NULLS ] | ...
- Word 2016中公式不能自动斜体的解决方法
参考资料: 中文版 Office 数学公式默认不是斜体 为什么Word 2007中的公式编辑器字体不能自动倾斜 归纳总结 这个问题自Word 2007开始就存在,直至我目前用的Word 2016都没有 ...
- python 数据库小测试
1.整理博客 2.详细解释下列mysql执行语句的每个参数与参数值的含义 mysql -hlocalhost -P3306 -uroot -proot # mysql (连接数据库) # hloc ...
- 201871010106-丁宣元 《面向对象程序设计(java)》第二周学习总结
丁宣元 <面向对象程序设计(java)>第二周学习总结 正文开头 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在 ...
- sublime ctrl b突然不能用解决方法
Sublime Text 2 ?ctrl+b 如果出现运行为空白,按ctrl+`来显示错误,如下所示,转载了一篇解决方案 ? 文章参考:http://eric.themoritzfamily.com/ ...
- CentOS7编译安装Keepalived2.0.19
实验环境:centos7 节点1:10.15.192.21 节点2:10.15.192.22 vip地址:10.15.192.23 1.下载文件 cd /usr/local/src wget http ...