ACM: 图论题 BFS + STL + priority…

来源:互联网 发布:淘宝网店加盟哪家好 编辑:程序博客网 时间:2024/05/05 19:22

                                                                       Borg Maze
Description
The Borg is an immensely powerful race of enhanced humanoids fromthe delta quadrant of the galaxy. The Borg collective is the termused to describe the group consciousness of the Borg civilization.Each Borg individual is linked to the collective by a sophisticatedsubspace network that insures each member is given constantsupervision and guidance.

Your task is to help the Borg (yes, really) by developing a programwhich helps the Borg to estimate the minimal cost of scanning amaze for the assimilation of aliens hiding in the maze, by movingin north, west, east, and south steps. The tricky thing is that thebeginning of the search is conducted by a large group of over 100individuals. Whenever an alien is assimilated, or at the beginningof the search, the group may split in two or more groups (but theirconsciousness is still collective.). The cost of searching a mazeis definied as the total distance covered by all the groupsinvolved in the search together. That is, if the original groupwalks five steps, then splits into two groups each walking threesteps, 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 suchthat 1 <= x,y <= 50. After this, ylines follow, each which x characters. For each character, a space`` '' stands for an open space, a hash mark ``#'' stands for anobstructing wall, the capital letter ``A'' stand for an alien, andthe capital letter ``S'' stands for the start of the search. Theperimeter of the maze is always closed, i.e., there is no way toget out from the coordinate of the ``S''. At most 100 aliens arepresent in the maze, and everyone is reachable.

Output
For every test case, output one line containing the minimal cost ofa 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 点出发 , 寻找外星人 , 可以分组进行搜索 , 分组可以是一开始就分组 , 或者是找到一个
         外星人后再分组 , 但是全部组的行动步数是叠加起来的 , 要计算出最小的路径找到全部ET.

解题思路:
              1. 想了好久啊 . 看了题解 , 弄明白了分组是怎么回事.(题意中有解释)
              2. 好了 , 分组解决了 , 接下来是选择算法 , 分组的模拟与BFS(广搜类似.)
              3. 寻找外星人用BFS , 附加条件是要MST(最小生成树), 采用优先队列.

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
#define MAX 53

typedef struct priority_node
{
    int u ,v;
    intdist;
    booloperator< (const priority_node&st)const
    {
       return st.dist < dist;
    }
}PNODE;

typedef struct node
{
    int u ,v;
    intdist;
}NODE;

typedef priority_queue<PNODE>PQU;
typedef queue<NODE> QU;

int dir[4][2] = { {0,1} , {0,-1} , {1,0} , {-1,0} };
int n , m;
char map[MAX][MAX];
NODE cur , next;
PNODE pcur;

int find(int su,int sv)
{
    PQUpqu;
    QU qu;
    boolvisit[MAX][MAX] , mark[MAX][MAX];
    int sum =0;
    int i;
    int flag =0;
   memset(visit,false,sizeof(visit));
    
    pcur.u =su;
    pcur.v =sv;
    pcur.dist =0;
   pqu.push(pcur);
    
    while(!pqu.empty() )   // MST +priority_queue;
    {
       pcur = pqu.top();
       while(visit[ pcur.u ][ pcur.v ] == true)
       {
           pqu.pop();
           if( pqu.empty() )
           {
               return sum;
           }
           pcur = pqu.top();
       }
       
       pqu.pop();
       visit[ pcur.u ][ pcur.v ] = true;
       sum += pcur.dist;
       cur.u = pcur.u;
       cur.v = pcur.v;
       cur.dist = 0;
       
       qu.push(cur);
       memset(mark,false,sizeof(mark));
       mark[ cur.u ][ cur.v ] = true;
       
       while( !qu.empty() )  //bfs
       {
           cur = qu.front();
           qu.pop();
           if(visit[ cur.u ][ cur.v ] == false&& map[ cur.u ][ cur.v ] =='A')
           {
               pcur.u = cur.u;
               pcur.v = cur.v;
               pcur.dist = cur.dist;
               pqu.push(pcur);
           }
           
           for(i = 0; i < 4; ++i)
           {
               next.u = cur.u + dir[i][0];
               next.v = cur.v + dir[i][1];
               if(next.u >= 1 &&next.u <= m &&next.v >= 1 &&next.v <= n && mark[next.u ][ next.v ] == false && map[next.u ][ next.v ] != '#')
               {
                   mark[ next.u ][ next.v ] = true;
                   next.dist = cur.dist + 1;
                   qu.push(next);
               }
           }
       }
    }
    
    returnsum;
}

int main()
{
//   freopen("input.txt","r",stdin);
    intnum;
   scanf("%d",&num);
   while(num--)
    {
       int i , j;
       int su , sv;
       scanf("%d %d",&n,&m);
       for(i = 1; i <= m; ++i)
       {
           while(getchar() != '\n');
           for(j = 1; j <= n; ++j)
           {
               scanf("%c",&map[i][j]);
               if(map[i][j] == 'S')
               {
                   su = i;
                   sv = j;
               //    cout<< su<< " "<< sv<< endl;
               }
           }
       }
       
       int result = find(su,sv);
       printf("%d\n",result);
    }
    
    return0;
}

0 0
原创粉丝点击