题目链接:http://poj.org/problem?id=3026

感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值。

 #include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std; char maze[][];
int par[];
struct Point
{
int x, y;
}point[]; struct Edge
{
int u, v, w;
bool operator<(const struct Edge &b)const
{
return w < b.w;
}
}edge[]; struct node
{
int x, y, step;
}; int find_set(int x)
{
return x == par[x] ? x : par[x] = find_set(par[x]);
} queue<struct node>q;
bool vis[][];
int bfs(int x, int y, int ex, int ey)
{
while(!q.empty())q.pop();
memset(vis, , sizeof(vis));
int dir[][] = {{, }, {, -}, {-, }, {, }};
q.push((struct node){x, y, });
vis[x][y] = ;
while(!q.empty())
{
struct node u = q.front();
q.pop();
if(u.x == ex && u.y == ey)
return u.step;
for(int i = ; i < ; i++)
{
if(!vis[u.x+dir[i][]][u.y+dir[i][]] && maze[u.x+dir[i][]][u.y+dir[i][]] != '#')
{
vis[u.x+dir[i][]][u.y+dir[i][]] = ;
q.push((struct node){u.x+dir[i][], u.y+dir[i][], u.step+});
}
}
}
} int main()
{
int t, n, m;
char fuck_space[];
scanf("%d%*c", &t);
while(t--)
{
gets(fuck_space);
sscanf(fuck_space, "%d %d", &m, &n);
int point_rear = ;
for(int i = ; i < n; i++)
{
gets(maze[i]);
for(int j = ; j < m; j++)
if(maze[i][j] == 'S' || maze[i][j] == 'A')
point[point_rear++] = (struct Point){i, j};
}
int edge_rear = ;
for(int i = ; i < point_rear; i++)
{
for(int j = i+; j < point_rear; j++)
{
int w = bfs(point[i].x, point[i].y, point[j].x, point[j].y);
edge[edge_rear++] = (struct Edge){i, j, w};
}
}
int ans = ;
for(int i = ; i < point_rear; i++)
par[i] = i;
sort(edge, edge+edge_rear);
for(int i = ; i < edge_rear; i++)
{
int x = find_set(edge[i].u);
int y = find_set(edge[i].v);
if(x != y)
{
ans += edge[i].w;
par[x] = y;
}
}
printf("%d\n", ans);
}
return ;
}

POJ 3026 Borg Maze bfs+Kruskal的更多相关文章

  1. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  2. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

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

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

  4. POJ - 3026 Borg Maze bfs+最小生成树。

    http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...

  5. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

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

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

  7. POJ 3026 Borg Maze(bfs+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6634   Accepted: 2240 Descrip ...

  8. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

  9. POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16625   Accepted: 5383 Descri ...

随机推荐

  1. Ubuntu设置目录的读写权限(Linux命令chmod 777 dirName)

    更改文件所有者 sudo chown system_username /location_of_files_or_folders 更改文件的权限 鼠标右按钮点击文件/目录 -> 属性 权限 分页 ...

  2. mysql优化之索引优化

    Posted by Money Talks on 2012/02/23 | 第一篇 序章第二篇 连接优化第三篇 索引优化第四篇 查询优化第五篇 到实战中去 索引优化 索引优化涉及到几个方面,包括了索引 ...

  3. 关于Xcode7中的tbd文件

    tbd 是 text-based stub libraries的意思, 是苹果在Xcode7中使用的一个技术,便于减少Xcode7中SDK的体积. 下面讲解下Xcode7如何通过tbd这个技术减少SD ...

  4. 读取group by 之外的字段

    序号 姓名 性别 身高 1 张三 男  185 2 李四 女  161 3 王五 女  166 4 赵六 男  178 1.获取男生女生人数 select count(性别) , 性别 from 表名 ...

  5. 【转】SharePoint 中实现ReportView

    微软的Visual studio提供了ReportViewer控件以及RDLC报表设计工具.下文主要介绍如何在Sharepoint 2010项目开发中使用ReportViewer和RDLC生成项目报表 ...

  6. Magento模型和ORM基础

    对于任何一个MVC架构,模型(Model)层的实现都是占据了很大一部分.对于Magento来说,模型占据了一个更加重要的位置,因为它常常包含了一部分商业逻辑代码(可以说它对,也可以说它错).这些代码在 ...

  7. jqery筛选

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 20151211jquery ajax进阶代码备份

    //数据处理 $('form input[type=button]').click(function() { //json处理 /*$.ajax({ type:'POST', url:'test.js ...

  9. 向SQL2008R2导入Acess、excel数据

    一:导入Access数据 1.在sql2008查询分析 器中输入如下查询语句能查出access中的数据 SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLE ...

  10. gdal中文路径无法打开问题

    在C#中使用OGR读写矢量数据时,需要引用“using OSGeo.OGR;”. 同时为了处理中文路径和中文字段,需要在开始设置下面两个属性,代码如下: //为了支持中文路径,请添加下面这句代码(大多 ...