zoukankan      html  css  js  c++  java
  • Borg Maze prime算法 &&bfs()::重要的是广搜

    Problem 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 #include<iostream>
      2 #include<string>
      3 #include<cmath>
      4 #include<cstring>
      5 #include<cstdio>
      6 #include<queue>
      7 using namespace std;
      8 int map[2010][2010];
      9 int vis1[2010],n;
     10 int dis1[1001],dis2[1001][1001],vis2[1001][1001];
     11 int point[1001][1001];
     12 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
     13 int cas,row,col;
     14 char str[100][100];
     15 int i,j,k;
     16 struct node
     17 {
     18     int x,y;
     19 };
     20 void bfs(int si,int sj)//广搜两点之间的最小距离
     21 {
     22     memset(vis2,0,sizeof(vis2));
     23     memset(dis2,0,sizeof(dis2));
     24     queue<node>Q;
     25     while(!Q.empty())
     26       Q.pop();
     27     node cur,next;
     28     cur.x=si;
     29     cur.y=sj;
     30     Q.push(cur);
     31     vis2[si][sj]=1;
     32     int x,y;
     33     while(!Q.empty())
     34     {
     35         cur=Q.front();
     36         Q.pop();
     37         if(point[cur.x][cur.y])
     38          map[point[si][sj]][point[cur.x][cur.y]]=dis2[cur.x][cur.y];
     39         for(int it=0;it<4;it++)
     40         {
     41            next.x=x=cur.x+dir[it][0];
     42            next.y=y=cur.y+dir[it][1];
     43            if(x>=1&&x<=row&&y>=1&&y<=col&&!vis2[x][y]&&str[x][y]!='#')
     44             {
     45                vis2[x][y]=1;
     46                dis2[x][y]=dis2[cur.x][cur.y]+1;
     47                Q.push(next);
     48             }
     49         }
     50     }
     51 }
     52 int prime()
     53 {
     54     memset(vis1,0,sizeof(vis1));
     55     int lowcost[2001];
     56     int it,jt;
     57     for(it=1;it<=n;it++)
     58     {
     59         lowcost[it]=map[1][it];
     60     }
     61     vis1[1]=1;
     62     int sum=0;
     63     int kt;
     64     for(it=1;it<n;it++)
     65     {
     66         kt=1;
     67         while(vis1[kt])kt++;
     68         if(kt==n+1)break;
     69         for(jt=1;jt<=n;jt++)
     70         {
     71             if(!vis1[jt]&&lowcost[jt]<lowcost[kt])kt=jt;
     72         }
     73         vis1[kt]=1;
     74         sum+=lowcost[kt];
     75         for(jt=1;jt<=n;jt++)
     76          if(!vis1[jt]&&lowcost[jt]>map[kt][jt])
     77            lowcost[jt]=map[kt][jt];
     78     }
     79     return sum;
     80 }
     81 int main()
     82 {
     83   scanf("%d",&cas);
     84   while(cas--)
     85   {
     86       memset(point,0,sizeof(point));
     87       scanf("%d%d",&col,&row);
     88       gets(str[0]);
     89       n=0;
     90       for(i=1;i<=row;i++)
     91       {
     92           gets(str[i]+1);
     93           for(j=1;j<=col;j++)
     94            {
     95                if(str[i][j]=='A'||str[i][j]=='S')
     96                 point[i][j]=++n;
     97            }
     98       }
     99       for(i=1;i<=row;i++)
    100        for(j=1;j<=col;j++)
    101         if(point[i][j])
    102          bfs(i,j);
    103       /*for(i=1;i<=n;i++)
    104       {
    105           for(j=1;j<=n;j++)
    106             cout<<map[i][j]<<' ';
    107           cout<<endl;
    108       }*/
    109       printf("%d
    ",prime());
    110   }
    111 }
    View Code

  • 相关阅读:
    网络基础 03 交换机基本原理与配置
    网络基础 02 网络布线与数制转换
    C#操作excel(读取excel里的图片并show在页面上)
    让IIS支持中文文件名和目录
    Asp.net中防止用户多次登录的方法
    【转摘】一道关于实例化顺序的C#面试题
    选项卡调其他窗体方法
    同一个工作组的计算机能ping通不能访问对方共享资源
    Combobox出现System.Data.DataRowView的原因,以及指定ValueMember的时机问题
    无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口。
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3379147.html
Copyright © 2011-2022 走看看