Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16625   Accepted: 5383

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

Source

 

题意:给你一个图里面有S和A,让你求把S和A全部连在一起的最少消耗,明着最小生成树,但是每一个S和A之间的距离用bfs求出就行。

主要是题目有个坑,在n,和m后可能会有空行;

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int INF=0x3f3f3f3f;
char mapp[][];
int a[][];
int n,m;
int dx[]={,-,,};
int dy[]={,,,-};
struct node{
int x,y;
};
int flag[][];
bool vis[];
int minn[];
int cost[][];///记录总的各点到各点的距离
int cnt[][];///记录当前点到bfs到其他各点的距离
void bfs(int sx,int sy){ queue<node>q;
while(!q.empty())q.pop();
memset(cnt,-,sizeof(cnt));
cnt[sx][sy]=;
node first;
first.x=sx,first.y=sy;
q.push(first);
while(!q.empty()){
node now=q.front();
q.pop();
if(a[now.x][now.y]!=-)///记录到全局为生成树做准备
cost[a[sx][sy]][a[now.x][now.y]]=cnt[now.x][now.y];
for(int i=;i<;i++){
node next;
next.x=now.x+dx[i];
next.y=now.y+dy[i];
if(mapp[next.x][next.y]=='#'||cnt[next.x][next.y]!=-||next.x<||next.y<||next.x>=n||next.y>=m)continue;
cnt[next.x][next.y]=cnt[now.x][now.y]+;
q.push(next);
}
}
}
int prim(int n){
int ans=;
memset(vis,false,sizeof(vis));
vis[]=true;
for(int i=;i<n;i++)minn[i]=cost[][i];
for(int i=;i<n;i++){
int minc=INF;
int p=-;
for(int j=;j<n;j++){
if(!vis[j]&&minc>minn[j]){
minc=minn[j];
p=j;
}
}
ans+=minc;
vis[p]=true;
for(int j=;j<n;j++)
if(!vis[j]&&minn[j]>cost[p][j]){
minn[j]=cost[p][j];
}
}return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
cin>>t;
while(t--){
cin>>m>>n;
gets(mapp[]);
memset(a,-,sizeof(a));
int num=;
for(int i=;i<n;i++){
gets(mapp[i]);
for(int j=;j<m;j++){
if(mapp[i][j]=='A'||mapp[i][j]=='S')
a[i][j]=num++;
}
}
/* for(int i=0;i<n;i++){
cout<<mapp[i]<<endl;
}*/
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]!=-){
bfs(i,j);
}
}
}
/* for(int i=0;i<num;i++){
for(int j=0;j<num;j++){
cout<<cost[i][j]<<" ";
}
cout<<endl;
}*/
cout<<prim(num)<<endl;
}
return ;
}
 

POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)的更多相关文章

  1. poj 3026 Borg Maze (bfs + 最小生成树)

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

  2. POJ - 3026 Borg Maze bfs+最小生成树。

    http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...

  3. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  4. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

  5. POJ 3026 Borg Maze (最小生成树)

    Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...

  6. poj 3026 Borg Maze(最小生成树+bfs)

    题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...

  7. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  8. POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

  9. POJ - 3026 Borg Maze(最小生成树)

    https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...

随机推荐

  1. 【题解】Bzoj2560串珠子

    挺强的……容斥+状压DP.首先想到如果可以求出f[k],f[k]代表联通状态为k的情况下的合法方案数,则f[k] = g[k] - 非法方案数.g[k]为总的方案数,这是容易求得的.那么非法方案数我们 ...

  2. [LOJ#2553][CTSC2018]暴力写挂

    [LOJ#2553][CTSC2018]暴力写挂 试题描述 temporaryDO 是一个很菜的 OIer .在 4 月,他在省队选拔赛的考场上见到了<林克卡特树>一题,其中 \(k = ...

  3. wmic的用法

    原始文章链接:http://blog.sina.com.cn/s/blog_5fb265c70100w4d0.html 一.wmic的基本命令格式简析 经常看网上的相关资料的话,读者可能会对wmic有 ...

  4. [Leetcode] word ladder 单词阶梯

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  5. POJ2155 Matrix 【二维线段树】

    题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...

  6. AOJ.800 热身之开关灯

    热身之开关灯 Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 276 Submis ...

  7. Swing中使用UIManager批量自定义单一JComponent组件默认属性

    最近在研究Swing,被它的复杂性气的快吐血了,刚才本打算把JFrame的背景色换成白底,结果发现事情没想象中那么顺利,调用setBackground完全没有效果,猛然醒悟到JPanel本身是带不透明 ...

  8. [MySQL] explain执行计划解读

    Explain语法 EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可得 ...

  9. HDU 多校对抗赛 J Time Zone

    Time Zone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. Spring学习-- AOP入门动态代理

    AOP 的拦截功能是由 java 中的动态代理来实现的.说白了,就是在目标类的基础上增加切面逻辑,生成增强的目标类(该切面逻辑或者在目标类函数执行之前,或者目标类函数执行之后,或者在目标类函数抛出异常 ...