在一个迷宫里面需要把一些字母。也就是 ‘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. EventBus 事件总线 原理

    原理 一句话描述:register会把当前类中匹配的方法,存入一个map,而post会根据实参去map查找进行反射调用 撇开专业术语,其实EventBus就是在内部[存储]了一堆onEvent开头的方 ...

  2. 二分图最大匹配(匈牙利算法Dfs模板)

    #include<iostream> #include<cstdio> #include<cstring> #define maxn 2020 using name ...

  3. VBA取得EXCEL表格中的行数和列数

    VBA取得EXCEL表格中的行数和列数 初学EXCEL宏的童鞋,总是很想知道表格中含有数据的行数和列数,尤其是行数和列数不确定的情况下.这样可以避免很多的错误,并且可以提高效率.但每次用到的时候到网上 ...

  4. 关于timestamp的二三事

    之所以要写timestamp的随笔,是因为之前对它的理解存在误区,so. I have to remind myself by writing this informal essay. 微软文档链接: ...

  5. 开源的Android开发框架-------PowerFramework使用心得(二)图片异步加载ImageTask

    图片异步加载.可以备注图片是否缓存.缓存状态. 1.缓存-SD卡,路径可设置 2.图片压缩 3.可加载本地和网络图片 4.url为本地视频文件可以显示缩略图 5.中文url图片地址FileNotFou ...

  6. Oracle存储过程及函数

    1.在Oracle中,存储过程包括三部分组成:定义部分.执行部分.和异常处理部分(即例外) eg1:输入员工编号,查询员工的姓名和薪资 create or repalce  procedure myp ...

  7. XCode的一些调试技巧

    XCode 内置GDB,我们可以在命令行中使用 GDB 命令来调试我们的程序.下面将介绍一些常用的命令以及调试技巧. po 命令:为 print object 的缩写,显示对象的文本描述(显示从对象的 ...

  8. CSS 布局Float 【1】

    1.HTML元素分类 HTML元素大题可分为内联(inline)元素和块(block)元素. 1.1 内联元素(inline) ①元素显示方式:"文本方式",1个挨着1个,不独自占 ...

  9. 你好,C++(10)这次的C++考试你过了没有?C++中表示逻辑判断的布尔数据类型

    3.4  布尔类型 在日常生活中,我们除了需要使用int类型的变量表示216路公交车:需要使用float类型的变量表示西红柿3.5元一斤,有时候还需要表示一种数据,那就是逻辑状态: “这次的C++考试 ...

  10. jquery之bind(),live(),delegate()

    大纲: 1.bind(),live(),delegate()的含义 2.三者基于相同的原理即js的事件冒泡 3.三者相互之间的异同. bind()+live() V.S. delegate():bin ...