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 ...
随机推荐
- 分布式服务框架:Zookeeper
Zookeeper是一个高性能,分布式的,开源分布式应用协调服务.它提供了简单原始的功能,分布式应用可以基于它实现更高级的服务,比如同步,配置管理,集群管理,名空间.它被设计为易于编程,使用文件系统目 ...
- DataGridView 添加行号
private void dataGridViewX1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //Dat ...
- 03 在Linux下安装Myeclipse及Tomcat(含下载)
测试环境: 主机系统:Win 7 虚拟机:VMware workstation 11.1.0 虚拟机OS: centos 6.5 64位 Kernel 2.6.32-431-e16.x86_64 My ...
- 读书笔记:应用随机过程:概率模型导论:Aloha协议问题
例4.16,Aloha协议:就本书例题所涉及的部分来说,几乎等同于CSMA.这个例题重写如下: 考察一个包含多个设备的通信系统,其中在每个时间段发送信息的设备个数是独立同分布的.......每个设备将 ...
- 25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- c++ 迭代器库
来自http://zh.cppreference.com/w/cpp/iterator 迭代器库提供了5种迭代器的定义,同时还提供了迭代器特征.适配器及其相关的工具函数. 迭代器共有5种:InputI ...
- 黄聪:WordPress 的 Hook 机制与原理(add_action、add_filter)
稍有接触过 WordPress 主题或插件制作修改的朋友,对 WordPress 的Hook机制应该不陌生,但通常刚接触WordPress Hook 的新手,对其运作原理可能会有点混乱或模糊.本文针对 ...
- system函数
system两层含义: 1.正确退出后.还需要再判断,操作成功或者操作失败. 2.错误退出. #include <stdio.h> #include <stdlib.h> #i ...
- C# STUDY
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- CF 500 C. New Year Book Reading 贪心 简单题
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n b ...