首页 > 代码库 > POJ 3026 Borg Maze

POJ 3026 Borg Maze

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

最小生成树的题。字母间的最小生成树。

首先是找点并建图。找点的话就遍历图形node[i][j]表示图中mp[i][j]的位置储存的是第几个点,若没有则为0;

然后就是bfs求各个点之间的最短路径,最后prim求最小生成树。就是建图的时候很无语呀。这个题还有一个巨坑,就是列如6 5之后还有空格,必须读入字符串,我还是太年轻了=。=

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<limits.h>
using namespace std;
char mp[110][110];//存图
int dis[110][110];//储存的图中每个点的距离
int node[110][110];//储存第i,j的位置是第几个点
int low[110];
int visit[110][110];//bfs判重
int vis[110];
int t,n,m,num;
int dr[4][2]={{1,0},{0,1},{0,-1},{-1,0}};
struct nod{
    int x,y;
    int step;
};
void bfs(int sx,int sy)
{
   memset(visit,0,sizeof(visit));
   nod st,ed;
   st.x=sx;
   st.y=sy;
   st.step=0;
   visit[sx][sy]=1;
   queue<nod>q;
   q.push(st);
   while(!q.empty())
   {
       st=q.front();
       q.pop();
       if(node[st.x][st.y]&&(st.x!=sx||st.y!=sy))
           dis[node[sx][sy]][node[st.x][st.y]]=st.step;
       for(int i=0;i<4;i++)
       {
           int xx=st.x+dr[i][0];
           int yy=st.y+dr[i][1];
           if(xx<0||xx>=n||yy<0||yy>=m)
               continue;
           if(mp[xx][yy]!='#'&&!visit[xx][yy])
           {
               visit[xx][yy]=1;
               ed.x=xx;
               ed.y=yy;
               ed.step=st.step+1;
               q.push(ed);
           }
       }
   }
}
void prim()
{
    int  ans=0,pos;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=num;i++)
       low[i]=dis[1][i];
    vis[1]=1;
    for(int i=1;i<=num;i++)
    {
        pos=-1;
        for(int j=1;j<=num;j++)
        {
            if(!vis[j]&&(pos==-1||low[pos]>low[j]))
                pos=j;
        }
        if(pos==-1)
            break;
        ans+=low[pos];
        vis[pos]=1;
        for(int j=1;j<=num;j++)
        {
            if(!vis[j]&&low[j]>dis[pos][j])
                low[j]=dis[pos][j];
        }
    }
    printf("%d\n",ans);
}
int main()
{
   char str[5];
   cin>>t;
   while(t--)
   {
       cin>>m>>n;
       gets(str);
       memset(node,0,sizeof(node));
       num=0;
       for(int i=0;i<n;i++)
       {
           gets(mp[i]);
           for(int j=0;j<m;j++)
           {
               if(mp[i][j]=='S'||mp[i][j]=='A')
               {
                  node[i][j]=++num;//表示是第几个点
//                  cout<<"i: "<<i<<"j: "<<j<<node[i][j]<<endl;
               }
           }
       }
       for(int i=0;i<n;i++)//遍历node中的每个点,求和其他点的最短路
       {
           for(int j=0;j<m;j++)
           {
               if(node[i][j])
                  bfs(i,j);
           }
       }
       prim();
   }
   return 0;
}