POJ 3026 Borg Maze bfs+Kruskal
题目链接: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的更多相关文章
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ - 3026 Borg Maze BFS加最小生成树
Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...
- poj 3026 Borg Maze (bfs + 最小生成树)
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...
- POJ - 3026 Borg Maze bfs+最小生成树。
http://poj.org/problem?id=3026 题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地.求从S出发,经过所有A所需要的最短路.你有一个特殊能力,当走到 ...
- poj 3026 Borg Maze bfs建图+最小生成树
题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16625 Accepted: 5383 Descri ...
随机推荐
- c#中cookies的存取操作
在客户端创建一个username的cookies,其值为gjy,有效期为1天. 方法1: Response.Cookies["username"].Value="zxf& ...
- autocommit=0
mysql; Query OK, rows affected (0.00 sec) mysql> create table test( a int); Query OK, rows affect ...
- How To Install Kernel 3.10 On Ubuntu, Linux Mint, Debian and Derivates
n this article I will show you how to install Linux Kernel 3.10 on Ubuntu 13.10 Saucy Salamander, Ub ...
- Objective-C语法快速参考(C# 和 Objective-C 语法的比较)
大部分有一点其他平台开发基础的初学者看到XCode ,第一感想是磨拳擦掌,看到 Interface Builder 之后,第一感想是跃跃欲试,而看到Objective-C 的语法,第一感想就变成就望 ...
- linux下安装apache2.4
linux安装Apache2步骤如下 apr 下载地址 http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.gz 安装过程 tar -xzvf apr- ...
- Java开发十大常用网站
Stackoverflow:有成千上万个好问题和答案 DZone:有相当多的开发者在这个网站上分享他们博客文章 LeetCode:如果有Java方面的面试问题可在教程中找到答案 Java SE技术文档 ...
- 鼠标移动到表格的TD上的时候显示成一个手型的样子怎么做?
在除了IE6的情况下,可以通过CSS的:hover伪类来实现: 假如你想设定的固定区域为: <div id="test"></div>,那么只需要在CSS样 ...
- CAS 单点登录,通过ticket 获取登录用户
string url =SSOValidate+"?service=" + WebValidate + "&ticket=" + Ticket + &q ...
- .Net 下FCKeditor上传图片加水印
配置FCKEditor请参考网上的. 如果你用的是.net的FCKEditor,把用到的FCKEditor.Net项目解压缩 在FCKEditor.net项目中,依次找到FileBrowser--&g ...
- HTML解析引擎:Jumony
Jumony Core首先提供了一个近乎完美的HTML解析引擎,其解析结果无限逼近浏览器的解析结果.不论是无结束标签的元素,可选结束标签的元素,或是标记属性,或是CSS选择器和样式,一切合法的,不合法 ...