poj3026——Borg Maze(BFS+最小生成树)

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 letterS” 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点处,求需要的最小步数。
因为人群可以分裂,其实就是不会走重复的路,模拟一下就正好是求一个最小生成树,其中S点和A点为点,他们之间的最短路径就是边。
最短路径由BFS可求出,然后转化成一棵树,再求最小生成树,注意其中的转换,因为一下用了两个模板,然后变量命名发生了冲突,调了两个多小时。。。

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 105
#define Mod 10001
using namespace std;
int x,y;
char mp[MAXN][MAXN];
int vis[MAXN][MAXN],map[MAXN][MAXN],dis[MAXN*MAXN],vis1[MAXN*MAXN];
struct Node
{
    int x,y,step;
};
Node p[MAXN*MAXN];
int kw[MAXN][MAXN];
int num;
void dfs(int xi,int yi)
{
    memset(vis,0,sizeof(vis));
    Node start,tmp;
    start.x=xi,start.y=yi;
    start.step=0;
    vis[xi][yi]=1;
    queue<Node> q;
    q.push(start);
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(mp[tmp.x][tmp.y]=='A'||mp[tmp.x][tmp.y]=='S')
        {
            map[kw[xi][yi]][kw[tmp.x][tmp.y]]=tmp.step;
            map[kw[tmp.x][tmp.y]][kw[xi][yi]]=tmp.step;
        }
        Node tmp1;
        tmp1.x=tmp.x+1,tmp1.y=tmp.y,tmp1.step=tmp.step+1;
        if(tmp1.x<x&&!vis[tmp1.x][tmp1.y]&&mp[tmp1.x][tmp1.y]!='#')
        {
            vis[tmp1.x][tmp1.y]=1;
            q.push(tmp1);
        }
        tmp1.x=tmp.x-1,tmp1.y=tmp.y,tmp1.step=tmp.step+1;
        if(tmp1.x>=0&&!vis[tmp1.x][tmp1.y]&&mp[tmp1.x][tmp1.y]!='#')
        {
            vis[tmp1.x][tmp1.y]=1;
            q.push(tmp1);
        }
        tmp1.x=tmp.x,tmp1.y=tmp.y+1,tmp1.step=tmp.step+1;
        if(tmp1.y<y&&!vis[tmp1.x][tmp1.y]&&mp[tmp1.x][tmp1.y]!='#')
        {
            vis[tmp1.x][tmp1.y]=1;
            q.push(tmp1);
        }
        tmp1.x=tmp.x,tmp1.y=tmp.y-1,tmp1.step=tmp.step+1;
        if(tmp1.y>=0&&!vis[tmp1.x][tmp1.y]&&mp[tmp1.x][tmp1.y]!='#')
        {
            vis[tmp1.x][tmp1.y]=1;
            q.push(tmp1);
        }
    }
}
int prim()
{
    int n=num;
    int i,j,pos,min,ans=0;
    memset(vis1,0,sizeof(vis1));
    memset(dis,0,sizeof(dis));
    vis1[1]=1,pos=1;
    for(i=1; i<=n; ++i)
        if(i!=pos)
            dis[i]=map[pos][i];
    for(i=1; i<n; ++i)
    {
        min=INF;
        for(j=1; j<=n; ++j)
            if(!vis1[j]&&dis[j]<min)
            {
                min=dis[j];
                pos=j;
            }
        ans+=min;
        vis1[pos]=1;
        for(j=1; j<=n; ++j)
            if(!vis1[j]&&dis[j]>map[pos][j])
                dis[j]=map[pos][j];
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(mp,0,sizeof(mp));
        memset(map,0,sizeof(map));
        num=0;
        cin>>y>>x;
        gets(mp[0]);
        for(int i=0; i<x; ++i)
        {
            gets(mp[i]);
            for(int j=0; j<y; ++j)
                if(mp[i][j]=='S'||mp[i][j]=='A')
                {
                    p[++num].x=i;
                    p[num].y=j;
                    kw[i][j]=num;
                }
        }
        for(int i=1; i<=num; ++i)
            dfs(p[i].x,p[i].y);
        printf("%d\n",prim());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值