POJ3026(最小生成树)(BFS)(注意输入)

题目链接
Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15796 Accepted: 5141

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#A##
# # A# # # A#
#S  ## #S  ##
##### #####
7 7

#AAA### #AAA###
#    A# #    A#
# S ### # S ###
#     # #     #
#AAA### #AAA###
##### #####

Sample Output

8
11

题意:问从S出发,派机器人到A的地方,机器人可以在图中分裂,求最少步数。
题解:因为可以分裂,那到一个A的最小步数就是从离他最近的那个出发的步数,就是最小生成树了。我们可以BFS搜索任意两个A或者S之间的距离作为边权,然后求一个最小生成树就好了。
注意输入,会有空格,然后。。。。用gets()也卡了好久,因为输入数字的回车不知道为啥用getchar吃不掉,看了看别人博客学习了一下。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
const int maxn = 105;
int t,n,m;
int id[maxn][maxn],mount,tot,dir[][2] = {1,0,-1,0,0,-1,0,1},f[maxn * maxn];
char str[maxn][maxn];
struct bbq{
    int x,y,len,id;
};
bbq e[maxn * maxn];
void bfs(int x1, int y1)
{
    int vis[maxn][maxn];
    memset(vis,0,sizeof vis);
    vis[x1][y1] = 1;
    queue<bbq> q;
    bbq temp;
    temp.x = x1,temp.y = y1, temp.len = 0,temp.id = id[x1][y1];
    q.push(temp);
    while(!q.empty())
    {
        bbq now = q.front();
        q.pop();
        for (int i = 0; i < 4;i ++)
        {
            int xx = now.x + dir[i][0];
            int yy = now.y + dir[i][1];
            if(xx < 0 || xx >= n || yy < 0 || yy >= m)
                continue;
            if(!vis[xx][yy] && str[xx][yy] != '#')
            {
                bbq temp1;
                temp1.x = xx,temp1.y = yy,temp1.len = now.len+1;
                vis[xx][yy] = 1;
                q.push(temp1);
                if(str[xx][yy] == 'A' ||str[xx][yy] == 'S')
                {
                    e[tot].x = temp.id;
                    e[tot].y = id[xx][yy];
                    e[tot++].len = temp1.len;
                }
            }
        }
    }

}
int fin(int n)
{
    if(f[n] == n)
        return f[n];
    else return f[n] = fin(f[n]);
}
int merg(int u,int v)
{
    int t1 = fin(u);
    int t2 = fin(v);
    if(t1 != t2)
    {
        f[t1] = t2;
        return true;
    }
    return false;
}
bool cmp(bbq a,bbq b)
{
    return a.len < b.len;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        mount = 1,tot = 0;
        scanf("%d %d\n",&m,&n);
        memset(e,0,sizeof e);
        for (int i = 0; i < n; i++)
            gets(str[i]);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if(str[i][j] == 'A' || str[i][j] == 'S')
                {
                    id[i][j] = mount++;
                }
            }
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if(str[i][j] == 'A' || str[i][j] == 'S')
                {
                    bfs(i,j);
                }
            }
        }
        for (int i = 0; i < tot; i++)
            f[i] = i;
        sort(e,e+tot,cmp);
        int ans = 0;
        for (int i = 0; i < tot; i++)
        {
            if(merg(e[i].x,e[i].y))
            {
                ans += e[i].len;
            }
        }
        printf("%d\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值