题目链接:

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.”。我们标记下四周的状态,就可以判断我们统计的块是不是被夹在角落,还是离散化之后伪的被夹在角落。

代码

  1. #include<map>
  2. #include<set>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<ctime>
  7. #include<vector>
  8. #include<cstdio>
  9. #include<string>
  10. #include<bitset>
  11. #include<cstdlib>
  12. #include<cstring>
  13. #include<iostream>
  14. #include<algorithm>
  15. #include<functional>
  16. using namespace std;
  17. #define X first
  18. #define Y second
  19. #define mkp make_pair
  20. #define lson (o<<1)
  21. #define rson ((o<<1)|1)
  22. #define mid (l+(r-l)/2)
  23. #define sz() size()
  24. #define pb(v) push_back(v)
  25. #define all(o) (o).begin(),(o).end()
  26. #define clr(a,v) memset(a,v,sizeof(a))
  27. #define bug(a) cout<<#a<<" = "<<a<<endl
  28. #define rep(i,a,b) for(int i=a;i<(b);i++)
  29. #define scf scanf
  30. #define prf printf
  31. typedef long long LL;
  32. typedef vector<int> VI;
  33. typedef pair<int,int> PII;
  34. typedef vector<pair<int,int> > VPII;
  35. const int INF=0x3f3f3f3f;
  36. const LL INFL=0x3f3f3f3f3f3f3f3fLL;
  37. const double eps=1e-8;
  38. const double PI = acos(-1.0);
  39. //start----------------------------------------------------------------------
  40. const int maxn=222;
  41. int R,C,n,nn,mm;
  42. PII pt[maxn];
  43. int vis[maxn][maxn];
  44. const int dx[]= {-1,1,0,0};
  45. const int dy[]= {0,0,-1,1};
  46. bool _flag[4];
  47. LL bfs(int xs,int ys) {
  48. queue<PII> Q;
  49. LL res=1;
  50. vis[xs][ys]=1;
  51. Q.push(mkp(xs,ys));
  52. int flag=0;
  53. while(!Q.empty()) {
  54. PII u=Q.front();
  55. Q.pop();
  56. int x=u.X,y=u.Y;
  57. for(int i=0; i<4; i++) {
  58. int nx=x+dx[i];
  59. int ny=y+dy[i];
  60. if(nx<1||nx>nn||ny<1||ny>mm) {
  61. if(nx<1&&_flag[0]==0) flag=1;
  62. if(nx>nn&&_flag[1]==0) flag=1;
  63. if(ny<1&&_flag[2]==0) flag=1;
  64. if(ny>mm&&_flag[3]==0) flag=1;
  65. continue;
  66. }
  67. if(!vis[nx][ny]) {
  68. vis[nx][ny]=1;
  69. res++;
  70. Q.push(mkp(nx,ny));
  71. }
  72. }
  73. }
  74. if(flag) return 0;
  75. return res;
  76. }
  77. int main() {
  78. int tc,kase=0;
  79. scf("%d",&tc);
  80. while(tc--) {
  81. scf("%d%d%d",&R,&C,&n);
  82. VI ha_x,ha_y;
  83. clr(_flag,0);
  84. rep(i,0,n) {
  85. scf("%d%d",&pt[i].X,&pt[i].Y);
  86. //标记四周的状态,用于判断被围在边缘的情况
  87. if(pt[i].X==1) _flag[0]=1;
  88. if(pt[i].X==R) _flag[1]=1;
  89. if(pt[i].Y==1) _flag[2]=1;
  90. if(pt[i].Y==C) _flag[3]=1;
  91. //离散化
  92. if(pt[i].X-1>=1) ha_x.pb(pt[i].X-1);
  93. ha_x.pb(pt[i].X);
  94. if(pt[i].X+1<=R) ha_x.pb(pt[i].X+1);
  95. if(pt[i].Y-1>=1) ha_y.pb(pt[i].Y-1);
  96. ha_y.pb(pt[i].Y);
  97. if(pt[i].Y+1<=C) ha_y.pb(pt[i].Y+1);
  98. }
  99. //离散化
  100. sort(all(ha_x));
  101. ha_x.erase(unique(all(ha_x)),ha_x.end());
  102. sort(all(ha_y));
  103. ha_y.erase(unique(all(ha_y)),ha_y.end());
  104. nn=ha_x.sz();
  105. mm=ha_y.sz();
  106. //外围的那个联通块
  107. LL Ma=(LL)R*C-n;
  108. vector<LL> ans;
  109. //离散化
  110. clr(vis,0);
  111. rep(i,0,n) {
  112. pt[i].X=lower_bound(all(ha_x),pt[i].X)-ha_x.begin()+1;
  113. pt[i].Y=lower_bound(all(ha_y),pt[i].Y)-ha_y.begin()+1;
  114. vis[pt[i].X][pt[i].Y]=1;
  115. }
  116. prf("Case #%d:\n",++kase);
  117. LL sum=0;
  118. //对于离散化的图暴力bfs
  119. for(int i=1; i<=nn; i++) {
  120. for(int j=1; j<=mm; j++) {
  121. if(!vis[i][j]) {
  122. LL res=bfs(i,j);
  123. if(res) {
  124. sum+=res;
  125. ans.pb(res);
  126. }
  127. }
  128. }
  129. }
  130. Ma-=sum;
  131. if(Ma) ans.pb(Ma);
  132. sort(all(ans));
  133. prf("%d\n",ans.sz());
  134. rep(i,0,ans.sz()) {
  135. prf("%lld",ans[i]);
  136. if(i==ans.sz()-1) prf("\n");
  137. else prf(" ");
  138. }
  139. }
  140. return 0;
  141. }
  142. //end-----------------------------------------------------------------------
  143. /*
  144. 222
  145. 4 4
  146. 4
  147. 1 2
  148. 2 1
  149. 2 3
  150. 3 2
  151. 7 7
  152. 4
  153. 3 4
  154. 4 3
  155. 4 5
  156. 5 4
  157. */

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. JavaScript-闭包函数(理解)

    JavaScript-闭包函数(理解) var foo = function (a) { return function inner () { console.log(a) } } var faa = ...

  2. 如何在Windows版本的VMware虚拟机上安装苹果系统

    有时我想玩玩苹果系统,但自己有没有mac,只能在虚拟机上装一个苹果玩玩,但又由于某些原因虚拟机软件VMware不支持安装苹果系统,还在有大佬出于不明目的,在网上散布了适用于Windows版本的VMwa ...

  3. 关于redis常用命令

    加载redis.cof文件命令: redis-server /etc/redis/redis.conf启动redis命令: redis-cli -p 6379 关于key命令:keys * //查看所 ...

  4. PHP代码优化—array_push

    PHP中数组插入数据通常有这么几种: 定义的时候直接赋值 $arr = array('apple', 'banana'); 使用数组变量操作 $arr = array(); $arr[] = 'app ...

  5. Kafka基础认识

    1):Apache kafka介绍及架构详解 假设一个场景: 数据源: 应用系统A 产生的用户访问数据和订单数据 10000 条一秒钟 push:推送数据 消息系统:队列 产生的数据量>数据量 ...

  6. Ruby 装pg的坑

    sudo apt-get install libpq-devsudo gem install pg -v '0.21.0'

  7. HTTPS为什么又快又安全?

    一.基础:对称加密和非对称加密 对称加密 通信两端用一样的密钥加解密.如DES.AES. 优点:性能损耗低,速度快: 缺点:密钥存在泄露的可能. 非对称加密 通信两端各自持有对方的公钥及自己的私钥,通 ...

  8. STM8S——Universal asynchronous receiver transmitter (UART)

    UART基本介绍: 通用异步收发器UART他的功能非常强大 我们只使用UART的全双工异步通信功能,使用中断接收数据. UART_RX:串行数据输入. UART_TX:串行数据输出. 硬件支持: 连接 ...

  9. 第四节:Windows系统安装时BIOS设置及注意

    BIOS系统 BIOS是英文"Basic Input Output System"的缩略词,直译过来后中文名称就是"基本输入输出系统".在IBM PC兼容系统上 ...

  10. 二级域名 cookie session 共享

    setcookie('login','12345',0,'/','.abc.com'); session_set_cookie_params(0,'/','.abc.com');session_sta ...