图论 --- 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 ...
随机推荐
- js正则只能包含小写数字分割符,切不能以分割符开头和结尾
const version = /^(?!_)(?!.*-$)[a-z0-9_]+$/; 1.一个正则表达式,只含有数字.小写字母.中划线不能以中划线开头和结尾: ^(?!-)(?!.*-$)[a-z ...
- 使用Jenkins+Docker+Gitlab+Maven搭建持续集成环境
继使用Docker搭建Gitlab后 大致的步骤如下: 开发人员通过IDE工具(IntelliJ IDEA)将代码推送到gitlab. jenkins从gitlab中获取到源码,并使用maven编译. ...
- Linux用户环境配置文件
用户操作环境配置文件: 从/etc/skel目录复制过来 .bashrc 打开新终端 /etc/bashrc .bash_profile 用户登 ...
- 03、磁盘管理+swap分区创建+磁盘配额+自动挂载
磁盘管理 分区标识 一般用4位标识,前两位,磁盘类型,第3位,磁盘编号,第4位,分区编号 如: /dev/sda1 sd 磁盘类型 a 磁盘编号 1 分区编号 [root@s1 ...
- 201871010110 - 李华 《面向对象程序设计(java)》第二周学习总结
第一部分:理论知识学习部分 一.简单的Java程序应运程序 1.标识符0标识符由字母.下划线.美元符号和数字组成,且第一个符号不能为数字. 标识符可用作:类名.对象名.变量名.方法名.数组名.文件 ...
- linux 以导入文件形式添加定时任务(crontab)时需要注意的坑
在实际操作过程中发现,使用导入文件形式添加定时任务时,会将用户已有的定时任务全部覆盖清理(先清空,再重新导入),所以在使用文件导入定时任务时,需要先将已有定时任务导出,然后将新任务进行追加到已有定时任 ...
- 洛谷 P2661 信息传递(NOIP 提高 2015)
传送门 本题本来是一个很好的并查集的题(似乎靠的就是并查集),然而蒟蒻我刚刚学习了 tarjan 所以就用 terjan做了一下 大概题意就是:一个人要和另外的一个人告诉他所知道的信息,然后问什么时候 ...
- haproxy 配置文件详解 之 global
配置示例: global log 127.0.0.1 local0 info maxconn user nobody group nobody daemon nbproc pidfile /usr/l ...
- iptables 自定义链
当默认链中的规则非常多时,不方便我们管理. 想象一下,如果INPUT链中存放了200条规则,这200条规则有针对httpd服务的,有针对sshd服务的,有针对私网IP的,有针对公网IP的,假如,我们突 ...
- 【caffe编译】 fatal error: hdf5.h: 没有那个文件或目录
src/caffe/layers/hdf5_output_layer.cpp:3:18: fatal error: hdf5.h: 没有那个文件或目录 查找文件 locate hdf5.h 修改Mak ...