在一个迷宫里面需要把一些字母。也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离,
**********************************************************************************
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int maxn = ;
const int oo = 0xfffffff; int  dir[][] = { {,},{,},{-,},{,-} };
char G[maxn][maxn];          //保存地图
int  D[maxn][maxn];          //记录两点间的距离
int  use[maxn][maxn];        //标记地图
int  Index[maxn][maxn];      //记录‘A’或者‘B’的编号
struct node{int x, y, step;}; void BFS(int k, int M,int N, int x, int y)
{
    queue<node> Q;
    node s;
    s.x = x, s.y = y, s.step = ;
    use[s.x][s.y] = k; Q.push(s);     while(Q.size())
    {
        s = Q.front();Q.pop();
        if(G[s.x][s.y]>='A' && G[s.x][s.y] <='Z')
            D[k][ Index[s.x][s.y] ] = s.step;         for(int i=; i<; i++)
        {
            node q = s;
            q.x += dir[i][], q.y += dir[i][];             if(q.x>=&&q.x<M && q.y>=&&q.y<N && G[q.x][q.y]!='#' && use[q.x][q.y]!=k)
            {
                use[q.x][q.y] = k;
                q.step += ;
                Q.push(q);
            }
        }
    }
}
int  Prim(int N)            //这里面的N代表编号最多到N
{
    int i, dist[maxn], vis[maxn]={, };
    int ans = , T=N-;     for(i=; i<=N; i++)
        dist[i] = D[][i];     while(T--)
    {
        int k=, mini = oo;         for(i=; i<=N; i++)
        {
            if(!vis[i] && mini > dist[i])
                mini = dist[i], k=i;
        }
        ans += mini;
        vis[k] = true;         for(i=; i<=N; i++)
            if(!vis[i])dist[i] = min(dist[i], D[k][i]);
    }     return ans;
} int main()
{
    int T;     scanf("%d", &T);     while(T--)
    {
        int i, j, M, N, t=;         scanf("%d%d ", &N, &M);         for(i=; i<M; i++)
        {
            gets(G[i]);
            for(j=; j<N; j++)
            {
                if(G[i][j]>='A' && G[i][j]<='Z')
                    Index[i][j] = t++;
                use[i][j] = ;
            }
        }         for(i=; i<M; i++)
        for(j=; j<=N; j++)
        {
            if(G[i][j]>='A' && G[i][j]<='Z')
                BFS(Index[i][j], M, N, i, j);
        }         int ans = Prim(t-);         printf("%d\n", ans);
    }     return ;
}

J - Borg Maze - poj 3026(BFS+prim)的更多相关文章

  1. POJ 3026(BFS+prim)

    http://poj.org/problem?id=3026 题意:任意两个字母可以连线,求把所有字母串联起来和最小. 很明显这就是一个最小生成树,不过这个题有毒.他的输入有问题.在输入m和N后面,可 ...

  2. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  3. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

  4. (最小生成树) Borg Maze -- POJ -- 3026

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  5. Borg Maze - poj 3026(BFS + Kruskal 算法)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 Description The B ...

  6. Borg Maze POJ - 3026 (BFS + 最小生成树)

    题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...

  7. Borg Maze poj 3026

    Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...

  8. poj3026(bfs+prim)

    The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...

  9. poj3026(bfs+prim)最小生成树

    The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...

随机推荐

  1. codevs 1183 泥泞的道路 (二分+SPFA+差分约束)

    /* 二分答案(注意精度) 对于每一个答案 有(s1+s2+s3...)/(t1+t2+t3...)>=ans 时符合条件 这时ans有变大的空间 对于上述不等式如果枚举每一条路显得太暴力 化简 ...

  2. C#解leetcode 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  3. springmvc学习笔记(理论)

    1.springmvc是什么? Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层 进行职责解耦,基 ...

  4. ADO.NET和ORACLE操作数据库传参数赋值的方式

    在使用.Net使用OracleParameter进行Oracle数据库操作的时候,因为Oracle和SQLServer针对查询参数化的语法不同, 在操作SQLServer的时候使用的是@Paramet ...

  5. input输入过滤js

    html部分使用方式 <input  onkeyup="usrNameSet(this)" /> 其它的自己可以随便调用 Js部分 //只能输入数字.字母.小数点.汉字 ...

  6. phonegap 2.8.1 toast

    目录结构如下: 以上三个用红色框勾出的地方是需要修改的文件夹. 首先:添加java代码. 在src目录下新建一个包裹:org.apache.cordova 在该包裹下新建类:ToastPlugin.j ...

  7. 【转载】ASP.NET线程安全与静态变量的生命周期浅谈

    ASP.NET线程安全所涉及的是什么呢?让我们先来看看静态变量的生命周期问题,下面是我理解的静态变量的生命周期: void Application_Start开始 void Application_E ...

  8. UIView -> image & 本地时间获取

    //UIView 转换为图片 UIGraphicsBeginImageContext(self.rootsView.bounds.size); [_rootsView.layer renderInCo ...

  9. 【USACO 2.3.3】零数列

    [题目描述] 请考虑一个由1到N(N=3, 4, 5 ... 9)的数字组成的递增数列:1 2 3 ... N. 现在请在数列中插入“+”表示加,或者“-”表示减,“ ”表示空白(例如1-2 3就等于 ...

  10. 优秀的弹窗插件 jquery.lightbox_me.js

    项目地址: https://github.com/buckwilson/Lightbox_me用法:http://buckwilson.me/lightboxme/ var opt = { 'cent ...