• POJ 3026 : Borg Maze(BFS + Prim)


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

    Borg Maze
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12086   Accepted: 3953

    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

     

      做法:直接用BFS算出每个A或者S到其他A或者S的距离,建出图后用Prim算法直接做就可以了。(有一个坑点就是题目的Input貌似有点错误,输入完两个整数后需要再输入一个串再输入图。我也是看了Discuss才知道,还有个奇奇怪怪的地方,有人知道什么回事请告诉下我,谢谢)。

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <iostream>
      4 #include <cmath>
      5 #include <string>
      6 #include <queue>
      7 #include <vector>
      8 #include <algorithm>
      9 using namespace std;
     10 #define MAXN 210
     11 #define INF 1000000000
     12 struct node
     13 {
     14     int x,y;
     15 };
     16 //node alien[210]; //这句是我之前写留下来的,可是不知道为什么删了之后就WA了,如果有人知道请务必告诉我(现在发现原来交C++就会错,交G++就不会了,不知道什么情况呀)
     17 char maze[210][210];
     18 int mp[210][210],low[210],used[210],vis[210][210],d[210][210];
     19 int n,m,dx[]={1,-1,0,0},dy[]={0,0,1,-1},cnt;
     20 
     21 bool check(int x,int y)
     22 {
     23     if(d[x][y]==-1&&maze[x][y]!='#'&&0<=x&&x<=m&&0<=y&&y<=n) return true;
     24     else return false;
     25 }
     26 
     27 void bfs(int sx,int sy)
     28 {
     29     queue<node> que;
     30     while(!que.empty()) que.pop();
     31     memset(d,-1,sizeof(d));
     32     node a;
     33     a.x=sx;a.y=sy;
     34     d[a.x][a.y]=0;
     35     que.push(a);
     36     while(!que.empty()){
     37         node b;
     38         a=que.front();que.pop();
     39         if(d[a.x][a.y]!=-1){
     40             mp[vis[sx][sy]][vis[a.x][a.y]]=d[a.x][a.y];
     41         }
     42         for(int k=0;k<4;k++){
     43             int nx=dx[k]+a.x,ny=dy[k]+a.y;
     44             if(check(nx,ny)){
     45                 b.x=nx,b.y=ny,d[b.x][b.y]=d[a.x][a.y]+1;
     46                 que.push(b);
     47             }
     48         }
     49     }
     50 }
     51 
     52 int Prim()
     53 {
     54     int pos=0,res=0;
     55     for(int i=0;i<cnt;i++) low[i]=mp[pos][i];
     56     for(int i=0;i<cnt;i++){
     57         int mi=INF;
     58         for(int j=0;j<cnt;j++){
     59             if(!used[j]&&low[j]<mi){
     60                 mi=low[j],pos=j;
     61             }
     62         }
     63         used[pos]=1;
     64         res+=low[pos];
     65         for(int j=0;j<cnt;j++){
     66             if(!used[j]&&low[j]>mp[pos][j]){
     67                 low[j]=mp[pos][j];
     68             }
     69         }
     70     }
     71     return res;
     72 }
     73 
     74 int main()
     75 {
     76     int t;
     77     cin>>t;
     78     while(t--){
     79         memset(used,0,sizeof(used));
     80         memset(vis,-1,sizeof(vis));
     81         for(int i=0;i<210;i++)
     82             for(int j=0;j<210;j++) mp[i][j]=INF;
     83         cin>>n>>m;
     84         char s[110];
     85         gets(s); //后面有些奇怪的东西,加上就AC了
     86         cnt=0;
     87         for(int i=0;i<m;i++){
     88             gets(maze[i]);
     89         }
     90 
     91         for(int i=0;i<m;i++){
     92             for(int j=0;j<n;j++){
     93                 if(maze[i][j]=='S'||maze[i][j]=='A'){
     94                     vis[i][j]=cnt++;
     95                 }
     96             }
     97         }
     98         for(int i=0;i<m;i++){
     99             for(int j=0;j<n;j++){
    100                 if(vis[i][j]!=-1) bfs(i,j);
    101             }
    102         }
    103         int ans=Prim();
    104         cout<<ans<<endl;
    105     }
    106     return 0;
    107 }

    2016-05-20

  • 相关阅读:
    结合人工智能的高性能医学:现状、挑战与未来
    2019年人工智能行业25大趋势
    睡眠分期--深度学习算法
    并行技术
    元数据--你有没有注意?
    战略、策略与执行
    机器学习前沿03
    元学习--learn to learn
    机器学习前沿02
    机器学习玩法
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5511810.html
Copyright © 2020-2023  润新知