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 ...
随机推荐
- 为 vsftpd 启动 vsftpd:500 OOPS: bad bool value in config file for: pasv_enable
每行的值都不要有空格,否则启动时会出现错误,举个例子,假如我在listen=YES后多了个空格,那我启动时就出现.. 为 vsftpd 启动 vsftpd:500 OOPS: bad bool val ...
- 【VBA研究】变量定义的类型和实际赋值类型
作者:iamlaosong VBA中变量能够先定义后使用,也能够不定义直接使用.假设模块前面加了Option Explicit语句,则变量必须先定义后使用. 只是.实验发现.VBA对变量类型没有进行严 ...
- PHP【第一篇】安装
一.准备 1.环境 系统平台:Red Hat Enterprise Linux Server release 7.3 (Maipo) 内核版本:3.10.0-514.el7.x86_64 2.下载安装 ...
- Windows下安装破解JIRA6.3.6
相关工具下载地址:http://pan.baidu.com/s/1kT9xZEJ 安装环境: WindowsXP MySQL-5.5.28 JDK1.6.0_21 JIRA功能全面,界面友好,安装简单 ...
- c结构体初始化问题
结构体中的数组的初始化问题 我的代码 C/C++ code #include <stdio.h> #include <stdlib.h> struct a { int leng ...
- 深入理解计算机系统第二版习题解答CSAPP 2.7
下面的函数将输出什么结果? const char *s = "abcdef"; show_bytes((byte_pointer) s, strlen(s)); 其中字母'a'~' ...
- Python模拟键盘输入和鼠标操作
Python模拟键盘输入和鼠标操作 一.Python键盘输入模拟: import win32api import win32con win32api.keybd_event(17,0,0,0) #c ...
- web开发学习之旅---html第一天
一.认识浏览器 浏览器就是接收浏览着的操作,然后帮浏览者去web服务器请求网页内容(html格式返回),然后展现成人眼能够看得懂的可视化页面的软件. IE==浏览器?IE是浏览器的一种,还有Firef ...
- Asp.Net静态资源动态压缩之WebOptimization
一.Asp.Net中对Css/Js的动态压缩工具 WebOptimization 在Asp.NetMVC自带的模板项目中自动引入了当前WebOptimization工具.如果使用的空模板Nuget命令 ...
- HttpRuntime.Cache被清空的DataTable
将一个DataTable存到Cache中后,另一个页面新建变量并获取,操作变量,Cache中的数据也被改动了? 页面a.aspx 初始化并赋值,输出当前缓存内DataTable内数据条数 Page_L ...