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. [Codeforces667A]Pouring Rain(数学,几何)

    题目链接:http://codeforces.com/contest/667/problem/A 题意:一个杯子里有水,一个人在喝并且同时在往里倒.问这个人能不能喝完,多久能喝完. 把相关变量都量化成 ...

  2. Catalan数推导(转载)

    Raney引理: 设整数序列A = {Ai, i=1, 2, …, N},且部分和Sk=A1+…+Ak,序列中所有的数字的和SN=1,在A的N个循环表示中,有且仅有一个序列B,满足B的任意部分和Si均 ...

  3. 类handler

    /** The handler class is the interface for dynamically loadable storage engines. Do not add ifdefs a ...

  4. 函数os_file_pread

    /*******************************************************************//** Does a synchronous read ope ...

  5. HDU 1686 (KMP模式串出现的次数) Oulipo

    题意: 求模式串W在母串T中出现的次数,各个匹配串中允许有重叠的部分. 分析: 一开始想不清楚当一次匹配完成时该怎么办,我还SB地让i回溯到某个位置上去. 后来仔细想想,完全不用,直接让模式串向前滑动 ...

  6. Asp.Net Unix时间戳和DateTime类型转换

    using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...

  7. windows2003 IIS6网络负载平衡设置

    问题 随着计算机技术的不断发展,单台计算机的性能和可靠性越来越高.但现实中还是有许多应用是单台计算机难以达到,例如: 1.银行存储用户数据的数据库服务器必须保证24小时不间断的运转,并在发生严重硬件故 ...

  8. mysql 索引与优化like查询

    索引与优化like查询 1. like %keyword    索引失效,使用全表扫描.但可以通过翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全表扫描. 2. like key ...

  9. git - 搭建git仓库

    1. 更新git版本: http://codelife.me/blog/2013/06/25/upgrade-git-on-centos-6-4/ 2. 建立git仓库: git init --bar ...

  10. Python - re - 正则表达式 - 怎么用

    <python cookbook> - 1.18 - 一次完成多个替换 这个blog介绍正则,写得不错,而且,一如既往的‘长’. 1. re.escape(string) THIS,说明函 ...