您的位置:首页 > 其它

poj 3026 Borg Maze 最小生成树 + 广搜

2013-08-23 12:11 316 查看
点击打开链接

Borg Maze

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 7097Accepted: 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: