poj 3026 Borg Maze 最小生成树 + 广搜
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7097 | Accepted: 2389 |
Description
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
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
Sample Input
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8
11
题目大意就是从s点开始出发,在地图中搜索A,找到了以后就可以分裂成若干个小组继续搜索其他的A,分裂只能在S和A点处发生,#是墙不能走,仔细分析会发现其实所有路径组合起来就是一个图,题目所求的就是搜索到所有A的最短路径其实就是最小生成树
使用prim算法每次添加一个点,就广搜一次找每个点的最短距离
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char map[51][51];
int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int dis[51][51];
typedef struct NODE
{
int x, y, step;
}Node;
void bfs(int x, int y, int top)
{
queue<Node> q;
bool flag[51][51] = {0};
Node node, new_node; node.x = x;
node.y = y;
node.step = 0;
q.push(node);
int count = 0;
int i;
// int min = 0x7fffffff;
int mark_x = 0, mark_y = 0;
while(!q.empty())
{
node = q.front();
q.pop();
for(i = 0; i < 4; i++)
{
if(map[node.x + step[i][0]][node.y + step[i][1]] != '#' && flag[node.x + step[i][0]][node.y + step[i][1]] == 0)
{
new_node.x = node.x + step[i][0];
new_node.y = node.y + step[i][1];
new_node.step = node.step + 1;
q.push(new_node);
flag[new_node.x][new_node.y] = 1;
if(map[new_node.x][new_node.y] == 'A')
{
dis[new_node.x][new_node.y] = new_node.step > dis[new_node.x][new_node.y] ? dis[new_node.x][new_node.y] : new_node.step;
count++;
if(count == top)
return ;
}
}
}
}
}
int prim(int x, int y, int max_x, int max_y)
{
int i, j;
int count = 0;
int min_len = 0x7fffffff;
int ans = 0;
bfs(x, y, 3000);
for(i = 1; i <= max_x; i++)
{
for(j = 1; j <= max_y; j++)
{
if(dis[i][j] < 0x7fffffff)
{
count ++;
}
}
}
int min = 0x7fffffff;
int k;
int mark_x = 0, mark_y = 0;
for(i = count; i > 0; i--)
{
for(j = 1; j <= max_x; j++)
{
for(k = 1; k <= max_y; k++)
{
if(dis[j][k] < min_len )
{
min_len = dis[j][k];
mark_x = j;
mark_y = k;
}
}
}
map[mark_x][mark_y] = '#';
ans += min_len;
dis[mark_x][mark_y] = 0x7fffffff;
bfs(mark_x, mark_y, i);
min_len = 0x7fffffff;
}
return ans;
}
int main()
{
// freopen("test.txt", "r", stdin);
int n;
scanf("%d", &n);
while(' ' == getchar());
while(n--)
{
int x, y;
char ch;
memset(map, '#', sizeof(map));
scanf("%d %d", &x, &y);
while((ch =getchar()) == ' ');
int i, j; int total = 1;
int s_x, s_y;
for(i = 1; i <= y; i++)
for(j = 1; j <= x; j++)
dis[i][j] = 0x7fffffff;
for(i = 1; i <= y; i++)
{
for(j = 1; j <= x; j++)
{
ch = getchar();
if(ch == 'S')
{
s_x = i;
s_y = j;
}
map[i][j] = ch;
}
while((ch =getchar()) == ' ');
map[i][j] = 0;
}
printf("%d\n", prim(s_x, s_y, y, x));
}
return 0;
}
poj 3026 Borg Maze 最小生成树 + 广搜的更多相关文章
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- poj 3026 Borg Maze (最小生成树+bfs)
有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- POJ 3026 Borg Maze (最小生成树)
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16625 Accepted: 5383 Descri ...
随机推荐
- python escape sequences
转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \' 单引号 \" 双引号 \a 响铃 \b 退格(Backspace) \e 转义 \000 空 \n 换行 \v 纵向制表符 \ ...
- web前端面试2
百度另外一部门面试 1.前端html5新特性 2.数组常用函数 3.前后台json树形结构遍历,对算法的了解 4.css3背景属性设置为适合匹配模块的属性是什么 5.jquery插件的写法 6.研究过 ...
- TKinter布局之pack
pack布局非常简单,不用做过多的设置,直接使用一个 pack 函数就可以了. 1.我们使用 pack 函数的时候,默认先使用的放到上面,然 后 依次向下排,它会给我们的组件一个自认为合适的位置 和大 ...
- ORACLE 表函数实现
1.创建表对象类型. 在Oracle中想要返回表对象,必须自定义一个表类型,如下所示: create or replace type t_table is table of number; 上面的类型 ...
- phpredis中文手册——《redis中文手册》 php版--引用他人
出处: http://www.cnblogs.com/zcy_soft/archive/2012/09/21/2697006.html 目录(使用CTRL+F快速查找命令): Key String H ...
- Android monkey介绍
Android monkey介绍 原文地址 1 简略 monkey是android下自动化测试比较重要的的一个工具,该工具可以运行在host端或者设备(模拟器或真实设备).它会向系统发送随机事件流(即 ...
- JavaScript判断字符串能否转化为数字
判断一个字符串能否转化为数字 我们常常使用parseInt函数. 不过该函数的适用范围是很小的. 一般说来对于 如下类似 var myStr = "123hello"; 使用par ...
- PEM文件格式详细解析
PEM文件格式存档 Author:Roson sun sunxiao@tomonline-inc.com Time:2006-4-11 1. 描述: Openssl使用PEM(RFC 1421-14 ...
- JQUERY 拖拽 draggable droppable resizable selectable sortable
今天用了jq ui的拖动碰撞功能,好不容易看到有详细的API解说,记录如下: <script language="JavaScript" type="text/ ...
- IntelliJ IDEA自动去掉行尾空格
Settings→Editor→General 先选中Allow placement of caret after end of line 再修改Strip trailing spaces on Sa ...