• Borg Maze poj 3026


    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

    这个题的目的就是,找一个最小生成树,把所有的字母链接起来;

    大概思路;

     1.输入编号

     2.找出每两个字母之间的 权重并储存

     3.对于数组中的数据按权重排序

     4.并查集+Kruskal算法最小生成树。

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<cstdlib>
      4 #include<queue>
      5 using namespace std;
      6 int v[105],mapp[55][55],vis[55][55];
      7 int m,n,num,c[4]= {0,0,1,-1},r[4]= {1,-1,0,0};
      8 struct node
      9 {
     10     int a;
     11     int b;
     12     int weight;
     13 } s[55000],p,q;
     14 queue<node>que;
     15 int cmp(const void*a,const void *b)
     16 {
     17     return (*(node*)a).weight-(*(node*)b).weight;
     18 }
     19 int findl(int n)
     20 {
     21     return v[n]==n?n:findl(v[n]);
     22 }
     23 void get()//输入函数
     24 {
     25     int i,j,k=1;
     26     char ch;
     27     memset(mapp,0,sizeof(mapp));//清零
     28     scanf("%d %d",&n,&m);
     29     char str[300];//据说输入m,n后会有很多空格,我就是因为这个WA了一次
     30     gets(str);
     31     m++,n++;
     32     for(i=1; i<m; i++)
     33     {
     34         for(j=1; j<n; j++)
     35         {
     36             scanf("%c",&ch);
     37             if(ch=='A'||ch=='S')//大于O的表示字母
     38                 mapp[i][j]=k++;
     39             if(ch==' ')//-1表示可以走的路
     40                 mapp[i][j]=-1;
     41         }
     42         getchar();//去掉换行
     43     }
     44 }
     45 void bfs(int i,int j,int step)
     46 {
     47     int a,b,k;
     48     a=p.a=i,b=p.b=j,p.weight=step;
     49     memset(vis,0,sizeof(vis));
     50     vis[a][b]=1;
     51     que.push(p);
     52     while(!que.empty())
     53     {
     54         p=que.front();
     55         que.pop();
     56         for(k=0; k<4; k++)
     57         {
     58             if(!mapp[p.a+c[k]][p.b+r[k]]||vis[p.a+c[k]][p.b+r[k]])
     59                 continue;
     60             if(mapp[p.a+c[k]][p.b+r[k]]>0)//找到字母,将字母序号,权重存入数组
     61             {
     62                 s[num].a=mapp[a][b];
     63                 s[num].b=mapp[p.a+c[k]][p.b+r[k]];
     64                 s[num].weight=p.weight+1;
     65                 num++;
     66             }
     67             q.a=p.a+c[k],q.b=p.b+r[k],q.weight=p.weight+1;
     68             que.push(q);
     69             vis[q.a][q.b]=1;
     70         }
     71     }
     72 
     73 }
     74 int main()
     75 {
     76     int t,i,j,sum;
     77 
     78     scanf("%d",&t);
     79     while(t--)
     80     {
     81         num=sum=0;
     82         get();//输入函数
     83         for(i=1; i<m; i++)
     84         {
     85             for(j=1; j<n; j++)
     86             {
     87                 if(mapp[i][j]>0)
     88                     bfs(i,j,0);
     89             }
     90         }
     91         qsort(s,num,sizeof(node),cmp);//按权值从小到大排序
     92         for(i=0; i<105; i++)
     93             v[i]=i;
     94         for(i=0; i<num; i++)
     95         {
     96             if(findl(s[i].a)!=findl(s[i].b))//简单的并查集
     97             {
     98                 sum+=s[i].weight;
     99                 v[findl(s[i].a)]=s[i].b;
    100             }
    101         }
    102         printf("%d
    ",sum);
    103     }
    104     return 0;
    105 }
    View Code
  • 相关阅读:
    VS2013 此模板尝试加载组件程序集”NuGet.VisualStudio.interop,Version=1.0.0.0 的解决办法
    备份集中的数据库备份与现有的xx数据库不同”解决方法
    Couchbase 集群小实践
    EF5+MVC4系列(1) Podwerdesigner15.1设计数据库;PD中间表和EF实体模型设计器生成中间表的区别;EF5.0 表关系插入数据(一对多,多对多)
    Vue个人笔记
    [转载] HashMap的工作原理-hashcode和equals的区别
    用VS Code写C#
    MongoDB学习笔记一:MongoDB基础
    Vue学习笔记十三:Vue+Bootstrap+vue-resource从接口获取数据库数据
    Vue学习笔记十二:vue-resource的基本使用
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3255460.html
Copyright © 2020-2023  润新知