题目链接:

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. PMP项目管理考试培训机构内部资料打包赠送(3个PPT)

    PMP认证考试我自己这边是今年6月份考过了的,手里觉得对自己有帮助的资料就是这3个PPT,讲解的比较清晰,知识点详细.结合自己做的笔记,备考十分轻松.所以推荐大家也看一下. 有需要的可以联系. PPT ...

  2. plink:ped格式转换为bed格式

    命令行如下: plink --file FILENAME --make-bed --out FILENAME 第一个FILENAME的后缀为.ped和.map,生成的第二个FILENAME的后缀为.b ...

  3. 【译】5. Java反射——方法

    原文地址:http://tutorials.jenkov.com/java-reflection/methods.html ====================================== ...

  4. 【非专业前端】Vue UI 之 建立Vuetify工程

    先建立一个工程[Webpack] .. ..建立好之后,进入目录,添加vuetify插件 cd vuetify-demo vue add vuetify[会出错] npm install vuetif ...

  5. Contest1593 - 2018-2019赛季多校联合新生训练赛第三场(部分题解)

    H 10255 自然数无序拆分 H 传送门 题干: 题目描述 美羊羊给喜羊羊和沸羊羊出了一道难题,说谁能先做出来,我就奖励给他我自己做的一样礼物.沸羊羊这下可乐了,于是马上答应立刻做出来,喜羊羊见状, ...

  6. Game1---游戏设计

    自己玩的一些游戏简单策划 先设计3个类似的游戏场景,第一个场景只进行时间限制,第二个场景道具进行上下移动,第三个场景随机生成敌人: 1.上面的台阶道具应该是随着人物的高度上升逐渐生成,逐渐呈现在玩家的 ...

  7. Going Home POJ - 2195 (最小费用最大流)

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

  8. Mac idea中git igenore设置

  9. 数据库MySQL

    --IN 关键字 在.....里 SELECT * FROM zhangwu WHERE money IN (66,666,700); 1.主键约束 特点非空 只用于表示當前的记录 primary k ...

  10. codeforces794D dfs+图上hash

    http://codeforces.com/problemset/problem/794/D 题意:在一个国家有 n 座城市和一些双向边.这些城市被编号为 1 到 n. 一共有 m 条双线边,第 i条 ...