POJ-3026_Borg Maze
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的更多相关文章
- poj 3026Borg Maze
http://poj.org/problem?id=3026 #include<cstdio> #include<iostream> #include<cstring&g ...
- POJ 2157 Maze
Maze Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3183 Accepted: 996 Description A ...
- {POJ}{3897}{Maze Stretching}{二分答案+BFS}
题意:给定迷宫,可以更改高度比,问如何使最短路等于输入数据. 思路:由于是单调的,可以用二分答案,然后BFS验证.这里用优先队列,每次压入也要进行检查(dis大小)防止数据过多,A*也可以.好久不写图 ...
- poj 3897 Maze Stretching 二分+A*搜索
题意,给你一个l,和一个地图,让你从起点走到终点,使得路程刚好等于l. 你可以选择一个系数,把纵向的地图拉伸或收缩,比如你选择系数0.5,也就是说现在上下走一步消耗0.5的距离,如果选择系数3,也就是 ...
- 【bfs】 poj 3984 maze 队列存储
#include <iostream> #include <stdio.h> #include <cstring> #define Max 0x7f7f7f7f u ...
- 搜索 || BFS || POJ 2157 Maze
走迷宫拿宝藏,拿到所有对应的钥匙才能开门 *解法:从起点bfs,遇到门时先放入队列中,取出的时候看钥匙够不够决定开不开门,如果不够就把它再放回队列继续往下走,当队列里只有几个门循环的时候就可以退出,所 ...
- BFS广搜题目(转载)
BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
随机推荐
- Hadoop Serialization -- hadoop序列化详解 (2)【Text,BytesWritable,NullWritable】
回顾: 回顾序列化,其实原书的结构很清晰,我截图给出书中的章节结构: 序列化最主要的,最底层的是实现writable接口,wiritable规定读和写的游戏规则 (void write(DataOut ...
- AT2164 Rabbit Exercise
传送门 解题思路 首先考虑k=1的情况,对于每一个a[i],它可能会到a[i-1]*2-a[i] 与 a[i+1]*2-a[i]两个位置,概率都为%50,那么它的期望位置为 (a[i-1]*2-a[i ...
- 用localStorage在页面间传值
注意:要在同一域名下的页面才有效 在需要存储数据页面用localStorage设置数据 localStorage.setItem(key,value);//key要用单引号或者双引号包括着,value ...
- CSS3的transform 转换
transform是可以实现元素位移,旋转,缩放和变形.只介绍了2D转换~ translate 位移:改变元素位置 最多设置两个值,一个水平,一个垂直.如果设置为负数,则代表反方向.可设置百分比.eg ...
- HTML5属性
HTML5同时增加和废除了很多属性.下面介绍一些常用的属性. 1.表单属性 为input(type=text).select.textarea与button元素新增了autofocus属性.(它以指定 ...
- jS生成二叉树,二叉树的遍历,查找以及插入
js递归,二叉树的操作 //递归算法n次幂 function foo(n) { if (n == 1) { return 1; } else { return n * foo(n - 1); } } ...
- 创建动态MSSQL数据库表的方法
代码如下: ImportsSystem.Data ImportsSystem.Data.SqlClient PublicClassForm1 InheritsSystem.windows.Forms. ...
- windows下maven的安装配置
什么是maven Maven是基于POM(工程对象模型),通过一小段描述来对项目的代码.报告.文件进管理的工具. Maven是一个跨平台的项目管理工具,它是使用java开发的,它要依赖于jdk1.6及 ...
- Django与HTML业务基本结合--基本的用户名密码提交方法2
from django.shortcuts import render # Create your views here. from django.shortcuts import render de ...
- 全球城市群Megalopolis
Megalopolis From Wikipedia, the free encyclopedia (Redirected from Megalopolis (city type)) &quo ...