http://poj.org/problem?id=3026

Borg Maze

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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
最小生成树问题,但较不同点的是没有给原图,所以要用BFS找各个字母之间的距离,即构成原图
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<queue>
#define INF 0x3f3f3f3f
#define max(a, b)(a > b ? a : b)
#define min(a, b)(a < b ? a : b)
#define N 110 using namespace std; struct node
{
int x, y, step;
}; int G[N][N], dist[N], p[N][N];//p记录字母的坐标
bool vis[N], use[N][N];
char maps[N][N];
int m, n, num;
int d[][] = {{, }, {, }, {-, }, {, -}}; void Init()
{
int i, j;
memset(p, , sizeof(p));
memset(vis, false, sizeof(vis));
for(i = ; i <= num ; i++)
{
for(j = ; j <= num ; j++)
{
if(i == j)
G[i][j] = ;
else
G[i][j] = G[j][i] = INF;
}
}
} int prim(int s)//最小生成树,求最小花费
{
int i, j, index, Min, ans = ;
for(i = ; i <= num ; i++)
dist[i] = G[s][i];
vis[s] = true;
for(i = ; i < num ; i++)
{
Min = INF;
for(j = ; j <= num ; j++)
{
if(!vis[j] && dist[j] < Min)
{
Min = dist[j];
index = j;
}
}
ans += Min;
vis[index] = true;
for(j = ; j <= num ; j++)
{
if(!vis[j] && dist[j] > G[index][j])
dist[j] = G[index][j];
}
}
return ans;
} void BFS(int x, int y)//广搜构建原图
{
queue<node>Q;
int i, a, b;
node now, next;
memset(use, false, sizeof(use));
now.x = x;
now.y = y;
now.step = ;
use[x][y] = true;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(p[now.x][now.y] > )//该点为字母
G[p[x][y]][p[now.x][now.y]] = now.step;
for(i = ; i < ; i++)
{
a = next.x = now.x + d[i][];
b = next.y = now.y + d[i][]; if(a >= && a < m && b >= && b < n && !use[a][b] && maps[a][b] != '#')
{
next.step = now.step + ;
use[a][b] = true;
Q.push(next); }
}
}
} int main()
{
int i, j, t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d ", &n, &m);
Init();
num = ;
for(i = ; i < m ; i++)
{
for(j = ; j < n ; j++)
{
scanf("%c", &maps[i][j]);
}
getchar();
}
for(i = ; i < m ; i++)
{
for(j = ; j < n ; j++)
{
if(maps[i][j] == 'A' || maps[i][j] == 'S')
p[i][j] = ++num;//统计字母的个数,即要进入树的点的个数
}
}
for(i = ; i < m ; i++)
{
for(j = ; j < n ; j++)
if(p[i][j] > )
BFS(i, j);
}
printf("%d\n", prim());
}
return ;
}

poj 3026 Borg Maze (BFS + Prim)的更多相关文章

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

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

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

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

  3. POJ 3026 Borg Maze(Prim+bfs求各点间距离)

    题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...

  4. POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  5. POJ 3026 Borg Maze bfs+Kruskal

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

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

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

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

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

  8. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

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

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6634   Accepted: 2240 Descrip ...

随机推荐

  1. C#通过代码注册COM组件

    using System; using System.Diagnostics; using Microsoft.Win32; namespace ChuckLu.Utility { public cl ...

  2. RAPIDXML 中文手册,根据官方文档完整翻译!

    简介:这个号称是最快的DOM模型XML分析器,在使用它之前我都是用TinyXML的,因为它小巧和容易上手,但真正在项目中使用时才发现如果分析一个比较大的XML时TinyXML还是表现一般,所以我们决定 ...

  3. UVa 11076 (有重元素的排列) Add Again

    n个可重复的元素的排列一共有 = All种,其中 假设这些数依次为ai,每种数字有mi个. 从右往左考虑第d位数(d≥0),第i个数字出现的次数为,那么这个数字对所求答案的贡献为 其实可以先一次求出个 ...

  4. UVa 10341 (二分求根) Solve It

    很水的一道题,因为你发现这个函数是单调递减的,所以二分法求出函数的根即可. #include <cstdio> #include <cmath> //using namespa ...

  5. Android之 环境搭建

    一. 使用ADT Bundle多合一下载包 下载地址:链接:http://pan.baidu.com/s/1gepNRjX  密码: ozdi 说      明:多合一下载包,里面包含了:sdk + ...

  6. VS2010安装中遇到的错误

    背景 用win7 64位系统安装VS2010遇到一个错误,网上查了各种资料也没有找到这种解决办法,后来自己找到了解决办法,分享一下,让他人少走一些弯路. 错误信息 安装过程中遇到如下错误: [08/2 ...

  7. 在fmri研究中,cca的应用历史

    1.02年ola是第一个应用cca在fmri激活检测上的学者. <exploratory fmri analysis by autocorrelation maximization> 2. ...

  8. Android进度加载的Loading效果

    网上看到的一个开源项目的loading效果,效果很赞,记录一下: 开源项目地址如下:https://github.com/RomainPiel/Titanic

  9. 【转】linux : waitpid函数

    原文网址:http://blog.csdn.net/jifengszf/article/details/3067841 [waitpid系统调用]       功能描述: 等待进程改变其状态.所有下面 ...

  10. java classpath、path用法

    java环境配置classpath和path变量的作用: path:指定cmd中命令执行文件所在的路径.比如javac.java两个可执行文件在jdk的bin目录下,如果path值含有这个bin目录, ...