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. TZOJ 5963 Increasing Sequences(线性DP)

    描述 Given a string of digits, insert commas to create a sequence of strictly increasing numbers so as ...

  2. TZOJ 5986 玄武密码(AC自动机)

    描述 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此. 很多年后,人们终于在 ...

  3. php工作中常用到的linux命令

    压缩并指定目录举例:zip -r /home/kms/kms.zip /home/kms/server/kms 解压并指定目录举例:unzip /home/kms/kms.zip -d /home/k ...

  4. android 数据绑定(3)自动更新UI

    1.官方文档 https://developer.android.com/topic/libraries/data-binding/observability 2.observable 属性 适合对象 ...

  5. Django项目:CRM(客户关系管理系统)--28--20PerfectCRM实现King_admin数据修改美化

    {#table_change.html#} {## ————————19PerfectCRM实现King_admin数据修改————————#} {#{% extends "king_mas ...

  6. for循环遍历json(附习题及答案)

    三种方法 var mapColumn = { "vdoing" : "访问深度", "_visitorNumber": "访问量& ...

  7. Web前端开发的就业前景怎么样,薪资待遇如何

    信息技术的迅速发展,使IT技术者们赶上了一个百年难遇的好机会,尤其是国家出台了“互联网+”的政策后,更是催生了IT行业的就业空间,使其呈现爆发性增长. 如今,微信逐渐成为了大家主要的交流工具,随着各种 ...

  8. JS---元素隐藏的不同方式

    元素隐藏的不同方式 dispaly, visibility, opacity, height&border 为0 <!DOCTYPE html> <html lang=&qu ...

  9. mysql查询 包含某个字符的记录

    从excel导入数据库的时候,发现poi自动把电话号码转换为科学计数法了 所以要把带e的筛选出来 SELECT * FROM t_customer WHERE phone like '%E%'; 然后 ...

  10. IO流9 --- 使用FileInputStream和FileOutputStream读写非文本文件 --- 技术搬运工(尚硅谷)

    字节流读写非文本文件(图片.视频等) @Test public void test5(){ File srcFile = new File("FLAMING MOUNTAIN.JPG&quo ...