Borg Maze-POJ3026(bfs+最小生成树)

http://poj.org/problem?id=3026

如果一个一个普通搜处理不好的话会超时  可以连到一块搜

我觉得这个方法特别好

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<queue>
#include<algorithm>
#define N 105
#define INF 0x3f3f3f3f
using namespace std;
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int G[N][N],d[N],vis[N][N],m,n,v[N],b[N][N];
char F[N][N];
struct node
{
    int x,y,temp;
}a;

void Inn()
{
    int i,j;
    for(i=0;i<=N;i++)
    {
        d[i]=INF;
        for(j=1;j<=N;j++)
            G[i][j]=INF;
    }
    memset(b,0,sizeof(b));
    memset(F,0,sizeof(F));
}

void bfs(int s,int x,int y)
{
    int i;
    memset(vis,0,sizeof(vis));
    queue<node>Q;
    a.x=x;
    a.y=y;
    a.temp=0;
    Q.push(a);
    vis[a.x][a.y]=1;
    while(Q.size())
    {
        node q,p;
        q=Q.front();
        Q.pop();
        if(F[q.x][q.y]==‘A‘ || F[q.x][q.y]==‘S‘)
            G[s][b[q.x][q.y]]=q.temp;
        for(i=0;i<4;i++)
        {
            p.x=q.x+dis[i][0];
            p.y=q.y+dis[i][1];

            if(F[p.x][p.y]!=‘#‘ && p.x<m&&p.y<n&&p.x>=0&&p.y>=0&&!vis[p.x][p.y])
            {
                p.temp=q.temp+1;
                Q.push(p);
                vis[p.x][p.y]=1;
            }
        }
    }
}

int prim(int k)
{
    memset(v,0,sizeof(v));
    int i,j,ans=0;
    for(i=1;i<=k;i++)
    {
        d[i]=G[1][i];
    }
    v[1]=1;
    for(i=1;i<k;i++)
    {
        int Min=INF;
        int dist;
        for(j=1;j<=k;j++)
        {
            if(!v[j] && d[j]<Min)
            {
                Min=d[j];
                dist=j;
            }
        }
        v[dist]=1;
        ans+=Min;
        for(j=1;j<=k;j++)
        {
            if(!v[j] && d[j]>G[dist][j])
                d[j]=G[dist][j];
        }
    }
    return ans;
}

int main()
{
    int T,i,j;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d ",&n,&m);
        Inn();
        int k=1;
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%c",&F[i][j]);
                if(F[i][j]==‘A‘ || F[i][j]==‘S‘)
                {
                    b[i][j]=k;
                    k++;
                }
            }
            getchar();
        }
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                if(F[i][j]==‘A‘ || F[i][j]==‘S‘)
                    bfs(b[i][j],i,j);
            }
        }
        printf("%d\n",prim(k-1));
    }
    return 0;
}
时间: 2024-10-22 04:44:12

Borg Maze-POJ3026(bfs+最小生成树)的相关文章

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 Borg Maze(BFS+最小生成树【有坑】)

Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12012 Accepted: 3924 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 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 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

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

POJ3026 Borg Maze(Prim)(BFS)

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

Borg Maze(MST &amp; bfs)

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

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 (bfs + 最小生成树)

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

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 &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