题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5925

Coconuts

Time Limit: 9000/4500 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#### 问题描述
> TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, TanBig dreams of a field of coconuts, and the field looks like a large chessboard which has R rows and C columns. In every cell of the field, there is one coconut. Unfortunately, some of the coconuts have gone bad. For sake of his health, TanBig will eat the coconuts following the rule that he can only eat good coconuts and can only eat a connected component of good coconuts one time(you can consider the bad coconuts as barriers, and the good coconuts are 4-connected, which means one coconut in cell (x, y) is connected to (x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1).
>
> Now TanBig wants to know how many times he needs to eat all the good coconuts in the field, and how many coconuts he would eat each time(the area of each 4-connected component).
#### 输入
> The first line contains apositiveinteger T(T≤10) which denotes the test cases. T test cases begin from the second line. In every test case, the first line contains two integers R and C, 0
> It is guaranteed that in the input data, the first row and the last row will not have bad coconuts at the same time, the first column and the last column will not have bad coconuts at the same time.

输出

For each test case, output "Case #x:" in the first line, where x denotes the number of test case, one integer k in the second line, denoting the number of times TanBig needs, in the third line, k integers denoting the number of coconuts he would eat each time, you should output them in increasing order.

样例输入

2

3 3

2

1 2

2 1

3 3

1

2 2

样例输出

Case #1:

2

1 6

Case #2:

1

8

题意

给你一个n*m的网格,问障碍物把网格分割成多少个连通块,按从大到小的顺序输出每个连通块的大小。

题解

由于障碍就200多个,我们对障碍物离散化下,因为离散化不会影响被障碍物包围起来的连通块,所以离散化之后我们只统计被包围的那些(一个点事障碍物,要把它周围的点也离散化,否则会出现本来没被围起来的被围了),然后最后再算出外围的大块。

注意:在边缘的被分割开的连通块需要特殊处理下,这需要用到题目里面的一个提示:“the first row and the last row will not have bad coconuts at the same time, the first column and the last column will not have bad coconuts at the same time.”。我们标记下四周的状态,就可以判断我们统计的块是不是被夹在角落,还是离散化之后伪的被夹在角落。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=222; int R,C,n,nn,mm; PII pt[maxn];
int vis[maxn][maxn]; const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1}; bool _flag[4];
LL bfs(int xs,int ys) {
queue<PII> Q;
LL res=1;
vis[xs][ys]=1;
Q.push(mkp(xs,ys));
int flag=0;
while(!Q.empty()) {
PII u=Q.front();
Q.pop();
int x=u.X,y=u.Y;
for(int i=0; i<4; i++) {
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<1||nx>nn||ny<1||ny>mm) {
if(nx<1&&_flag[0]==0) flag=1;
if(nx>nn&&_flag[1]==0) flag=1;
if(ny<1&&_flag[2]==0) flag=1;
if(ny>mm&&_flag[3]==0) flag=1;
continue;
}
if(!vis[nx][ny]) {
vis[nx][ny]=1;
res++;
Q.push(mkp(nx,ny));
}
}
}
if(flag) return 0;
return res;
} int main() {
int tc,kase=0;
scf("%d",&tc);
while(tc--) {
scf("%d%d%d",&R,&C,&n);
VI ha_x,ha_y;
clr(_flag,0);
rep(i,0,n) {
scf("%d%d",&pt[i].X,&pt[i].Y);
//标记四周的状态,用于判断被围在边缘的情况
if(pt[i].X==1) _flag[0]=1;
if(pt[i].X==R) _flag[1]=1;
if(pt[i].Y==1) _flag[2]=1;
if(pt[i].Y==C) _flag[3]=1; //离散化
if(pt[i].X-1>=1) ha_x.pb(pt[i].X-1);
ha_x.pb(pt[i].X);
if(pt[i].X+1<=R) ha_x.pb(pt[i].X+1); if(pt[i].Y-1>=1) ha_y.pb(pt[i].Y-1);
ha_y.pb(pt[i].Y);
if(pt[i].Y+1<=C) ha_y.pb(pt[i].Y+1);
} //离散化
sort(all(ha_x));
ha_x.erase(unique(all(ha_x)),ha_x.end());
sort(all(ha_y));
ha_y.erase(unique(all(ha_y)),ha_y.end()); nn=ha_x.sz();
mm=ha_y.sz(); //外围的那个联通块
LL Ma=(LL)R*C-n;
vector<LL> ans; //离散化
clr(vis,0);
rep(i,0,n) {
pt[i].X=lower_bound(all(ha_x),pt[i].X)-ha_x.begin()+1;
pt[i].Y=lower_bound(all(ha_y),pt[i].Y)-ha_y.begin()+1;
vis[pt[i].X][pt[i].Y]=1;
} prf("Case #%d:\n",++kase);
LL sum=0;
//对于离散化的图暴力bfs
for(int i=1; i<=nn; i++) {
for(int j=1; j<=mm; j++) {
if(!vis[i][j]) {
LL res=bfs(i,j);
if(res) {
sum+=res;
ans.pb(res);
}
}
}
}
Ma-=sum;
if(Ma) ans.pb(Ma); sort(all(ans)); prf("%d\n",ans.sz());
rep(i,0,ans.sz()) {
prf("%lld",ans[i]);
if(i==ans.sz()-1) prf("\n");
else prf(" ");
}
} return 0;
} //end----------------------------------------------------------------------- /*
222
4 4
4
1 2
2 1
2 3
3 2 7 7
4
3 4
4 3
4 5
5 4 */

