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. 2019-6-23-win10-uwp-未给任务-GenerateAppxPackageRecipe-的必需参数-AppxManifestXml-赋值

    title author date CreateTime categories win10 uwp 未给任务 GenerateAppxPackageRecipe 的必需参数 AppxManifestX ...

  2. Leetcode179. Largest Number最大数

    给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说明: 输出结果 ...

  3. 跟我一起做一个vue的小项目(五)

    接下来我们要做的是热门推荐页面,我们写一个推荐组件 使用的方法也是前端data中的数据渲染到页面上面,这里对文字过长取省略号的方法不成功使用了一个小技巧 使用了min-width:0 我们来看完整的代 ...

  4. Jdbc封装和对CURD的封装

    1.查询emp表中的所有记录为例 2.测试类 public Emp getByNameAndEmail(String name, String email){ String sql = "s ...

  5. TZ_05_Spring_annotation常见注解

    Spring常用的注解大全和解释 注解 解释 @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类 ...

  6. IO流4 --- IO流体系 --- 技术搬运工(尚硅谷)

  7. centos下nginx无法加载php文件,404

    前提:html文件可以正常加载,php安装正常,nginx配置正确.仍然无法加载php文件,明明文件是存在,却报404,而不是直接输出文件 原因是:未启动php-fpm,未开启9000端口 首先查看是 ...

  8. web前端学习(二)html学习笔记部分(5)--拖放元素、canvas画布使用

    1.2.11  拖放 1.2.11.1  html拖放 1.2.11.2  html拖放本次资源 showOjb(一个对象)展示一下一个对象的信息. 1.2.12  html画布(canvas)  标 ...

  9. DFA算法实现敏感词过滤

    DFA算法:即确定有穷自动机,简单点说就是,它是是通过event和当前的state得到下一个state,即event+state=nextstate.理解为系统中有多个节点,通过传递进入的event, ...

  10. Python中的进程池与线程池(包含代码)

    Python中的进程池与线程池 引入进程池与线程池 使用ProcessPoolExecutor进程池,使用ThreadPoolExecutor 使用shutdown 使用submit同步调用 使用su ...