POJ 3026 - Borg Maze(最小树+BFS)

11 篇文章 0 订阅
10 篇文章 0 订阅

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7226 Accepted: 2429

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


==========================

只能在S或A处分裂,满足最小生成树,对每个点都bfs,求出各个点之间的距离,再求最小生成树


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
const int INF=0x3f3f3f3f;
int dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
int dis[111][111],vis[111][111],mark[111][111];
int m,n,cnt;
char map[111][111];

struct point
{
    int x,y,step,id;
}a[333],p,temp;

bool check(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=m) return 1;
    else return 0;
}

void bfs(point start)
{
    int num=0;
    queue<point>que;
    p.x=start.x;p.y=start.y;p.step=0;
    que.push(p);
    vis[p.x][p.y]=1;
    while(!que.empty())
    {
        temp=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            p.x=temp.x+dir[i][0];
            p.y=temp.y+dir[i][1];
            p.step=temp.step+1;
            if(!check(p.x,p.y)||map[p.x][p.y]=='#'||vis[p.x][p.y]) continue;
            que.push(p);
            vis[p.x][p.y]=1;
            if((map[p.x][p.y]=='A'||map[p.x][p.y]=='S'))
            {
                if(dis[ mark[p.x][p.y] ][start.id]==INF)
                    dis[ mark[p.x][p.y ]][start.id]=dis[start.id][ mark[p.x][p.y] ]=p.step;
                num++;
            }
            if(num==cnt-1) return;
        }
    }
}

int prim(int s)
{
    int res=0,vis[111],d[111];
    for(int i=1; i<=cnt; i++)
    {
        d[i]=INF;
        vis[i]=false;
    }
    d[s]=0;
    while(1)
    {
        int u=0,mn=INF;
        for (int i=1;i<=cnt;i++)
        {
            if (!vis[i]&&d[i]<mn)
            {
                mn=d[i];
                u=i;
            }
        }
        if(u==0) break;
        vis[u]=true;
        res+=mn;
        //cout<<"u="<<u<<" res="<<res<<" "<<endl;
        for (int i=1;i<=cnt;i++)
        {
            if (d[i]>dis[u][i]&&u!=i)
                d[i]=dis[u][i];
        }
    }
    return res;
}

int main()
{
    int T;char str[1111];
    scanf("%d",&T);
    while(T--)
    {
        memset(dis,INF,sizeof(dis));
        memset(mark,0,sizeof(mark));
        scanf("%d%d",&m,&n);
        gets(str);
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            gets(map[i]+1);
            for(int j=1;j<=m;j++)
            {
                if(map[i][j]=='S'||map[i][j]=='A')
                {
                    a[++cnt].id=cnt;
                    a[cnt].x=i;
                    a[cnt].y=j;
                    mark[i][j]=cnt;
                }
            }
        }
        for(int i=1;i<=cnt;i++)
            dis[i][i]=0;
        for(int i=1;i<=cnt;i++)
        {
            memset(vis,0,sizeof(vis));
            bfs(a[i]);
        }
        /*for(int i=1;i<=cnt;i++)
        {
            for(int j=1;j<=cnt;j++)
                cout<<dis[i][j]<<" ";
            cout<<endl;
        }*/
        printf("%d\n",prim(1));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值