poj3206(bfs+最小生成树)

传送门:Borg Maze

题意:有一个迷宫,里面有一些外星人,外星人用字母A表示,#表示墙,不能走,空格可以走,从S点出发,在起点S和A处可以分叉走,问找到所有的外星人的最短路径是多少?

分析:分别bfs由S和所有A出发到其他点的距离,然后建好图进行最小生成树处理即可。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 110
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct edge
{
    int u,v,w;
    edge() {}
    edge(int u,int v,int w):u(u),v(v),w(w){}
    bool operator<(const edge &a)const
    {
        return w<a.w;
    }
} e[N*N];
struct node
{
    int x,y,step;
    node(){}
    node(int x,int y,int step):x(x),y(y),step(step){}
};
int fa[N],tot,total;
char str[N][N];
int num[N][N],vis[N][N],n,m;
int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
int MST(int n)
{
    int ans=0;
    for(int i=1;i<=n;i++)fa[i]=i;
    sort(e,e+tot);
    for(int i=0; i<tot; i++)
    {
        int a=find(e[i].u);
        int b=find(e[i].v);
        if(a==b)continue;
        fa[a]=b;
        ans+=e[i].w;
    }
    return ans;
}
bool judge(int a,int b)
{
    return a>=1&&a<=n&&b>=1&&b<=m&&str[a][b]!=‘#‘&&!vis[a][b];
}
void bfs(int x,int y)
{
    queue<node>que;
    while(!que.empty())que.pop();
    FILL(vis,0);
    vis[x][y]=1;
    que.push(node(x,y,0));
    while(!que.empty())
    {
        node now=que.front();que.pop();
        for(int i=-1;i<=1;i++)
        for(int j=-1;j<=1;j++)
        {
            if(i+j==0||i==j)continue;
            int a=now.x+i,b=now.y+j,step=now.step+1;
            if(judge(a,b))
            {
                if(str[a][b]==‘A‘||str[a][b]==‘S‘)
                {
                    e[tot++]=edge(num[x][y],num[a][b],step);
                }
                vis[a][b]=1;
                que.push(node(a,b,step));
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        gets(str[0]);
        for(int i=1;i<=n;i++)
            gets(str[i]);
        total=0;tot=0;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            if(str[i][j]==‘A‘||str[i][j]==‘S‘)
            {
                num[i][j]=++total;
            }
        }
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            if(str[i][j]==‘A‘||str[i][j]==‘S‘)
            {
                bfs(i,j);
            }
        }
        printf("%d\n",MST(total));
    }
}

时间: 2024-12-29 11:32:06

poj3206(bfs+最小生成树)的相关文章

【bzoj4242】水壶 BFS+最小生成树+倍增LCA

题目描述 JOI君所居住的IOI市以一年四季都十分炎热著称. IOI市是一个被分成纵H*横W块区域的长方形,每个区域都是建筑物.原野.墙壁之一.建筑物的区域有P个,编号为1...P. JOI君只能进入建筑物与原野,而且每次只能走到相邻的区域中,且不能移动到市外. JOI君因为各种各样的事情,必须在各个建筑物之间往返.虽然建筑物中的冷气设备非常好,但原野上的日光十分强烈,因此在原野上每走过一个区域都需要1单位的水.此外,原野上没有诸如自动售货机.饮水处之类的东西,因此IOI市的市民一般都携带水壶出

poj 3026 Borg Maze (bfs + 最小生成树)

链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走),空格代表空位(可走),S代表搜索起点(可走) A代表外星人站(可走),现在要从S出发,将S和所有的A之间都连通,求路线总距离最小值 分析:可以先用bfs将所有的A,S两两之间的最短距离,题目的目的是将S与所有的A连通,使得总距离最小, 所有任选一点开始按最小生成树的算法做就行,并非非要从S点开始 注:题目输入x,y后可能有很多空格,可以用gets将多余的空格取走,开数组是尽量开大点,之前虽然开的比题目数据     稍大,但一

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

Borg Maze DescriptionThe 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

POJ 3026 Borg Maze &amp; UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)

http://poj.org/problem?id=3026 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1248 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8498   Accepted: 2862 Description The Bor

POJ Borg Maze (BFS+最小生成树)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10428   Accepted: 3463 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 desc

POJ 3026 Borg Maze【BFS+最小生成树MST】

Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12014 Accepted: 3925 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

poj 3026(BFS+最小生成树)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12032   Accepted: 3932 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 desc

【练习赛补题】poj 3026 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 c

POJ - 3026(BFS+最小生成树.krustal)

题目: 题目链接: http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17462   Accepted: 5631 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The