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 ...
随机推荐
- memcache 启动参数
启动方式: 参数 说明 -d 以守护程序(daemon)方式运行 -u root 指定用户,如果当前为 root ,需要使用此参数指定用户 -P /tmp/a.pid 保存PID到指定文件 内存设置: ...
- C#中的List<string>泛型类示例
在C#代码中使用一系列字符串(strings)并需要为其创建一个列表时,List<string>泛型类是一个用于存储一系列字 符串(strings)的极其优秀的解决办法.下面一起有一些Li ...
- 【性能诊断】三、单功能场景的性能分析(RedGate Profiler)
上一篇我们简单的对客户前端和数据库后端的性能问题进行了定位,如果排除了这两块,问题基本就确定在应用服务器上.但是我们往往对应用服务器,或者说应用程序的性能最陌生,一旦出现性能问题往往有无所适从的感觉, ...
- 日期转换工具类 CommUtil.java
package com.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.ut ...
- Oracle “CONNECT BY” 使用 [转]
Oracle “CONNECT BY”是层次查询子句,一般用于树状或者层次结果集的查询.其语法是: 1 [ START WITH condition ]2 CONNECT BY [ NOCYCLE ] ...
- Android 文件的选择
Android 文件的选择 打开文件选择器 private void showFileChooser() { Intent intent = new Intent(Intent.ACTION_GET_ ...
- MVC4 WebAPI(二)——Web API工作方式
http://www.cnblogs.com/wk1234/archive/2012/05/07/2486872.html 在上篇文章中和大家一起学习了建立基本的WebAPI应用,立刻就有人想到了一些 ...
- LintCode "Subarray Sum II"
Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...
- (转)mongodb分片
本文转载自:http://www.cnblogs.com/huangxincheng/archive/2012/03/07/2383284.html 在mongodb里面存在另一种集群,就是分片技术, ...
- C语言每日一题之No.12
文件操作知识:如何将一个文件的内容读取到另一个文件里? fread函数和fwrite函数 1.函数功能 用来读写一个数据块. 2.一般调用形式 fread(buffer,count,siz ...