zoukankan      html  css  js  c++  java
  • POJ3026——Borg Maze(BFS+最小生成树)

    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

    题目大意:

        一个迷宫,' '代表可以到达,'#'代表不可以到达。'S'初始点。'A'是要到达的点。

        输出从S开始路过所有的A点所需要的路程(重复路过只算一次)。

    解题思路:

        因为重复的路过某个点,路程只算一次,就相当于计算一个包含所有的'A'和'S'的最小树。

        使用BFS来计算边的权值(上下左右四种情况,简单的BFS,注意初始点的dis为0),来构成完全无向图,再用Kruskal算法算出权值和即可。(注意题目数据范围,数组开小了会WA)

        PS : POJ坑爹的后台,每行后面可能有坑爹的多余空格,注意处理掉即可。

    Code:

      1 #include<stdio.h>
      2 #include<iostream>
      3 #include<algorithm>
      4 #include<string>
      5 #define MAXN 1000
      6 using namespace std;
      7 struct point
      8 {
      9     int x,y;
     10 } P[120];
     11 struct point q[12000];
     12 int N,M,father[120];
     13 bool vis[60][60],v[60][60];
     14 int dis[12000];
     15 struct edge
     16 {
     17     int begin,end;
     18     int dis;
     19 } T[100*100];
     20 void init()
     21 {
     22     memset(vis,0,sizeof(vis));
     23     for (int i=1; i<=110; i++)
     24         father[i]=i;
     25 }
     26 int bfs(int a,int b)
     27 {
     28     int front=0,rear=1;
     29     q[front]=P[a];
     30     for (int i=1; i<=M; i++)
     31         for (int j=1; j<=N; j++)
     32             v[i][j]=vis[i][j];
     33     dis[front]=0;
     34     while (front<rear)
     35     {
     36         if (q[front].x==P[b].x&&q[front].y==P[b].y) break;
     37         int x=q[front].x,y=q[front].y;
     38         int a[4]= {x-1,x+1,x,x},b[4]= {y,y,y+1,y-1};
     39         for (int i=0; i<=3; i++)
     40             if (a[i]>=1&&a[i]<=M&&b[i]>=1&&b[i]<=N&&!v[a[i]][b[i]])
     41             {
     42                 q[rear].x=a[i],q[rear].y=b[i];
     43                 dis[rear]=dis[front]+1;
     44                 v[a[i]][b[i]]=1;
     45                 rear++;
     46             }
     47         front++;
     48     }
     49     return dis[front];
     50 }
     51 int find(int x)
     52 {
     53     if (father[x]!=x)
     54         father[x]=find(father[x]);
     55     return father[x];
     56 }
     57 void join(int x,int y)
     58 {
     59     int fx=find(x),fy=find(y);
     60     father[fx]=fy;
     61 }
     62 bool cmp(struct edge a,struct edge b)
     63 {
     64     return a.dis<b.dis;
     65 }
     66 int main()
     67 {
     68     int C;
     69     cin>>C;
     70     while (C--)
     71     {
     72         init();
     73         cin>>N>>M;
     74         int k=1;
     75         char ch;
     76         while (1)
     77         {
     78             ch=getchar();
     79             if (ch=='
    ') break;
     80         }
     81         for (int i=1; i<=M; i++)
     82         {
     83             for (int j=1; j<=N; j++)
     84             {
     85                 char tmp;
     86                 scanf("%c",&tmp);
     87                 if (tmp=='#') vis[i][j]=1;
     88                 else vis[i][j]=0;
     89                 if (tmp=='A'||tmp=='S')
     90                 {
     91                     P[k].x=i,P[k].y=j;
     92                     k++;
     93                 }
     94             }
     95             while (1)
     96             {
     97                 ch=getchar();
     98                 if (ch=='
    ') break;
     99             }
    100         }
    101         int t=1;
    102         for (int i=1; i<k; i++)
    103             for (int j=1; j<i; j++)
    104             {
    105                 T[t].begin=i;
    106                 T[t].end=j;
    107                 T[t].dis=bfs(i,j);
    108                 t++;
    109             }
    110         int cnt=0,sum;
    111         sort(T+1,T+t,cmp);
    112         for (int i=1; i<t; i++)
    113         {
    114             if (find(T[i].begin)!=find(T[i].end))
    115             {
    116                 cnt+=T[i].dis;
    117                 join(T[i].begin,T[i].end);
    118                 sum++;
    119                 if (sum==k-2) break;
    120             }
    121         }
    122         printf("%d
    ",cnt);
    123     }
    124     return 0;
    125 }
  • 相关阅读:
    CentOS 7 镜像下载
    Ambari+HDP生产集群搭建(二)
    elasticsearch-head 关闭窗口服务停止解决方案
    git提交错误 error: failed to push some refs to
    git提交错误 git config --global user.email "you@example.com" git config --global user.name "Your Name
    Java SE入门(一)——变量与数据类型
    markdown基本语法
    numpy的基本API(四)——拼接、拆分、添加、删除
    数理统计(二)——Python中的概率分布API
    统计学习方法与Python实现(三)——朴素贝叶斯法
  • 原文地址:https://www.cnblogs.com/Enumz/p/3857297.html
Copyright © 2011-2022 走看看