zoukankan      html  css  js  c++  java
  • [POJ3026] Borg Maze

    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

    题解

    先将输入转为(mp[][]):'S'、'A'、' '、'#'用具体的数值来代替,并将出现的节点('S'和‘A’)存放在一个(pair)类型的数组,接着分别以前节点数-1的节点为原点,(BFS)它到各点的距离,存放于一个优先队列,最后就是求最小生成树,我用的是(Kruskal),但是对于这题,用(Prim)求最小生成树可能会更快……

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    
    typedef pair<int,int> pr;
    const int NorM=51,NM=2501;
    const int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
    int n,m,num,mp[NorM][NorM],dis[NorM][NorM];
    int f[NM];
    pr Node[NM];
    queue<pr> Q;
    struct point
    {
    	int a,b,c;
    	bool operator<(point x) const
    	{
    		return c>x.c;
    	}
    };
    priority_queue<point> Que;
    
    bool ok(int x,int y)
    {
    	if(x<1||x>n||y<1||y>m) return 0;
    	if(!mp[x][y]) return 0;
    	if(dis[x][y]!=-1) return 0;
    	return 1;
    }
    
    void Bfs(pr s)
    {
    	memset(dis,-1,sizeof(dis));
    	dis[s.first][s.second]=0;
    	Q.push(s);
    	pr nw,nt;
    	while(!Q.empty())
    	{
    		nw=Q.front(); Q.pop();
    		for(int i=0;i<4;++i)
    		{
    			nt=make_pair(nw.first+dx[i],nw.second+dy[i]);
    			if(ok(nt.first,nt.second))
    			{
    				dis[nt.first][nt.second]=dis[nw.first][nw.second]+1;
    				Q.push(nt);
    			}
    		}
    	}
    }
    
    int find(int x){return f[x]=(f[x]==x?x:find(f[x]));}
    void together(int x,int y)
    {
    	int fx=find(x),fy=find(y);
    	f[fx]=fy;
    }
    
    int main()
    {
    	int T,Ans; string s; point nw;
    	bool flag;
    	for(scanf("%d",&T);T;--T)
    	{
    		scanf("%d%d",&m,&n);
    		getline(cin,s);
    		num=1;
    		for(int i=1;i<=n;++i)
    		{
    			getline(cin,s);
    			for(int j=1;j<=m;++j)
    			{
    				if(s[j-1]=='#') mp[i][j]=0;
    				else if(s[j-1]==' ') mp[i][j]=1;
    				else if(s[j-1]=='S') Node[1]=make_pair(i,j),mp[i][j]=2;
    				else ++num,Node[num]=make_pair(i,j),mp[i][j]=2;
    			}
    		}
    		while(!Que.empty()) Que.pop();
    		for(int i=1;i<=num;++i) f[i]=i;
    		Ans=0;
    		//kruskal初始化
    		for(int i=1;i<num;++i)
    		{
    			Bfs(Node[i]);
    			for(int j=i+1;j<=num;++j)
    				if(dis[Node[j].first][Node[j].second]!=-1)
    					Que.push((point){i,j,dis[Node[j].first][Node[j].second]});//建边
    		}
    		for(int i=2;i<=num;++i)
    			for(flag=1;flag;)
    			{
    				nw=Que.top(); Que.pop();
    				if(find(nw.a)!=find(nw.b))
    				{
    					together(nw.a,nw.b);
    					Ans+=nw.c,flag=0;
    				}
    			}
    		//kruskal
    		printf("%d
    ",Ans);
    	}
    	return 0;
    }
    

    本文作者:OItby @ https://www.cnblogs.com/hihocoder/

    未经允许,请勿转载。

  • 相关阅读:
    Laravel框架之Session操作
    Laravel框架之Response操作
    Laravel之简单的学生信息管理平台
    Laravel中的模板引擎Blade
    Laravel中的查询构造器
    Laravel中使用模型对数据进行操作
    Laravel中的模型的创建
    springboot
    不丢失log的情况下迁移git空间
    Vue2.0中v-for迭代语法变化(key、index)
  • 原文地址:https://www.cnblogs.com/hihocoder/p/11419174.html
Copyright © 2011-2022 走看看