UVA 572 油田连通块-并查集解决
题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块。
分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了。。。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#define ll long long
#define mem(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define maxn 1100
const double PI=-acos(-1.0);
using namespace std;
struct Node
{
int val, x, y;
bool operator < (const Node a)const
{
return val > a.val;
}
Node(int v=, int x=, int y=):val(v), x(x), y(y) {}
} nd[maxn*maxn];
struct Query
{
int h, id;
bool operator < (const Query a)const
{
return h>a.h;
}
Query(int h=, int id=):h(h), id(id) {}
} q[];
bool vis[maxn][maxn];
int n, m, pre[maxn*maxn];
const int dx[]= {, , -, , , , -, -};
const int dy[]= {, -, , , , -, , -};
int Find(int x)
{
if(pre[x] == -) return x;
return pre[x] = Find(pre[x]);
}
bool OK(int x, int y)
{
return x && y && x<=n && y<=m;
}
int judge(int x, int y)
{
int v[], cnt=;
for(int i=; i<; i++)///寻找他周围的四个数字是哪些集合的
{
int nx=x+dx[i], ny=y+dy[i];
if(OK(nx, ny) && vis[nx][ny])///已经遍历过的
{
int t = Find(m*(nx-)+ny);
v[cnt++] = t;
}
}
sort(v, v+cnt);
cnt = unique(v, v+cnt)-v;
for(int i=; i < cnt; i++)
pre[v[i]] = m*(x-)+y;///把筛选下来的全都归入(x,y)中
return - cnt;///返回的值也只有-1,0,1
}
int main()
{
char c[][];
int T, t, v;
while(~scanf("%d%d", &n, &m) ,n + m)
{
getchar();
int tot=;
repu(i,,n+)
gets(c[i]);
repu(i,,n+)
{
repu(j,,m)
{
if(c[i][j] == '*')
nd[tot]=Node(, i, j+);
else
nd[tot]=Node(, i, j+);
tot++;
}
}
///都是从大到小
sort(nd, nd+tot);
memset(vis, , sizeof vis);
memset(pre, -, sizeof pre);
int cnt=,j = ;
for(; nd[j].val > && j < tot; j++)
{
///如果这个点比所查询的点大,则可以加入其坐标
cnt += judge(nd[j].x, nd[j].y);
vis[nd[j].x][nd[j].y]=;
}
printf("%d\n",cnt);
}
return ;
}
/*
3 5
*@*@*
**@**
*@*@*
*/
借助的是UVA 1665 大神的模板
UVA 572 油田连通块-并查集解决的更多相关文章
- PAT天梯赛练习题——L3-004. 肿瘤诊断(三维连通块并查集)
L3-004. 肿瘤诊断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶 ...
- LightOJ——1012Guilty Prince(连通块并查集)
1012 - Guilty Prince Time Limit: 2 second(s) Memory Limit: 32 MB Once there was a king named Akbar. ...
- 牛客练习赛39 D 动态连通块+并查集 X bitset 优化
https://ac.nowcoder.com/acm/contest/368/D 题意 小T有n个点,每个点可能是黑色的,可能是白色的.小T对这张图的定义了白连通块和黑连通块:白连通块:图中一个点集 ...
- 《程序员代码面试指南》第三章 二叉树问题 Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题
题目待续.... Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题 java代码
- UVa 572 油田(DFS求连通块)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [洛谷Luogu]P1141 01迷宫[联通块 并查集]
题目链接 大致题意 相邻格子不同为连通,计算每个点所在的连通块大小. 想法 我采用了并查集的做法. 开一个辅助数组记录连通块大小,每次合并的时候更新父亲节点的大小即可. 一个点先与它上面的点判定,若判 ...
- UVA 12232 - Exclusive-OR(带权并查集)
UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...
- uva 1493 - Draw a Mess(并查集)
题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
随机推荐
- css中各种情况下的元素的垂直和水平居中的问题
问题:外边是一个容器,容器中还有一个容器,那么请问怎么让里边的容器垂直水平居中显示?? No1: 外边的容器宽度和高度确认,里边是行内元素 .container{width:200px; height ...
- WPF知识总结(一)
在一个项目中, 发现有的项目引用的动态库是一个网页地址,下面就看看这个网页地址怎么来的. 目标:新建一个WPF工程,实现一个用户控件的功能.在 工程中加入一个类库,然后在类库中增加一个用户控件页面,在 ...
- SQL Server数据库脚本备份与还原
use[master]go /*完整--备份* backup 备份 * disk='E:\USERDB.bak' 磁盘,备份文件存放的路径* name='user' 备份显示的文件名称,可随意修改* ...
- linux 使用sftp命令
1.使用SecureCRT软件进入sftp界面 2.常用的一些命令 服务器 本地 进入目录 cd lcd 查看目录结构 ...
- hdoj 5074
Problem Description Hatsune Miku is a popular virtual singer. It is very popular in both Japan and C ...
- 【 2013 Multi-University Training Contest 1 】
HDU 4602 Partition f[i]表示和为i的方案数.已知f[i]=2i-1. dp[i]表示和为i,k有多少个.那么dp[i]=dp[1]+dp[2]+...+dp[i-1]+f[i-k ...
- Android中的DrawerLayout
简介 Drawerlayout是google自带的控件,功能类似开源的SlidingMenu,在support-v4包下用来替代SlidingMenu(google好无耻啊). 使用方法 在activ ...
- 场景4 Data Warehouse Management 数据仓库
场景4 Data Warehouse Management 数据仓库 parallel 4 100% —> 必须获得指定的4个并行度,如果获得的进程个数小于设置的并行度个数,则操作失败 para ...
- Linux编程 ---- dup函数
dup,dup2,dup3函数 顾名思义,dup及duplicate的简写,也就是复制的意思.而事实上这几个函数的功能也确实是复制文件描述符.那为什么要复制文件描述符呢?呵呵,我认为是程序 ...
- HTML代码简写法:Emmet和Haml
http://www.ruanyifeng.com/blog/2013/06/emmet_and_haml.html?bsh_bid=657901854 HTML代码简写法:Emmet和Haml ...