图论 --- BFS + MST

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[110];
int row,line;
int dist[60];
char Map[60][60];
bool vis[60][60],vist[60]; //邻接矩阵存储
int mark[60][60],dis[110][110]; //给每个字母一个编号
int dir[4][2]={-1,0, 0,1, 1,0, 0,-1};

void bfs(int index)
{
queue<Node> que;
memset(vis, false, sizeof(vis));
Node now, next;
node[index].step = 0;
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 = 0; i < 4; ++i)
{
int tx = x + dir[i][0], ty = y +dir[i][1];
if(vis[tx][ty] == false && Map[tx][ty] != ‘#‘) //如果这一步可以走
{
next.x = tx;
next.y = ty;
vis[tx][ty] = true;
next.step = now.step + 1;
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 = 0; i < cnt; ++i)
{
dist[i] = INF;
vist[i] = false;
}
dist[0] = 0;
while(1)
{
int min = INF, now = -1;
for(int i = 0; i < cnt; ++i)
{
if(min > dist[i] && vist[i] == false)
{
min = dist[i];
now = i;
}
}
if(now == -1)
return ans;
ans += min;
vist[now] = true;
for(int i = 0; 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 = 0;
memset(mark, 0, sizeof(mark));
scanf("%d%d", &row, &line);
char ch;
while(ch = getchar(), ch != ‘\n‘);
for(int i = 0; i < line; ++i)
{
for(int j = 0; 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 = 0; i < cnt; ++i)
bfs(i);
prim();
printf("%d\n", ans);
}
return 0;
}

图论 --- BFS + MST

时间: 2024-10-09 23:19:48

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

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

数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索(BFS)遍历,输出从某个顶点出发的遍历序列.(同一个结点的同层邻接点,节点编号小的优先遍历) 输入 输入第一行为整数n(0< n <100),表示数据的组数. 对于每组数据,第一行是三个整数k,m,t(0<k<100,0<m<(k-1)*k/2,0< t<k),

Pku oj 3026 Borg Maze(BFS+MST)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12028   Accepted: 3930 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 desc

图论 BFS总结

1.关于BFS的Key_word: ①hash或状态压缩记录状态  ②状态剪枝 ③反向BFS ④双向BFS ⑤特殊初始化VIS数组 ⑥动态图的搜索 ⑦优先队列优化搜索 ⑧数位搜索 下面是一一讲解: 1.hash或状态压缩记录状态 : 当状态太多而且边界也广时数组难以存储状态时或者题目对空间的要求较为苛刻,这时候就要使用状态压缩来保存所需的状态或者hash的方式将一个状态对应为一个整数通过一维数组来记录是否访问,当数据过于离散时可以考虑使用map,但是相应的时间复杂度也会上升,如果真的要将所有状态

【图论】MST及拓展

MST的三种解法 1>prim 2>kruskal 算法步骤步骤一:T是边的集合,其初始状态为空:步骤二:从原图剩余边中选取一条最小代价的边:步骤三:看其是否与当前T中其它边构成环路:步骤四:如果未构成环路,则加入T中:否则,丢弃该边:步骤五:是否还有剩余边,如果有则返回步骤二,否则,程序结束. 算法证明(来自网络) (1)Kruskal算法一定能得到一个生成树:(2)该生成树具有最小代价. =>(1)假设该算法得到的不是生成树,根据树的定义,就有两种情形,第一是得到的图是有环的,第二就

[图论][BFS]Fennec VS. Snuke

题目描述 Fennec and Snuke are playing a board game.On the board, there are N cells numbered 1 through N, and N−1 roads, each connecting two cells. Cell ai is adjacent to Cell bi through the i-th road. Every cell can be reached from every other cell by re

BFS学习总结

BFS学习总结 给你一个n*m的网格迷宫,迷宫中有些格子不能走,其他的格子都能走.然后给你起点与终点,问你从起点走到终点最少需要多少步? 上面的问题就是一个典型的BFS问题,对于这类问题来说,只要你掌握了这类问题的关键思想,其实他们都是可以用类似的思路来做的. 你可以把BFS问题想象成:从一个父亲(起点状态)生儿子(后继状态),儿子又生孙子(后继状态)的过程,只要这个家族中出生了一个满意的后代(终点状态),这个家族就不生了. 但是如果这个家族中有两个完全一样的人出生(他们的辈分不一定相同),那么

ACM入门 训练方法

ppt:http://pan.baidu.com/s/1eQBzFqE 入门知识汇总: 经典DP: LIS LCS, 状态压缩DP 区间DP 图论:MST , 最短路三种算法(dijkstra , bellman ford, floyd ),最大流, 双连通分量(点双连通,边双连通,强连通) 数学:质因数分解,筛素数,数论的常用结论 数据结构: 线段树,树状数组,字典树,kmp,哈希,平衡树(treap 或者 splay) 搜索:普通dfs  bfs , 双向广搜, 常用的剪枝策略. 算法的中英

BDFZOI 树的直径

提交次数:2 涉及知识:基础图论/BFS 描述 一棵树T的"直径"定义为结点两两间距离的最大值.给定带权树T,求T的直径长度. 输入 第一行包含2个整数N.M,表示图中共有N个结点和M条无向边.(N <= 5000,M<n)接下来M行,每行包含3个整数{u,v,w},表示有一条无向边连接结点u.v*输入保证是无环图输出一个整数,代表直径长度 样例输入 4 31 2 12 3 22 4 3 样例输出 5 代码: 1 #include<iostream> 2 #in

CodeForces Round #285 Div.2

C.Misha and Forest (图论 BFS) 比赛进行了一半才想起来有场CF没打,=_=|| 前两道题快速切掉,C题一直卡没什么好的思路 憋了几天,忍不住偷偷瞄了一下别人AC的代码,发现我题没看清题目,题中说了给出的图是森林. 于是切入点找到了! 题意: 一个由n个节点构成的森林,编号从0到n-1,给出每个节点的 度数 和 相邻节点编号的异或和,输出图中的边数和所有邻接节点的编号. 分析: 因为是森林,所以图中的叶子节点就是突破口.叶子节点最明显的特征就是: 度数为1 它相邻节点编号的