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. Linux改IP后务必重启网络服务

    [root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0BOOTPROTO=noneONBOOT=yes ...

  2. linux系统设置服务开机启动3种方法,Linux开机启动程序详解

    linux系统设置服务开机启动 方法1:.利用ntsysv伪图形进行设置,利用root登陆 终端命令下输入ntsysv 回车:如下图     方法2:利用命令行chkconfig命令进行设置 简要说明 ...

  3. org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml

    org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml at org.hibernate ...

  4. 使用截图方式将Excel导出为PNG图片的不可行性

    博主前面一篇文章使用了JAVA的Robot机制 模拟打开Excel然后Robot移动到指定区域,截图并生成PNG格式图片 试图使用这种方式将复杂的Excel报表转化成无差别的PNG图片 但是这种方式遇 ...

  5. ASP.NET MVC ActionResult的实现

    1.11种ActionResult 在System.Web.Mvc命名空间下: ActionResult是一个抽象类, 在Action中返回的都是其派生类.下面是我整理的ASP.NET MVC 1.0 ...

  6. UVA 11478 Halum(用bellman-ford解差分约束)

    对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...

  7. UVA 1659 Help Little Laura 帮助小劳拉 (最小费用流,最小循环流)

    (同时也是HDU 2982,UVA的数据多) 题意:平面上有m条有向线段连接了n个点.你从某个点出发顺着有向线段行走,给走过的每条线段涂一种不同的颜色,最后回到起点.你可以多次行走,给多个回路涂色(要 ...

  8. Oracle 跟踪事件 set event

    一.Oracle跟踪文件 Oracle跟踪文件分为三种类型,一种是后台报警日志文件,记录数据库在启动.关闭和运行期间后台进程的活动情况,如表空间创建.回滚段创建.某些alter命令.日志切换.错误消息 ...

  9. chrome console js多行输入

    一直以来,Chrome控制台都缺少象IE调试台那样的多行执行模式.  今天意外发现Chrome其实也支持多行模式.默认在Chrome控制台上输入回车后会执行该命令,只需要通过输入Shift+Enter ...

  10. 通过userAgent判断手机浏览器类型

    我们可以通过userAgent来判断,比如检测某些关键字,例如:AppleWebKit*****Mobile或AppleWebKit,需要注意的是有些浏览器的userAgent中并不包含AppleWe ...