POJ 3026:Borg Maze:BFS+prim

Borg Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8086   Accepted: 2703

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时刻,都可以分叉出多个继续搜索,求解最少的路径和。很明显,使用BFS搜索迷宫,找出每两个节点之间需要的距离,就进入无向图完全图的最小生成树求解方式,非常清晰的思路。

但是,对于不太会STL的人来讲,手写BFS需要手写队列,那么队列的大小定义是个问题,当然也可以使用循环队列。这就为该题目的坑爹做了很好的掩护。题目的坑爹之处在于:在输入用例中,x,y后面有不同数量的空格!直接使得下面的读取图数据混乱。由于途中也有空格表示可以行走的地方,因此,读取数据的时候不可以使用scanf(%s),如果你使用scanf(%c)就会发现:图的后面也有空格,导致整个图输入混乱!必须使用gets(),然后在x,y后面也要使用gets()来读取坑爹的空格。

就是因为没有空格,使得一直RTE,我还以为是因为我的队列设置不够,一直增大队列,还使用循环队列。不知道出题人的数据怎么给的,那么大的坑,要是不去讨论区,打死看不出来问题~
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 100000000
int n,tu[105][105],a[105][105],x,y,num;
int usetu[105][105],use[105],d[105];
char ts[205];
typedef struct node
{
	int tx,ty;
	int ceng;
}node;
node duilie[1000];
typedef struct nnode
{
	int ttx,tty;
}nnode;
nnode zuobiao[205];
int head,end;
void init()
{
	int i,j;
	int k=2;
	scanf("%d%d",&x,&y);
	gets(ts);
	for(i=0;i<y;i++)
	{
		gets(ts);
		for(j=0;j<x;j++)
		{
			switch(ts[j])
			{
			case '#':tu[i][j]=-1;break;
			case ' ':tu[i][j]=0;break;
			case 'S':tu[i][j]=1;
				zuobiao[1].ttx=i;
				zuobiao[1].tty=j;break;
			case 'A':tu[i][j]=k;
				zuobiao[k].ttx=i;
				zuobiao[k].tty=j;
				k++;break;
			default:break;
			};
		}
	}
	num=k-1;
	for(i=1;i<=num;i++)
		for(j=1;j<=num;j++)
			a[i][j]=maxn;
	memset(a,0,sizeof(a));
}
void bfs(int hao,int beginx,int beginy)
{
	int i,j;
	int tceng,temx,temy;
	int temhao;
	duilie[0].ceng=0;
	duilie[0].tx=beginx;
	duilie[0].ty=beginy;
	head=0;
	end=1;
	usetu[beginx][beginy]=1;
	memset(usetu,0,sizeof(usetu));
	while(end!=head)
	{
		tceng=duilie[head].ceng;
		temx=duilie[head].tx;
		temy=duilie[head].ty;
		head=(head+1)%1000;
		if(tu[temx][temy]>hao)
		{
			temhao=tu[temx][temy];
			a[hao][temhao]=a[temhao][hao]=tceng;
		}
		if(tu[temx-1][temy]!=-1&&!usetu[temx-1][temy])
		{
			duilie[end].ceng=tceng+1;
			duilie[end].tx=temx-1;
			duilie[end].ty=temy;
			end=(end+1)%1000;
			usetu[temx-1][temy]=1;
		}
		if(tu[temx+1][temy]!=-1&&!usetu[temx+1][temy])
		{
			duilie[end].ceng=tceng+1;
			duilie[end].tx=temx+1;
			duilie[end].ty=temy;
			end=(end+1)%1000;
			usetu[temx+1][temy]=1;
		}
		if(tu[temx][temy-1]!=-1&&!usetu[temx][temy-1])
		{
			duilie[end].ceng=tceng+1;
			duilie[end].tx=temx;
			duilie[end].ty=temy-1;
			end=(end+1)%1000;
			usetu[temx][temy-1]=1;
		}
		if(tu[temx][temy+1]!=-1&&!usetu[temx][temy+1])
		{
			duilie[end].ceng=tceng+1;
			duilie[end].tx=temx;
			duilie[end].ty=temy+1;
			end=(end+1)%1000;
			usetu[temx][temy+1]=1;
		}
	}
}
void prim()
{
	int i,j;
	int minc,mind;
	memset(use,0,sizeof(use));
	memset(d,0,sizeof(d));
	use[1]=1;
	d[1]=0;
	for(i=2;i<=num;i++)
		d[i]=a[1][i];
	for(j=0;j<num-1;j++)
	{
		minc=maxn;
		for(i=1;i<=num;i++)
		{
			if(!use[i]&&minc>d[i])
			{
				minc=d[i];
				mind=i;
			}
		}
		if(minc!=maxn)
		{
			use[mind]=1;
			for(i=1;i<=num;i++)
			{
				if(!use[i]&&d[i]>a[mind][i])
					d[i]=a[mind][i];
			}
		}
	}
}
void print()
{
	int i,j;
	int sum=0;
	for(i=1;i<=num;i++)
		sum+=d[i];
	printf("%d\n",sum);
}
int main()
{
	int i,j;
	while(scanf("%d",&n)!=EOF&&n)
	{
		while(n--)
		{
			init();
			for(i=1;i<=num;i++)
				bfs(i,zuobiao[i].ttx,zuobiao[i].tty);
            prim();
			print();
		}
	}
	return 0;
}


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