码迷,mamicode.com
首页 > 其他好文 > 详细

Borg Maze

时间:2015-08-19 23:46:09      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

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;
}


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

Borg Maze

标签:

原文地址:http://blog.csdn.net/wang2534499/article/details/47790149

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!