POJ3026 Borg Maze

题目来源:http://poj.org/problem?id=3026


Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14890 Accepted: 4825

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

本以为是dp,方程都写出来了,然而运行样例都有错。。。。。。

可以这样看这道题:所有A和S均可看作节点,任意节点间都存在一条最短路径,可以把这条最短路径当作节点间的权值。

到达某个A的只可能是从起点走来的一波或是从另一个A处走来的一波。

要使所有点均被访问到且总路径最短,这就转化成最小生成树问题。。。。。。

本题还有坑点:在每组数据的第一行,在n和m后可能仍有空格,需要过滤除去。


另外提供一组POJ Discuss上的数据,对WA掉的或是TLE的或许有帮助。

/*
1
50 50     
##################################################
#AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#S                                              A#
##################################################
输出141
*/



代码:


#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn=51;
int n,m;
int a[1001][1001];
int sx,sy;
int cnt;
int x[1002],y[1002];
int g[1002][1002];
int dis[1002][1002];
int t=0;
int dx[]={0,0,-1,1};
bool vis[1002];
int dy[]={-1,1,0,0};
int to[2002];
queue<int> q;
void bfs(int px,int py)
{
	dis[px][py]=0;
	q.push(px);q.push(py);
	while(!q.empty())
	{
		int u=q.front();q.pop();
		int v=q.front();q.pop();
		for(int i=0;i<4;i++)
		{
			if(u+dx[i]>=1&&u+dx[i]<=n&&v+dy[i]>=1&&v+dy[i]<=m&&a[u+dx[i]][v+dy[i]]!=1&&dis[u+dx[i]][v+dy[i]]>dis[u][v]+1)
			{
				q.push(u+dx[i]);
				q.push(v+dy[i]);
				dis[u+dx[i]][v+dy[i]]=dis[u][v]+1;
			}
		}
	}
}
int main()
{
//	freopen("std.out","w",stdout);
	int _;scanf("%d",&_);
	while(_--)
	{
		memset(a,0,sizeof(a));
		memset(g,63,sizeof(g));
		scanf("%d%d\n",&m,&n);
		t=0;getchar();
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				char c=getchar();
				if(c==' ')a[i][j]=0;
				if(c=='#')a[i][j]=1;
				if(c=='S')
				{
					a[i][j]=0;
					sx=i;sy=j;
				}
				if(c=='A')a[i][j]=2,t++;
			}
			if(i!=n)getchar();
		}
		/*for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			cout<<a[i][j]<<" ";
			cout<<endl;
		}*/
		cnt=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(a[i][j]==2)
				{
					x[++cnt]=i;
					y[cnt]=j;
				}
			}
		}
		x[++cnt]=sx;
		y[cnt]=sy;
		for(int i=1;i<=cnt;i++)
		{
			memset(dis,63,sizeof(dis));
			bfs(x[i],y[i]);
			for(int j=1;j<=cnt;j++)g[i][j]=dis[x[j]][y[j]];
			/*for(int ii=1;ii<=n;ii++)
			{
				for(int jj=1;jj<=m;jj++){
					if(dis[ii][jj]>1e9)cout<<"* ";
					else cout<<dis[ii][jj]<<" ";
				}
				cout<<endl;
			}
			cout<<endl;*/
		}
/*		for(int i=1;i<=cnt;i++)
		{
			for(int j=1;j<=cnt;j++)cout<<g[i][j]<<" ";
			cout<<endl;
		}*/
		memset(vis,0,sizeof(vis));
		memset(to,63,sizeof(to));
		int tot=0;
		to[1]=0;
	//	cout<<cnt<<"******"<<endl;
		for(int i=1;i<=cnt;i++)
		{
			int s=1e9;
			int pos=0;
			for(int j=1;j<=cnt;j++)
			{
				if(to[j]<s&&vis[j]==0)
				{
					pos=j;
					s=to[j];
				}
			}
			if(pos==0)break;
			vis[pos]=1;
			tot+=to[pos];
			for(int j=1;j<=cnt;j++)
			{
				if(vis[j]==0&&to[j]>g[pos][j])
				{
					to[j]=g[pos][j];
				}
			}
		}
		printf("%d\n",tot);
	}
	return 0;
}
/*
2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  
*/


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值