POJ 3026 Borg Maze

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6126 Accepted: 2056

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

解题思路:首先题意是有一个50*50的迷宫,这个迷宫里有一点是搜索起始点,从这一点出发去寻找藏在迷宫里的外来入侵者,已知搜索人员必多于入侵者且当搜索队员组队前进时,他们的步伐算一格,这样的话,问最少需要走多少格可以搜索出全部入侵者,其实他们走动的距离就是两点间的最短距离,求最短距离之和就是求最小生成树,所以用BFS+Prim,因为将图中包括S和A的两种点都看做构图时的顶点,所以BFS是用来计算构图时两顶点间的边权的,再然后就是Prim了。这一题的坑爹之处是输入时在cin>>y>>x后会加有一些空格,所以必须用gets(temp)来消除这些空格,= =!
#include<iostream>
#include<queue>
#define INF 3000
using namespace std;
int dis[102][102],graph[102][102],vex[55][55],lowcost[102],x,y,n;
char maze[55][55];
bool visit[55][55],v[102];
int X[4]={-1,1,0,0},Y[4]={0,0,-1,1};//上,下,左,右
void bfs(int m,int n)
{
	int k;
	//两个队列分别维护行和列
	queue<int> row;
	queue<int> col;
	memset(dis,0,sizeof(dis));//dis记录该顶点到其他位置的距离
	memset(visit,false,sizeof(visit));
	row.push(m);col.push(n);
	visit[m][n]=true;
	while(!row.empty()&&!col.empty())
	{
		int i=row.front();int j=col.front();
		row.pop();col.pop();
		if(vex[i][j])//若搜索到的位置是顶点,将两点间的距离加入到图中的边权上
			graph[vex[m][n]][vex[i][j]]=dis[i][j];
		for(k=0;k<4;k++)//对上下左右四个位置进行搜索
		{
			int p=i+X[k];int q=j+Y[k];
			if(p>=0&&p<x&&q>=0&&q<y)
				if(!visit[p][q]&&maze[p][q]!='#')
				{
					visit[p][q]=true;
					row.push(p);col.push(q);
					dis[p][q]=dis[i][j]+1;//记录距离
				}
		}
	}
}
int Prim()
{
	int i,j,k,temp,flag,ans=0;
	memset(v,false,sizeof(v));
	v[1]=true;
	for(i=1;i<=n;i++)
		lowcost[i]=graph[1][i];
	for(i=1;i<n;i++)
	{
		temp=INF;
		for(j=2;j<=n;j++)
			if(!v[j]&&temp>lowcost[j])
			{
				temp=lowcost[j];
				flag=j;
			}
			v[flag]=true;
		for(k=1;k<=n;k++)
			if(!v[k]&&lowcost[k]>graph[flag][k])
				lowcost[k]=graph[flag][k];
		ans+=temp;
	}
	return ans;
}
int main()
{
	int i,j,test;
	cin>>test;
	while(test--)
	{
		n=0;
		memset(vex,0,sizeof(vex));//记录顶点编号
		cin>>y>>x;//先输入列再输入行
		char temp[55];
		gets(temp);//消除空格的影响
		for(i=0;i<x;i++)
		{
			cin.getline(maze[i],y+1);
			for(j=0;j<y;j++)
				if(maze[i][j]=='A'||maze[i][j]=='S')
					vex[i][j]=++n;//记录顶点编号
		}
		for(i=0;i<x;i++)
			for(j=0;j<y;j++)
				if(vex[i][j])//对图中顶点进行BFS并记录两点间的边权
				   bfs(i,j);
		cout<<Prim()<<endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值