POJ 3026--Borg Maze (BFS+Prim)

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5258   Accepted: 1756

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

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001


S与A都当成图的顶点,用广搜来求出各边的距离,再用prim算法


dfsdfsd


#include<iostream>
using namespace std;

const int MAX_NODE = 105;
const int MAX_ROW = 55;
const int INF = 100000000;

int row,col,n;
int node[MAX_NODE];			//用来记录S和A结点,以这种形式:r*col+c(col是列数)
char map[MAX_ROW][MAX_ROW];	//record the map
int graph[MAX_NODE][MAX_NODE];	//the graph to build by bfs



void BFS(int start,int d[][MAX_ROW])	//d[i][j]表示从起点到i行,j列的最短距离
{
	int r,c,i,j,rr,cc;					//(r,c)指当前点,(rr,cc)指下一点
	int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};	//上,下,左,右
	int head = 0,rear = 0,que[MAX_ROW*MAX_ROW];		//BFS用到的队列,当是' ','S','A',入队
	bool vis[MAX_ROW][MAX_ROW];						//记录访问的点
	for(i=0;i<row;i++)								//初始化
		for(j=0;j<col;j++)
		{
			d[i][j] = INF;
			vis[i][j] = false;
		}
	que[rear++] = start;							//起点入队
	r = start / col;	c = start % col;
	vis[r][c] = true;
	d[r][c] = 0;
	while(head != rear)								//判断队是否为空
	{
		int t = que[head++];
		r = t / col;
		c = t % col;
		for(i=0;i<4;i++)
		{
			rr = r + dir[i][0];
			cc = c + dir[i][1];
			if(!vis[rr][cc] && map[rr][cc] != '#')	//判断可扩展结点
			{
				d[rr][cc] = d[r][c] + 1;
				que[rear++] = rr*col + cc;
			}
			vis[rr][cc] = true;
		}
	} 
}

void BuildGraph()
{
	int i,j;
	int d[MAX_ROW][MAX_ROW];
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			graph[i][j] = INF;
	for(i=0;i<n;i++)
	{
		BFS(node[i],d);
		for(j=0;j<n;j++)
		{
			if(i == j)
				graph[i][j] = INF;
			else
				graph[i][j] = d[node[j]/col][node[j]%col];
		}
	}
}

int Prim()
{
	int i,j,k,res = 0;
	int dis[MAX_NODE];
	bool vis[MAX_NODE];
	k = 0;
	for(i=0;i<n;i++)
	{
		vis[i] = false;	
		dis[i] = graph[k][i];
	}
	dis[k]=0;
	vis[k] = true;
	res = 0;
	for(j=1;j<n;j++)
	{
		int min = INF;
		for(i=0;i<n;i++)
			if(!vis[i] && min > dis[i])
				min = dis[k = i];
		res += min;
		vis[k] = true;
		for(i=0;i<n;i++)
			if(!vis[i] && dis[i] > graph[k][i])
				dis[i] = graph[k][i];
	}
	return res;
}

int main()
{
	int T;
	int i,j;
	cin >> T;
	while(T--)
	{
		cin >> col >> row;
		n = 0;
		for(i=0;i<row;i++)
		{
			while(getchar() != '\n');    //测试数据的问题,害我WA了好多次,输入行列后面还有很多空格,无语……
			for(j=0;j<col;j++)
			{
				map[i][j] = getchar();
				if(map[i][j] == 'A' || map[i][j] == 'S')
					node[n++] = i*col + j;
			}
		}

		BuildGraph();
		cout << Prim() <<endl;
	}
	system("pause");
	return 0;
}


你可能感兴趣的:(POJ 3026--Borg Maze (BFS+Prim))