POJ 3026 Borg Maze 圖論 prim算法(最小生成樹)+BFS算法(廣度優先搜索)

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7151   Accepted: 2405

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

這道題一看見,就想到BFS了,但是,主頁君搜索實在木有怎麼看過,就立刻翻書學習BFS和DFS,這道題具體思路就是對於這張迷宮,從任意一個出發點或者外星人窩藏的點出發,用DFS搜索圖表,值就等於上一個節點的值+1,然後得到圖中的值,這個具體方法可以百度,是BFS算法最基礎的一部分,然後繪製出圖以後,將圖帶入prim算法求出最小生成樹的最小權值,輸出這個權值即可AC。題目有絲進階,主要利用了多種算法。

下面是AC代碼:

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
#define max_vertexes 105
int point[55][55],G[105][105];
int dist[55][55];
int m,n;
void bfs(int a,int b)
{
	int x,y,flag[55][55];
	queue<int> quex;
	queue<int> quey;
	quex.push(a);
	quey.push(b);
	memset(dist,0,sizeof(dist));
	memset(flag,0,sizeof(flag));
	flag[a][b]=1;
    while(quex.empty()==0)
	{
		x=quex.front();
		y=quey.front();
		flag[x][y]=1;
		quex.pop();
		quey.pop();
		if(point[x][y]>0)
		{
			G[point[a][b]][point[x][y]]=dist[x][y];
			G[point[x][y]][point[a][b]]=dist[x][y];
		}
		if(x-1>=0&&point[x-1][y]>=0&&flag[x-1][y]==0)
		{
			dist[x-1][y]=dist[x][y]+1;
			flag[x-1][y]=1;
			quex.push(x-1);
			quey.push(y);
		}
		if(x+1<n&&point[x+1][y]>=0&&flag[x+1][y]==0)
		{
			dist[x+1][y]=dist[x][y]+1;
			flag[x+1][y]=1;
			quex.push(x+1);
			quey.push(y);
		}
		if(y-1>=0&&point[x][y-1]>=0&&flag[x][y-1]==0)
		{
			dist[x][y-1]=dist[x][y]+1;
			flag[x][y-1]=1;
			quex.push(x);
			quey.push(y-1);
		}
		if(y+1<m&&point[x][y+1]>=0&&flag[x][y+1]==0)
		{
			dist[x][y+1]=dist[x][y]+1;
			flag[x][y+1]=1;
			quex.push(x);
			quey.push(y+1);
		}
	}
	return;
}
int prim(int vcount)
{
    int i,j,k,sum=0;
    int lowcost[max_vertexes],closeset[max_vertexes],used[max_vertexes];
    for (i=1;i<=vcount;i++)
        {
        lowcost[i]=G[1][i];
        closeset[i]=1; 
        used[i]=0;
        }
    used[1]=1; 
    for (i=1;i<vcount;i++)
        {
        j=1;
        while (used[j]) j++;
        for (k=1;k<=vcount;k++)
            if ((!used[k])&&(lowcost[k]<lowcost[j])) j=k;
		sum+=G[j][closeset[j]];
        used[j]=1;
        for (k=1;k<=vcount;k++)
            if (!used[k]&&(G[j][k]<lowcost[k]))
                { lowcost[k]=G[j][k];
                closeset[k]=j; }
        }
	return sum;
}
int main()
{
	int t,i,j,num;
	char ch[55][55];
	cin>>t;
	while(t--)
	{
		scanf("%d%d\n",&m,&n);
		for(i=0;i<n;i++)
			gets(ch[i]);
		memset(point,0,sizeof(point));
		memset(G,0,sizeof(G));
		num=1;
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				if(ch[i][j]=='S'||ch[i][j]=='A')
				{
					point[i][j]=num;
					num++;
				}
				else if(ch[i][j]=='#')
					point[i][j]=-1;
			}
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				if(point[i][j]>0)
					bfs(i,j);
		printf("%d\n",prim(num-1));
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章