zoukankan      html  css  js  c++  java
  • Borg Maze(BFS+MST)

    Borg Maze

    http://poj.org/problem?id=3026

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18230   Accepted: 5870

    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

    很坑,数据n,m后面有很长的空格,要用gets读掉

      1 #include<iostream>
      2 #include<algorithm>
      3 #include<queue>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<cstdio>
      7 using namespace std;
      8 
      9 struct sair{
     10     int x,y,step;
     11 }p[100005];
     12 int fa[100005];
     13 int n,m;
     14 
     15 int Find(int x){
     16     int r=x,y;
     17     while(x!=fa[x]){
     18         x=fa[x];
     19     }
     20     while(r!=x){
     21         y=fa[r];
     22         fa[r]=x;
     23         r=y;
     24     }
     25     return x;
     26 }
     27 
     28 int join(int x,int y){
     29     int xx=Find(x);
     30     int yy=Find(y);
     31     if(xx!=yy){
     32         fa[xx]=yy;
     33         return true;
     34     }
     35     return false;
     36 }
     37 
     38 struct DIST{
     39     int x,y,dis;
     40 }dis[1000005];
     41 
     42 bool cmp(DIST a,DIST b){
     43     return a.dis<b.dis;
     44 }
     45 
     46 int dir[4][2]={0,1,1,0,0,-1,-1,0};
     47 int co;
     48 
     49 char mp[505][505];
     50 int book[505][505];
     51 
     52 void BFS(int x,int y){
     53     queue<sair>Q;
     54     memset(book,0,sizeof(book));
     55     sair s,e;
     56     s.x=x,s.y=y,s.step=0;;
     57     Q.push(s);
     58     while(!Q.empty()){
     59         s=Q.front();
     60         Q.pop();
     61         for(int i=0;i<4;i++){
     62             e.x=s.x+dir[i][0];
     63             e.y=s.y+dir[i][1];
     64             if(e.x>=0&&e.x<n&&e.y>=0&&e.y<m&&!book[e.x][e.y]&&mp[e.x][e.y]!='#'){
     65                 e.step=s.step+1;
     66                 Q.push(e);
     67                 book[e.x][e.y]=1;
     68                 if(mp[e.x][e.y]=='A'||mp[e.x][e.y]=='S'){
     69                     dis[co].x=x*m+y+1,dis[co].y=e.x*m+e.y+1,dis[co++].dis=e.step;
     70                 }
     71             }
     72         }
     73     }
     74 }
     75 
     76 int main(){
     77     int T;
     78     scanf("%d",&T);
     79     while(T--){
     80         co=0;
     81         char dd[105];
     82         scanf("%d %d",&m,&n);
     83         gets(dd);
     84         for(int i=0;i<=m*n;i++) fa[i]=i;
     85         for(int i=0;i<n;i++) gets(mp[i]);
     86         for(int i=0;i<n;i++){
     87             for(int j=0;j<m;j++){
     88                 if(mp[i][j]=='S'||mp[i][j]=='A'){
     89                     BFS(i,j);
     90                 }
     91             }
     92         }
     93         sort(dis,dis+co,cmp);
     94         int ans=0;
     95         for(int i=0;i<co;i++){
     96             if(join(dis[i].x,dis[i].y)){
     97                 ans+=dis[i].dis;
     98             }
     99         }
    100         printf("%d
    ",ans);
    101     }
    102 }
    103 /*
    104 1
    105 50 7
    106 ##################################################
    107 #            AAAAAAA#AAAAAA AAA            S     #
    108 #   AAAAAAAAAA       AAAAAAAAAAAAAA#####AAAAA### #
    109 #    A##A#A#A#A###A#A#A#A#A#A#A#A#A#A#A##A#A#    #
    110 #    A                                 ###########
    111 #             AAAAAAAAAAAA##########             #
    112 ##################################################
    113 */
    View Code
  • 相关阅读:
    端口被占用
    启动Windows防火墙提示“0x8007042c"
    vue创建全局组件
    vue中过度动画之列表添加删除动画实现
    vue中过渡动画(类名结合动画实现方式)
    vue中过渡动画(类名实现方式)
    this.$nextTick()方法的使用
    利用axios获取数据并渲染到视图层
    axios的简单使用
    watch深度监听
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9984526.html
Copyright © 2011-2022 走看看