题意: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 油田连通块-并查集解决的更多相关文章

  1. PAT天梯赛练习题——L3-004. 肿瘤诊断(三维连通块并查集)

    L3-004. 肿瘤诊断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶 ...

  2. LightOJ——1012Guilty Prince(连通块并查集)

    1012 - Guilty Prince Time Limit: 2 second(s) Memory Limit: 32 MB Once there was a king named Akbar. ...

  3. 牛客练习赛39 D 动态连通块+并查集 X bitset 优化

    https://ac.nowcoder.com/acm/contest/368/D 题意 小T有n个点,每个点可能是黑色的,可能是白色的.小T对这张图的定义了白连通块和黑连通块:白连通块:图中一个点集 ...

  4. 《程序员代码面试指南》第三章 二叉树问题 Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题

    题目待续.... Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题 java代码

  5. UVa 572 油田(DFS求连通块)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. [洛谷Luogu]P1141 01迷宫[联通块 并查集]

    题目链接 大致题意 相邻格子不同为连通,计算每个点所在的连通块大小. 想法 我采用了并查集的做法. 开一个辅助数组记录连通块大小,每次合并的时候更新父亲节点的大小即可. 一个点先与它上面的点判定,若判 ...

  7. 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 ...

  8. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  9. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

随机推荐

  1. 递归算法(三)——多项分布问题

    问题 一个枪手打靶,靶从外向内有1-10分(考虑脱靶得0分),打十枪,求枪手最后得90分的概率. 解决思路 一.穷举 考虑10重for循环,累计十次得分和为90者,求得概率.该法通用性差,效率低,但容 ...

  2. css学习归纳总结(一) 转

    原文地址:CSS学习归纳总结(一) 选择器的分组 CSS选择器分为 1.群组选择器 如:p, body, img, div{} 2.兄弟选择器 如:p + p { color:#f00; } 3.属性 ...

  3. (转)Tomcat数据源连接池加密

    文章来源 :http://my.oschina.net/cimu/blog/164757 我们在使用Tomcat数据库连接池的时候都是明文存储数据库用户名和密码的,例如: <Resource n ...

  4. Leetcode 详解(valid plindrome)

    Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...

  5. centos 6安装epel

    1.通过:https://mirrors.ustc.edu.cn/找到epel rpm包链接,这里的是 https://mirrors.ustc.edu.cn/epel/epel-release-la ...

  6. C++与C的关系

     C是结构化和模块化的语言,是基于过程的.        C++是面向对象的程序设计语言.        C++是C的超集,对C的功能做了扩充,增加了面向对象的机制.        C++包含头文件的 ...

  7. iOS开发_MVC设计模式

    MVC,是一种主流的设计模式,本博文总结一下自己对MVC的看法和理解,浅知拙见,难登大雅之堂,如有欠缺遗漏,还望各位指正. MVC是Model-View-Controller 的缩写,Model 是指 ...

  8. Bootstrap <基础二十一>徽章(Badges)

    Bootstrap 徽章(Badges).徽章与标签相似,主要的区别在于徽章的边角更加圆滑. 徽章(Badges)主要用于突出显示新的或未读的项.如需使用徽章,只需要把 <span class= ...

  9. C# ADO.NET 连接Sybase 数据库

    using Sybase.Data.AseClient;//反编译修改后的DLL public class SybaseHelper { public AseConnection con; publi ...

  10. 配置 vim

    cd / vim /etc/vim/vimrc 1.Ubuntu vim显示行号 在文件末端添加一新行,输入 set nu 2.Ubuntu vim语法高亮 在文件中找到 "syntax o ...