题目链接:

D - Coconuts

 HDU - 5925

题目大意:首先是T组测试样例,然后给你n*m的矩阵,原先矩阵里面都是白色的点,然后再输入k个黑色的点。这k个黑色的点可能会使得原先白色的点分成多个联通块,然后问你白色联通块的个数和每一个连通块里面白色的点的个数。

具体思路:输入的点坐标比较大,但是个数比较小,所以离散化坐标就可以了。标记每一个整块里面的白色的点的个数就可以了。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 2e5+;
const int maxm =+;
vector<ll>contx;
vector<ll>conty;// 注意开long long
int vis[maxm<<][maxm<<];
ll a[maxm<<],cnt;
ll Map[maxm<<][maxm<<];
int f[][]= {{,-,,},{,,,-}};
struct node
{
int x,y;
node() {}
node(int xx,int yy)
{
x=xx;
y=yy;
}
} q[maxn];
bool judge(int x,int y)
{
if(x>=&&y>=&&x<=contx.size()&&y<=conty.size())
return true;
return false;
}
void dfs(int x,int y)
{
cnt+=Map[x][y];
vis[x][y]=;
for(int i=; i<; i++)
{
int tx=x+f[][i];
int ty=y+f[][i];
if(judge(tx,ty)&&vis[tx][ty]==)
dfs(tx,ty);
}
}
int main()
{
int T,Case=;
scanf("%d",&T);
while(T--)
{
int n,m,tot;
scanf("%d %d",&n,&m);
memset(vis,,sizeof(vis));
memset(Map,,sizeof(Map));
contx.clear();
conty.clear();
scanf("%d",&tot);
contx.push_back();
conty.push_back();
contx.push_back(n);
conty.push_back(m); /// 先把四个角输进去
for(int i=; i<=tot; i++)
{
scanf("%d %d",&q[i].x,&q[i].y);
contx.push_back(q[i].x);
conty.push_back(q[i].y);
if(q[i].x->=)
contx.push_back(q[i].x-);
if(q[i].x+<=n)
contx.push_back(q[i].x+);
if(q[i].y->=)
conty.push_back(q[i].y-);
if(q[i].y+<=m)
conty.push_back(q[i].y+);
}
sort(contx.begin(),contx.end());
sort(conty.begin(),conty.end());
contx.erase(unique(contx.begin(),contx.end()),contx.end());
conty.erase(unique(conty.begin(),conty.end()),conty.end());
for(int i=; i<=tot; i++)
{
int t1=lower_bound(contx.begin(),contx.end(),q[i].x)-contx.begin()+;
int t2=lower_bound(conty.begin(),conty.end(),q[i].y)-conty.begin()+;
vis[t1][t2]=;
}
for(int i=; i<contx.size(); i++)
{
for(int j=; j<conty.size(); j++)
{
if(i>&&j>)
Map[i+][j+]=(contx[i]-contx[i-])*(conty[j]-conty[j-]);
else if(i>)
Map[i+][j+]=(contx[i]-contx[i-])*conty[j];
else if(j>)
Map[i+][j+]=contx[i]*(conty[j]-conty[j-]);
else
Map[i+][j+]=contx[i]*conty[j];
}
}
int num=;
for(int i=; i<=contx.size(); i++)
{
for(int j=; j<=conty.size(); j++)
{
if(vis[i][j])continue;
cnt=;
dfs(i,j);
a[++num]=cnt;
}
}
sort(a+,a+num+);
printf("Case #%d:\n",++Case);
printf("%d\n",num);
for(int i=; i<=num; i++)
{
if(i==)
printf("%lld",a[i]);
else
printf(" %lld",a[i]);
}
printf("\n");
}
return ;
}

 

Coconuts HDU - 5925 (二维离散化求连通块的个数以及大小)的更多相关文章

  1. Coconuts HDU - 5925 二维离散化 自闭了

    TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, ...

  2. hdu 3072 Intelligence System(Tarjan 求连通块间最小值)

    Intelligence System Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  3. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

  4. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

  5. HDU1241 Oil Deposits —— DFS求连通块

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

  6. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  7. UVA 572 dfs求连通块

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  8. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

随机推荐

  1. Proxy代理模式

    https://www.cnblogs.com/vincentzh/p/5988145.html https://www.cnblogs.com/wrbxdj/p/5267370.html(不错)

  2. Mac idea中git igenore设置

  3. 完美解决windows+ngnix+phpcgi自动退出的问题

    [摘要]在windows下搭建nginx+php环境时,php-cgi.exe会经常性的自动关闭退出,本文介绍通过使用xxfpm进程管理器管理php-cgi.exe. php-cgi.exe在wind ...

  4. Python之字符编码与文件操作

    目录 字符编码 Python2和Python3中字符串类型的差别 文件操作 文件操作的方式 文件内光标的移动 文件修改 字符编码 什么是字符编码? ''' 字符编码就是制定的一个将人类的语言的字符与二 ...

  5. Linux系统centos6.7上安装libevent

    1 下载地址:http://libevent.org/ 2.解压 tar zxvf libevent-2.0.21-stable.tar.gz 安装前请先安装 gcc yum install gcc ...

  6. MySQL数据库优化_limit_1

    转自:https://blog.csdn.net/cbjcry/article/details/70155118 1. MySQL中,在某些情况下,如果明知道查询结果只有一个,SQL语句中使用LIMI ...

  7. Vue项目搭建

    1.环境搭建 安装node 官网下载安装包,傻瓜式安装:https://nodejs.org/zh-cn/ 安装cnpm npm install -g cnpm --registry=https:// ...

  8. DirectX11 With Windows SDK--03 索引缓冲区、常量缓冲区

    前言 一个立方体有8个顶点,然而绘制一个立方体需要画12个三角形,如果按照前面的方法绘制的话,则需要提供36个顶点,而且这里面的顶点数据会重复4次甚至5次.这样的绘制方法会占用大量的内存空间. 接下来 ...

  9. 归并排序_JAVA

    import java.util.Arrays; public class Main { public static void main(String[] args) { int[] a = { 6, ...

  10. 解决浏览器跨域限制方案之CORS

    一.什么是CORS CORS是解决浏览器跨域限制的W3C标准,详见:https://www.w3.org/TR/cors/. 根据CORS标准的定义,在浏览器中访问跨域资源时,需要做如下实现: 服务端 ...