poj 3026 Borg Maze bfs+prim

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

811

题意及分析:

在迷宫里由起点S出发,寻找所有的A。关键的理解在于距离是怎么算的。在S和A这两个地方可以分组。距离总和是所有分组的距离之和。比如说,我由S出发,找到了一个A,就可以分组。然后由这个A出发,到下一个A的最短距离就是要计算的数据。

这样的话,将S和A都看做节点,找到一棵连接他们的最小生成树,那么此时距离的总和就是最小的了。

数据里有一个地方让人很不爽。就是 再输入m,n后会有空格。所以必须得用gets,如果是getchar,就会死命的wa。(积分赛碰到了这样的东西就不多说了)

AC代码:

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
        #include 
        
          #include 
         
           #include 
          
            #include 
           
             #define INF 0x3f3f3f3f using namespace std; struct node { int x,y; int num; }que[10000]; struct point { int x; int y; }aim[150]; int mox[4]={1,-1,0,0}; int moy[4]={0,0,-1,1}; char maze[100][100]; int vis[100][100],edge[110][110],mark[110],dis[150],vis2[150]; int x,y,num,rec; void bfs(int k) { int front=0,rear=0; node now,tmp; int i,j; memset(vis,0,sizeof(vis)); memset(mark,0,sizeof(mark)); now.x=aim[k].x,now.y=aim[k].y,now.num=0; vis[now.x][now.y]=1; mark[k]=1; que[rear++]=now; while(front 
            
              edge[now][j]) dis[j]=edge[now][j]; } int sum=0; for(i=1;i<=num;i++) sum+=dis[i]; return sum; } int main() { int i,j,t; scanf("%d",&t); while(t--) { scanf("%d%d",&x,&y); // getchar(); gets(maze[0]); //就是这个地方,据说是 输入x,y之后不是马上给你一个换行,而是给你空格再给换行 for(i=0;i 
              
             
            
           
          
        
      
      
     
     
    
    
   
   

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值