zoukankan      html  css  js  c++  java
  • poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9718   Accepted: 3263

    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

     

     

     

     

     

     

     

    注意注意,本题两大神坑

    1   数组题里面说的是50,其实开100都是Wa,我开到300就AC了

    2 再输入完行与列后,不可以用getchar()在进行输入空行,,,,,,必须用gets(str[0]);

     

     

     

     

    本题详解

    在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。

    根据题意的“分离”规则,重复走过的路不再计算

    因此当使用prim算法求L的长度时,根据算法的特征恰好不用考虑这个问题(源点合并很好地解决了这个问题),L就是最少生成树的总权值W

    由于使用prim算法求在最小生成树,因此无论哪个点做起点都是一样的,(通常选取第一个点),因此起点不是S也没有关系

    所以所有的A和S都可以一视同仁,看成一模一样的顶点就可以了

    最后要注意的就是 字符的输入

    cin不读入空字符(包括 空格,换行等)

    gets读入空格,但不读入换行符)

    剩下的问题关键就是处理 任意两字母间的最短距离,由于存在了“墙#” ,这个距离不可能单纯地利用坐标加减去计算,必须额外考虑,推荐用BFS(广搜、宽搜),这是本题的唯一难点,因为prim根本直接套用就可以了

    任意两字母间的最短距离 时不能直接用BFS求,

    1、必须先把矩阵中每一个允许通行的格看做一个结点(就是在矩阵内所有非#的格都作为图M的一个顶点),对每一个结点i,分别用BFS求出它到其他所有结点的权值(包括其本身,为0),构造结点图M;

    2、然后再加一个判断条件,从图M中抽取以字母为顶点的图,进而构造字母图N

    这个判定条件就是当结点图M中的某点j为字母时,把i到j的权值再复制(不是抽离)出来,记录到字母图N的邻接矩阵中

    3、剩下的就是对字母图N求最小生成树了

      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<iostream>
      4 #include<queue>
      5 #include<algorithm>
      6 using namespace std;
      7 char str[300][300];
      8 int vis[300],tvis[300][300],dis[300],tdis[300][300];
      9 int point[300][300];
     10 int map[300][300];
     11 struct node{
     12   int x;
     13   int y;
     14 };
     15 int  tnext[4][2]={0,1,1,0,-1,0,0,-1};
     16 int ans,x,y;
     17 int prim(int u){
     18    int sum=0;
     19    for(int i=1;i<=ans;i++)
     20      dis[i]=map[u][i];
     21      vis[u]=1;
     22      for(int k=1;k<ans;k++){
     23         int tmin=999999999;
     24         int temp;
     25         for(int j=1;j<=ans;j++){
     26             if(dis[j]<tmin&&!vis[j]){
     27                  tmin=dis[j];
     28                  temp=j;
     29             }
     30         }
     31         sum+=tmin;
     32         vis[temp]=1;
     33         for(int i=1;i<=ans;i++){
     34            if(dis[i]>map[temp][i]&&!vis[i])
     35            dis[i]=map[temp][i];
     36         }
     37 
     38      }
     39      return sum;
     40 }
     41 void bfs(int tx,int ty){
     42      memset(tvis,0,sizeof(tvis));
     43      memset(tdis,0,sizeof(tdis));
     44      queue<node>q;
     45      node temp,next;
     46      temp.x=tx;
     47      temp.y=ty;
     48      q.push(temp);
     49      tvis[tx][ty]=1;
     50      int xx,yy;
     51      while(!q.empty()){
     52          temp=q.front();
     53          q.pop();
     54          if(point[temp.x][temp.y]){
     55             map[point[tx][ty]][point[temp.x][temp.y]]=tdis[temp.x][temp.y];
     56          }
     57 
     58          for(int k=0;k<4;k++){
     59              next.x=xx=temp.x+tnext[k][0];
     60              next.y=yy=temp.y+tnext[k][1];
     61              if(xx>=1&&xx<=x&&yy>=1&&yy<=y&&!tvis[xx][yy]&&str[xx][yy]!='#'){
     62                       tdis[xx][yy]=tdis[temp.x][temp.y]+1;
     63                         tvis[xx][yy]=1;
     64                         q.push(next);
     65              }
     66          }
     67      }
     68 }
     69 int main(){
     70    int t;
     71    scanf("%d",&t);
     72    while(t--){
     73        memset(point,0,sizeof(point));
     74     memset(str,0,sizeof(str));
     75     memset(vis,0,sizeof(vis));
     76     memset(dis,0,sizeof(dis));
     77      memset(map,0,sizeof(map));
     78 
     79     ans=0;
     80 
     81      scanf("%d%d",&y,&x);
     82      gets(str[0]);
     83      for(int i=1;i<=x;i++){
     84         gets(str[i]);
     85         for(int j=0;j<y;j++){
     86            if(str[i][j]=='S'||str[i][j]=='A'){
     87               point[i][j]=++ans;
     88            }
     89         }
     90      }
     91      for(int i=1;i<=x;i++){
     92         for(int j=1;j<=y;j++){
     93            if(point[i][j])
     94            bfs(i,j);
     95         }
     96      }
     97      printf("%d
    ",prim(1));
     98    }
     99    return 0;
    100 }

     

     

     

  • 相关阅读:
    butter
    医院设置
    NOIP 2000 进制转换
    图的M 着色问题
    闭合区域面积统计
    字符序列
    装载问题
    n皇后问题
    跳马问题
    数独
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4631356.html
Copyright © 2011-2022 走看看