HDU 5925 Coconuts 离散化的更多相关文章

  1. hdu 5925 Coconuts 离散化+dfs

    Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem ...

  2. HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

    Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  3. HDU 5925 Coconuts

    2016 CCPC 东北四省赛 D. 一道好题. 现场写崩了. 赛后LSh跟我讲了一种离散化的做法, 没听懂. 题意 一个\(R \cdot C\ (R, C\le 10^9)\) 的矩形点阵上有 $ ...

  4. Coconuts HDU - 5925 (二维离散化求连通块的个数以及大小)

    题目链接: D - Coconuts  HDU - 5925 题目大意:首先是T组测试样例,然后给你n*m的矩阵,原先矩阵里面都是白色的点,然后再输入k个黑色的点.这k个黑色的点可能会使得原先白色的点 ...

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

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

  6. HDU 5925 离散化

    东北赛的一道二等奖题 当时学长想了一个dfs的解法并且通过了 那时自己也有一个bfs的解法没有拿出来 一直没有机会和时ji间xing来验证对错 昨天和队友谈离散化的时候想到了 于是用当时的思路做了一下 ...

  7. HDU - 1255 扫描线+离散化进阶

    这道题最开始我以为和HDU - 1542 那道题一样,只需要把cover次数改成2次即可,但是后面仔细一想,我们需要求的是覆盖次数大于等于2次的,这样的话,我们需要维护两个长度,HDU-1542 由于 ...

  8. hdu 5303 DP(离散化,环形)+贪心

    题目无法正常粘贴,地址:http://acm.hdu.edu.cn/showproblem.php?pid=5303 大意是给出一个环形公路,和它的长度,给出若干颗果树的位置以及树上的果子个数. 起点 ...

  9. HDU 1711 kmp+离散化

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...

随机推荐

  1. 简单利用Clover四叶草安装U盘安装黑苹果

    配置是I5-7600K+技嘉Z270X-UD3+GTX 1050+简单利用Clover四叶草安装U盘安装黑苹果 <ignore_js_op><ignore_js_op> 成功黑 ...

  2. python3安装crypto出错,及解决方法

    首先我用的python3.5的版本 问题的由来,我想通过python去实现RSA加密算法时,破解某网站的js加密认证,网上说需要安装pycrypto,我就去进行pip安装了 pip install p ...

  3. WebLogic远程命令执行

    靶机说明 目标ip:172.16.53.28(window 2003) 本靶机所针对的序列化漏洞系列以及常见安全问题如下: 弱口令登陆控制台部署war包webshell CVE-2018-2893 C ...

  4. BSGS算法总结

    BSGS算法总结 \(BSGS\)算法(Baby Step Giant Step),即大步小步算法,用于解决这样一个问题: 求\(y^x\equiv z\ (mod\ p)\)的最小正整数解. 前提条 ...

  5. 【转载】基于MFC的ActiveX控件开发(3)

    原文:http://iysm.net/?p=122 3.事件 ActiveX 控件使用事件通知容器控件上发生了某些事情.事件的常见示例包括单击控件.使用键盘输入数据和控件状态更改.当发生这些操作时,控 ...

  6. Kubernetes学习之路(六)之创建K8S应用

    一.Deployment的概念 K8S本身并不提供网络的功能,所以需要借助第三方网络插件进行部署K8S中的网络,以打通各个节点中容器的互通. POD,是K8S中的一个逻辑概念,K8S管理的是POD,一 ...

  7. linux日志分割、去重、统计

    一.实例 单条日志模板: 2018-11-08 02:17:22 [Iceberg]process params:IcebergOfferServiceImpl.Params(pk=BF06NA2YE ...

  8. UWP DEP0700: 应用程序注册失败。[0x80073CF9] Install failed. Please contact your software vendor. (Exception from HRESULT: 0x80073CF9)

    现在部署的app项目八成是从以前的一个项目复制过来,修改的.或者本地存在一个相同的app没有卸载. 解决方法: 1. 卸载之前相同的app 2.如果是复制的项目A,但是经过修改后变成了项目B,并且A和 ...

  9. $('#uplodFileForm')[0].submit();

    jquery对象在[0]以下是取其相对应的Dom对象,即$("#mainForm")[0] = document.getElementById("mainForm&quo ...

  10. 开源项目CIIP(企业信息管理系统框架).2018.1.0910版更新介绍-上周工作总结

    又狂撸了一周的代码.简化了0904版本的多数操作. 上一次更新时,总共需要10步,这次简化成3步.嗯嗯,自我感觉不错. 重要的:在创建项目时,可以选择常用模块啦! 第一步:启动CIIP.Designe ...