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. poj 2155 Matrix (树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16797   Accepted: 6312 Descripti ...

  2. 2017北大校赛 J题 pairs

    题目链接 http://poj.openjudge.cn/practice/C17J/ orz 原来是一道无脑枚举题目 只是很卡常数而已 复杂度算错也是很醉orz 当时怎么没想着优化常数呢 题解:枚举 ...

  3. Android中代码设置RadioButton的高端技巧

    不知道怎么起标题,就这样了. 目前主要讲两个方面内容: 代码方式 设置RadioButton的 android:button . android:background 等属性为 @null : 代码方 ...

  4. eclipse 主题文件配置

    eclipse市场搜索 Eclipse Color Theme ----用于控制文本域主题 Eclipse 4 Chrome Theme  chrome风格的主题 最新的:Jeeeyul's Them ...

  5. 记录一次Nginx跳转报错的问题

    错误信息如下: An error occurred. Sorry, the page you are looking for is currently unavailable. Please try ...

  6. 知问前端——自动补全UI

    自动补全(autocomplete),是一个可以减少用户输入完整信息的UI工具.一般在输入邮箱.搜索关键字等,然后提取出相应完整字符串供用户选择. 调用autocomplete()方法 var hos ...

  7. 【STSRM13】木之本樱

    [题意]抽象模型后转化为:给定n个直线,ans+=C(x,4)*8,x为每个经过直线数>=4的点的直线数,不存在平行直线. [算法]数学 [题解] 运用了一个很简单的道理:经过同一个点的线段互相 ...

  8. DotNETCore 学习笔记 日志

    Logging --------------------------------------------------------------------------------------- Impl ...

  9. 线程局部存储 TLS

    C/C++运行库提供了TLS(线程局部存储),在多线程还未产生时,可以将数据与正在执行的线程关联.strtok()函数就是一个很好的例子.与它一起的还有strtok_s(),_tcstok_s()等等 ...

  10. Javascript传参参考

    可参考的细节: <!doctype html> <html lang="en"> <head> <meta charset="U ...