Borg Maze POJ - 3026(最小生成树prim,bfs)

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
这里写图片描述
Sample Output
8
11
题意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度。
题意明白之后就是解题方法了,最短的路径我们可以通过BFS找出,最小生成树可以使用Prim,也就是求出每个字母之间的距离之后把所有的字母用最小生成树连接起来

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;

#define N 105
#define INF 0xfffffff
int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
int D[N][N], n, m, vis[N], used[N][N],b[N][N], dist[N];
char map[N][N];

struct node
{
    int x, y, step;
} a[N];

void bfs(int s, int x, int y)
{
    memset(used, 0, sizeof(used));
    node p,q;
    queue<node>Q;
    p.x = x;
    p.y = y;
    p.step = 0;
    Q.push(p);
    used[x][y] = 1;
    while(Q.size())
    {
        p = Q.front();
        Q.pop();
        if(map[p.x][p.y] >= 'A' && map[p.x][p.y] <= 'Z')
            D[s][ b[p.x][p.y] ] = p.step;
        for(int i=0; i<4; i++)
        {
            q.x = p.x + dir[i][0];
            q.y = p.y + dir[i][1];
            if(q.x<m && q.x>=0 && q.y>=0 && q.y<n && used[q.x][q.y]!=1 && map[q.x][q.y] != '#')
            {
                used[q.x][q.y] = 1;
                q.step = p.step + 1;
                Q.push(q);
            }
        }
    }
}

int Prim(int cnt)
{
    int ans=0;
    vis[1] = 1;
    for(int i=1; i<=cnt; i++)
        dist[i] = D[1][i];
    for(int i=1; i<=cnt; i++)
    {
        int Min = INF, index = -1;
        for(int j=1; j<=cnt; j++)
            if(vis[j]==0 && Min > dist[j])
            {
                Min = dist[j];
                index = j;
            }
        if(index==-1)break;
        ans += Min;
        vis[index] = 1;
        for(int j=1; j<=cnt; j++)
        {
            if(vis[j] == 0 && dist[j] > D[index][j])
               dist[j] = D[index][j];
        }
    }
    return ans;
}

int main()
{
    int T, ans;
    scanf("%d", &T);
    while(T--)
    {
        memset(a, 0, sizeof(a));
        memset(vis, 0, sizeof(vis));
        memset(map, 0, sizeof(map));
        for(int i=0; i<N; i++)
        {
            dist[i] = INF;
            for(int j=0; j<N; j++)
                D[i][j] = INF;
            D[i][i] = 0;
        }

        int cnt = 1;
        scanf("%d%d ", &n,&m);
        for(int i=0; i<m; i++)
        {
            gets(map[i]);
            for(int j=0; j<n; j++)
            {
                if(map[i][j] <= 'Z' && map[i][j] >= 'A')
                    b[i][j] = cnt, cnt++;
            }
        }
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(map[i][j] <= 'Z' && map[i][j] >= 'A')
                    bfs(b[i][j], i, j);
            }
        }
        ans = Prim(cnt-1);
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值