点击打开链接

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7097   Accepted: 2389

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

题目大意就是从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 最小生成树 + 广搜的更多相关文章

  1. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

  2. poj 3026 Borg Maze (最小生成树+bfs)

    有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...

  3. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  4. POJ 3026 Borg Maze【BFS+最小生成树】

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  5. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

  6. POJ 3026 Borg Maze(bfs+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6634   Accepted: 2240 Descrip ...

  7. POJ 3026 Borg Maze (最小生成树)

    Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...

  8. poj 3026 Borg Maze (bfs + 最小生成树)

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

  9. POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16625   Accepted: 5383 Descri ...

随机推荐

  1. 命名空间"system.web"中不存在类型或命名空间名称security"

    在webservice中添加了一个md5加密报错: "命名空间"system.web"中不存在类型或命名空间名称security" 在引用中添加System.W ...

  2. How to send Email using C#

    try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail. ...

  3. 【Android代码片段之八】监听Android屏幕是否锁屏

    实现方法:1)通过BroadcastReceiver接收广播Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF可以判断屏幕状态是否锁屏,但是只有屏幕状态发 ...

  4. linux设置系统日期时间

    设置当前日期: date -s 08/06/2015 设置当前时间:date -s 10:03:00 写入BIOS: clock -w 显示当前日期时间: date

  5. SELECT INTO 和 INSERT INTO SELECT 两种表复制语句.txt

    Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我 ...

  6. 通用的业务编码规则设计实现[转:http://www.cnblogs.com/xqin/p/3708367.html]

    一.背景 每一个企业应用中不可避免的都会涉及到业务编码规则的问题,比如订单管理系统中的订单编号,比如商品管理系统中的商品编码,比如项目管理系统中的项目编码等等,这一系列的编码都需要管理起来,那么它们的 ...

  7. 读书笔记:应用随机过程:概率模型导论:Aloha协议问题

    例4.16,Aloha协议:就本书例题所涉及的部分来说,几乎等同于CSMA.这个例题重写如下: 考察一个包含多个设备的通信系统,其中在每个时间段发送信息的设备个数是独立同分布的.......每个设备将 ...

  8. Makefile编译选项CC与CXX/CPPFLAGS、CFLAGS与CXXFLAGS/LDFLAGS

    转自:http://www.firekyrin.com/archives/597.html 编译选项 让我们先看看 Makefile 规则中的编译命令通常是怎么写的. 大多数软件包遵守如下约定俗成的规 ...

  9. 理解Socket编程【转载】

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  10. PLSQL_Oracle临时表Temporary Table基本概念和用法(概念)

    2014-06-08 Created By BaoXinjian