图论 --- 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 ...
随机推荐
- 移动端调试神器vconsole,手机端网页的调试工具Eruda
移动端调试神器vconsole,手机端网页的调试工具Eruda 移动端中使用 vConsole调试 移动端调试工具vconsole安装Git地址:https://github.com/WechatFE ...
- JDK安装—JAVA
下载JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 关于版本选择需要注意的问题: 如果eclip ...
- OEL7.6源码安装MYSQL5.7
首先官网下载安装包https://dev.mysql.com/downloads/mysql/5.7.html#downloads 然后上传解压至/usr/local目录 [root@localhos ...
- angular6 页面加载数据时的loading提示
使用npm安装ngx-loading模块 npm install --save ngx-loading 在app.module.ts中导入模块 import { BrowserModule } fro ...
- 【Spring AOP】Spring AOP之你必须知道的AOP相关概念(1)
一.什么是AOP AOP(Aspect-oriented Programming)即面向切面编程,是对OOP( Object-oriented Programming)即面向对象编程的一种补充,AOP ...
- Java字符串——String深入
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10840495.html 一:字符串的不可变性 1.可变 与 不可变 辨析 Java中的对象按照创建后,对象的 ...
- 团队项目-Alpha版本发布1
此次作业的目的是让同学们在这个星期内完成团队项目α版本的第一次测试和发布,为发布下一次的 α版本做一个准备和前期检验. 1.作业要求: 提交一份α版本冲刺博客 2.博客要求: (1)请大家在作业开头添 ...
- flask实战-个人博客-视图函数
视图函数 在上面我们创建了所有必须的模型类.模板文件和表单类.经过程序规划和设计后,我们可以创建大部分视图函数.这些视图函数暂时没有实现具体功能,仅渲染对应的模板,或是重定向到其他视图.以blog蓝本 ...
- 异常CLRDBG_NOTIFICATION_EXCEPTION_CODE( 0x04242420)
简介 CLRDBG_NOTIFICATION_EXCEPTION_CODE,值为0x0x04242420.此异常在.CLR 4.0的启动路径期间触发,是CLR4.0版本初始化调试服务时向调试器发送消息 ...
- py 包和模块,软件开发目录规范
目录 py 包和模块,软件开发目录规范 什么是包? 什么是模块? 软件开发目录规范 py 包和模块,软件开发目录规范 什么是包? 包指的是内部包__init__.py的文件夹 包的作用: 存放模块,包 ...