Borg Maze

Time Limit: 1000MS Memory Limit: 65536K

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

题意:给你一个地图,地图中A代表外星人,S代表出发点,从S点出发,每遇到一个A点便可分裂成多个。求把所有A都吃完需要多少步。

题解:这是一个DFS+最短路的问题,用DFS遍历求出每个点到其他点的最短距离,然后用prim算法。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std; const int INF = 1e9+7; char s[55][55];
int f[55][55],m,n,num,mp[105][105]; struct node
{
int x,y;
int step;
}p[105]; int Next[4][2] = {-1,0,1,0,0,-1,0,1}; int judge(int x,int y)
{
if(x>=0&&y>=0&&x<n&&y<m)
return 1;
return 0;
} void bfs(int x)
{
node t1,t2;
int ff[55][55],dx,dy,i,num1,num2;
memset(ff,0,sizeof(ff));
queue<node> q;
t1 = p[x];
num1 = f[t1.x][t1.y];
ff[t1.x][t1.y] = 1;
t1.step = 0;
q.push(t1);
while(!q.empty())
{
t1 = q.front();
//printf("%d %d\n",t1.x,t1.y);
q.pop();
for(i=0;i<4;i++)
{
dx = t1.x + Next[i][0];
dy = t1.y + Next[i][1];
if(judge(dx,dy)&&s[dx][dy]!='#'&&!ff[dx][dy])
{
t2.x = dx;
t2.y = dy;
ff[dx][dy] = 1;
t2.step = t1.step + 1;
q.push(t2);
if(f[t2.x][t2.y]!=-1)
{ num2 = f[t2.x][t2.y];
//printf("%d\n",t2.step);
//printf("%d %d\n",num1,num2);
if(mp[num1][num2]>t2.step)
mp[num1][num2] = t2.step;
}
}
}
}
} void prim()
{
int i,j,k,MIN,sum = 0;
int f[105],dis[105];
for(i=0;i<num;i++)
{
dis[i] = mp[0][i];
f[i] = 0;
}
f[0] = 1;
for(i=0;i<num;i++)
{
MIN = INF;
k = -1;
for(j=0;j<num;j++)
{
if(!f[j]&&dis[j]<MIN)
{
MIN = dis[j];
k = j;
}
}
//printf("%d\n",k);
if(MIN == INF)
break;
f[k] = 1;
sum += MIN;
for(j=0;j<num;j++)
{
if(!f[j]&&dis[j]>mp[k][j])
dis[j] = mp[k][j];
}
}
printf("%d\n",sum);
} int main()
{
int t,i,j;
char a[105];
scanf("%d",&t);
while(t--)
{
memset(f,-1,sizeof(f));
num = 0;
for(i=0;i<=100;i++)
for(j=0;j<=100;j++)
mp[i][j] = (i==j)?0:INF;
scanf("%d%d",&m,&n);
gets(a);/*不知道为什么,getchar会WA*/
for(i=0;i<n;i++)
{
gets(s[i]);
for(j=0;j<m;j++)
{
if(s[i][j]=='A'||s[i][j]=='S')
{
p[num].x = i;
p[num].y = j;
f[i][j] = num++;
}
}
}
for(i=0;i<num;i++)
bfs(i);
// for(i=0;i<num;i++)
// {
// for(int j=0;j<num;j++)
// printf("%d ",mp[i][j]);
// printf("\n");
// }
prim();
}
return 0;
}

POJ-3026_Borg Maze的更多相关文章

  1. poj 3026Borg Maze

    http://poj.org/problem?id=3026 #include<cstdio> #include<iostream> #include<cstring&g ...

  2. POJ 2157 Maze

    Maze Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3183   Accepted: 996 Description A ...

  3. {POJ}{3897}{Maze Stretching}{二分答案+BFS}

    题意:给定迷宫,可以更改高度比,问如何使最短路等于输入数据. 思路:由于是单调的,可以用二分答案,然后BFS验证.这里用优先队列,每次压入也要进行检查(dis大小)防止数据过多,A*也可以.好久不写图 ...

  4. poj 3897 Maze Stretching 二分+A*搜索

    题意,给你一个l,和一个地图,让你从起点走到终点,使得路程刚好等于l. 你可以选择一个系数,把纵向的地图拉伸或收缩,比如你选择系数0.5,也就是说现在上下走一步消耗0.5的距离,如果选择系数3,也就是 ...

  5. 【bfs】 poj 3984 maze 队列存储

    #include <iostream> #include <stdio.h> #include <cstring> #define Max 0x7f7f7f7f u ...

  6. 搜索 || BFS || POJ 2157 Maze

    走迷宫拿宝藏,拿到所有对应的钥匙才能开门 *解法:从起点bfs,遇到门时先放入队列中,取出的时候看钥匙够不够决定开不开门,如果不够就把它再放回队列继续往下走,当队列里只有几个门循环的时候就可以退出,所 ...

  7. BFS广搜题目(转载)

    BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...

  8. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  9. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

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

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

随机推荐

  1. hbase Hfile处理原因

    Hile 内部的数据是按key排序好的,但Hfile之间数据并不能保证key的排序,也就是说对于新生成的Hfile,其内部的key并不都比老的Hfile的大,因此每次检索时,都需要在所有的Hfile中 ...

  2. SPOJ 2916 GSS5 - Can you answer these queries V

    传送门 解题思路 和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2, ...

  3. Vijos 学姐的逛街计划

    传送门 题解传送门 线性规划,最小费用最大流. 神奇的操作. //Achen #include<algorithm> #include<iostream> #include&l ...

  4. ECMAScript 6 (浅显入门)

    1.let:ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. var命令会发生”变量提升“现象,即变量可以在声明之前使用,值为unde ...

  5. JavaScript模板引擎实例应用(转)

    本文将举实例向大家讲解几个常用模板引擎的简单使用. 演示地址:模板引擎示例http://demo.52fhy.com/jstemp/ 准备工作 演示数据:blog.json结构: { "li ...

  6. 【Scala学习笔记】一、函数式编程的思想

    1. 函数是头等值.     在函数编程中,函数也是值,与整数和字符串处于同一地位.函数可以像变量一样被创建,修改,并当成变量一样传递,返回或是在函数中嵌套函数. 函数可以当做参数传递给其他函数.   ...

  7. django中的聚合索引

    Django(元信息)元类建索引 ORM查询(sql优化)优化 自定义聚合函数 Django的元类建索引————索引:索引的一个主要目的就是加快检索表中数据,索引是经过某种算法优化过的,因而查找次数要 ...

  8. 二零一七年工作中常用的基本Linux命令记录(Ubuntu)

    Linux命令如下(Ubuntu): 1. apt-get install openssh-server 下载远程工具 2. apt-get install lrzsz 上传下载工具 3. apt-g ...

  9. Leetcode74. Search a 2D Matrix搜索二维矩阵

    编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...

  10. 转:解决Onethink上传视频的问题 超棒的解决方案

    用过Onethink的都知道,它是不能上传视频的. 有人想到用上传附件的方式上传视频,但是结果……就是提示没有上传文件. 要是正常上传个一两兆的图片啊,压缩文件什么的还是可以的. 所以,重点来了 怎么 ...