Borg Maze

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的距离和的最小值,这就是传说中的最小生成树了。当然,需要求出两点之间的最短距离。

普利姆:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int INF = 0x3fffffff;

struct Node
{
	int x;
	int y;
	int d;
	Node(int a,int b,int c)
	{
		x = a;
		y = b;
		d = c;
	}
};

char map[100][100];   //字符
bool visited[100][100]; //判断访问过没有
int cnt[100][100];      //记录该坐标的编号(A和S才有编号)
int d[2555][2555];      //点之间的距离
int di[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
bool visit[2555];  //普利姆用
int d2[2555];
int res;

void bfs(int x,int y,int n,int m)
{
	memset(visited,false,sizeof(visited));
	queue<Node> q;
	q.push(Node(x,y,0));
	visited[x][y] = true;
	d[cnt[x][y]][cnt[x][y]] = 0;
	while(!q.empty())
	{
		Node p = q.front();
		q.pop();
		for(int i = 0; i < 4;i++)
		{
			int xx = p.x + di[i][0];
			int yy = p.y + di[i][1];
			if(xx >= 0 && xx < n && yy >= 0 && yy < m)
			{
				if(visited[xx][yy])
				{
					continue;
				}
				if(map[xx][yy] == '#')
				{
					continue;
				}
				if(cnt[xx][yy] != -1)
				    d[cnt[x][y]][cnt[xx][yy]] = p.d + 1;
				q.push(Node(xx,yy,p.d + 1));
				visited[xx][yy] = true;
			}
		}
	}
}

void prim(int n)
{
	memset(visit,false,sizeof(visit));
	res = 0;
	for(int i = 0;i < n;i++)
	{
		d2[i] = d[0][i];
	}
	d2[0] = 0;
	visit[0] = true;
	for(int i = 1;i < n;i++)
	{
		int min = 1000000000;
		int k;
		for(int j = 0;j < n;j++)
		{
			if(!visit[j] && min > d2[j])
			{
				min = d2[j];
				k = j;
			}
		}
		res += min;
		visit[k] = true;
		for(int j = 0;j < n;j++)
		{
			if(!visit[j] && d2[j] > d[k][j])
			{
				d2[j] = d[k][j];
			}
		}
	}
}

int main()
{
	int ncase;
	cin>>ncase;
	char s[30];
	while(ncase--)
	{
		int m,n;
		scanf("%d%d",&m,&n);
		gets(s);
		for(int i = 0;i < n;i++)
		{
			for(int j = 0;j < m;j++)
			{
			    scanf("%c",&map[i][j]);
			}
			getchar();
		}

		memset(cnt,-1,sizeof(cnt));
		int k = 0;
		for(int i = 0;i < n;i++)
		{
			for(int j = 0;j < m;j++)
			{
				if(map[i][j] == 'A' || map[i][j] == 'S')
				{
					cnt[i][j] = k++;  //编号,0开始
				}
			}
		}

		for(int i = 0;i < n;i++)
		{
			for(int j = 0;j < m;j++)
			{
				if(cnt[i][j] != -1)
				{
					bfs(i,j,n,m); //从每一点开始找到其他点的最短距离
				}
			}
		}

		prim(k);
		printf("%d\n",res);
	}

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 06:23:39

Borg Maze的相关文章

POJ 3026 Borg Maze

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7998   Accepted: 2675 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 descr

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

链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走),空格代表空位(可走),S代表搜索起点(可走) A代表外星人站(可走),现在要从S出发,将S和所有的A之间都连通,求路线总距离最小值 分析:可以先用bfs将所有的A,S两两之间的最短距离,题目的目的是将S与所有的A连通,使得总距离最小, 所有任选一点开始按最小生成树的算法做就行,并非非要从S点开始 注:题目输入x,y后可能有很多空格,可以用gets将多余的空格取走,开数组是尽量开大点,之前虽然开的比题目数据     稍大,但一

POJ Borg Maze (BFS+最小生成树)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10428   Accepted: 3463 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 desc

poj 3026 Borg Maze 最小生成树+bfs prim算法

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 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 descr

POJ3026——Borg Maze(BFS+最小生成树)

Borg Maze DescriptionThe 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

poj 3026 Borg Maze(bfs+prim)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10810   Accepted: 3574 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 desc

【POJ 3026】Borg Maze

[POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队可以无限分割 问搜索到所有alien所需要的总步数 即求一个无向图 包含所有的点并且总权值最小(最小生成树 BFS+最小生成树 Prim/Kruskal-懒死了 就这么贴吧--凑活看( ̄┰ ̄*) #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue&

Borg Maze(MST &amp; bfs)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9220   Accepted: 3087 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 descr

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

Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12014 Accepted: 3925 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

POJ 3026:Borg Maze(BFS建图+prim+MST)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8250   Accepted: 2762 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 descr