题目链接

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

Problem Description
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).

 
Input
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<R,C≤109 the second line contains an integer n, the number of bad coconuts, 0≤n≤200 from the third line, there comes n lines, each line contains two integers, xi and yi, which means in cell(xi,yi), there is a bad coconut.

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.

 
Output
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.
 
Sample Input
2
3 3
2
1 2
2 1
3 3
1
2 2
 
Sample Output
Case #1:
2
1 6
Case #2:
1
8
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5932 5931 5930 5929 5928 
 
题意:有一个R*C的棋盘方格,上面有n个障碍点,求这些障碍点将棋盘分割后的每一个区域的方格数,要求先输出分成的区域数,然后从小到大输出方格数;
 
思路:要求求出每一个区域的方格数,那么可以DFS深搜求出来,但是R*C太大,会超时,所以必须要进行离散化,减小棋盘; 可以分别用两个数组x[] y[] 存储障碍点的横纵坐标,然后从小到大排序,分别对x  y进行离散,这两个离散过程相同,例如在对x进行离散的过程的中如果x[i]==x[i-1]  那么离散到和x[i-1]同一行 ,如果x[i]==x[i-1]+1 那么离散到x[i-1]对应行的下一行, 其余情况离散到x[i-1]对应行的下一行的下一行;
 
代码如下:
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <vector>
  5. #include <cstdio>
  6. #include <cmath>
  7. using namespace std;
  8. typedef long long LL;
  9. const int N=;
  10. LL dx[N],dy[N];///表示每一格压缩的长度;
  11. bool v[N][N];
  12. int dir[][]={{,},{-,},{,},{,-}};
  13. LL r,c;
  14. int n;
  15.  
  16. struct Node{
  17. long long v,p;
  18. int id;
  19. }x[N],y[N];
  20.  
  21. bool cmp1(const Node s1,const Node s2){
  22. return s1.v<s2.v;
  23. }
  24. bool cmp2(const Node s1,const Node s2){
  25. return s1.id<s2.id;
  26. }
  27.  
  28. LL dfs(int sx,int sy)
  29. {
  30. LL sum=dx[sx]*dy[sy];
  31. v[sx][sy]=true;
  32. for(int i=;i<;i++){
  33. int nx=sx+dir[i][];
  34. int ny=sy+dir[i][];
  35. if(nx>&&ny>&&nx<=r&&ny<=c)
  36. if(!v[nx][ny]){
  37. sum+=dfs(nx,ny);
  38. }
  39. }
  40. return sum;
  41. }
  42.  
  43. int main()
  44. {
  45. int T,Case=;
  46. cin>>T;
  47. while(T--)
  48. {
  49. printf("Case #%d:\n",Case++);
  50. scanf("%lld%lld%d",&r,&c,&n);
  51. for(int i=;i<=n;i++)
  52. {
  53. scanf("%lld%lld",&x[i].v,&y[i].v);
  54. x[i].id=i;
  55. y[i].id=i;
  56. }
  57. sort(x+,x+n+,cmp1);
  58. sort(y+,y+n+,cmp1);
  59. x[n+].v=r; y[n+].v=c;
  60. x[n+].id=y[n+].id=n+;
  61. x[].v=y[].v=;
  62. x[].id=y[].id=;
  63.  
  64. int tot=;
  65. dx[]=;
  66. for(int i=;i<=n+;i++)
  67. {
  68. if(x[i].v==x[i-].v){
  69. x[i].p=tot;
  70. }
  71. else if(x[i].v==x[i-].v+){
  72. x[i].p=++tot;
  73. dx[tot]=;
  74. }
  75. else {
  76. x[i].p=tot+;
  77. dx[tot+]=;
  78. dx[tot+]=x[i].v-x[i-].v-;
  79. tot+=;
  80. }
  81. }
  82. r=tot;
  83. tot=;
  84. dy[]=;
  85. for(int i=;i<=n+;i++)
  86. {
  87. if(y[i].v==y[i-].v){
  88. y[i].p=tot;
  89. }
  90. else if(y[i].v==y[i-].v+){
  91. y[i].p=++tot;
  92. dy[tot]=;
  93. }
  94. else {
  95. y[i].p=tot+;
  96. dy[tot+]=;
  97. dy[tot+]=y[i].v-y[i-].v-;
  98. tot+=;
  99. }
  100. }
  101. c=tot;
  102.  
  103. memset(v,,sizeof(v));
  104. sort(x+,x+n+,cmp2);
  105. sort(y+,y+n+,cmp2);
  106. for(int i=;i<=n;i++)
  107. v[x[i].p][y[i].p]=true;
  108. long long ans[N];
  109. tot=;
  110. for(LL i=;i<=r;i++)
  111. for(LL j=;j<=c;j++)
  112. if(!v[i][j])
  113. ans[tot]=dfs(i,j),tot++;
  114. sort(ans,ans+tot);
  115. printf("%d\n",tot);
  116. for(int i=;i<tot;i++)
  117. printf("%lld%c",ans[i],(i+==tot)?'\n':' ');
  118. }
  119. return ;
  120. }

2016 长春东北赛---Coconuts(离散化+DFS)的更多相关文章

  1. hdu 5925 Coconuts 离散化+dfs

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

  2. 2016 大连网赛---Weak Pair(dfs+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...

  3. 2016 CCPC 东北地区重现赛

    1. 2016 CCPC 东北地区重现赛 2.总结:弱渣,只做出01.03.05水题 08   HDU5929 Basic Data Structure    模拟,双端队列 1.题意:模拟一个栈的操 ...

  4. 2015年ACM长春区域赛比赛感悟

    距离长春区域赛结束已经4天了,是时候整理一下这次比赛的点点滴滴了. 也是在比赛前一周才得到通知要我参加长春区域赛,当时也是既兴奋又感到有很大的压力,毕竟我的第一场比赛就是区域赛水平,还是很有挑战性的. ...

  5. 2012年长春网络赛(hdu命题)

    为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...

  6. HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)

    题目链接  2016 CCPC东北地区大学生程序设计竞赛 B题 题意  给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问, 每次选定树中的一个点集,然后真正被选上的是这 ...

  7. Aizu 0531 "Paint Color" (坐标离散化+DFS or BFS)

    传送门 题目描述: 为了宣传信息竞赛,要在长方形的三合板上喷油漆来制作招牌. 三合板上不需要涂色的部分预先贴好了护板. 被护板隔开的区域要涂上不同的颜色,比如上图就应该涂上5种颜色. 请编写一个程序计 ...

  8. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. HDU 4816 Bathysphere (2013长春现场赛D题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 2013长春区域赛的D题. 很简单的几何题,就是给了一条折线. 然后一个矩形窗去截取一部分,求最 ...

随机推荐

  1. node.js 简介

    简介:     Node,是一个可以让 JavaScript 运行在服务器端的平台.它可以让 JavaScript 脱离浏览器的束缚运行在一般的服务器环境下     Node.js 是一个为实时Web ...

  2. react6 事件传递参数

    <body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...

  3. CMD复制文件夹

    CMD复制文件夹 xcopy /E/I/Y "D:\GitHub\WIP\app" "D:\GitHub\WIP_server\html\webshell"

  4. 大型.NET商业软件代码保护技术 技术与实践相结合保护辛苦创造的劳动成果

    列举工作以来遇到的各种类型的软件所采用的代码保护技术,只讲原理不涉及技术细节实现,以避免产生法律问题.有些朋友说直接把代码放在Github开源下载,开源可以促进技术交流与进步,然而值钱的代码都积压在硬 ...

  5. 截取js数组中某段值(slice)

    // var a = [1,2,3]; // console.log(a.slice(1)); >>[2, 3] 从索引1开始截取. // console.log(a.slice(1,2) ...

  6. Mysql存储过程语法

    一口气弄完了! 一.条件语句if-then-else: create procedure demo_1(in param int) begin declare var int; ; then inse ...

  7. photoshop学习目录

    前面的话 前端工程师最基本的工作是切图.photoshop用的6不6,对于工作效率有很大的影响.小火柴将前端工程师需要掌握的photoshop的知识和技能进行了梳理和归纳,总结成以下目录 目录 前端工 ...

  8. 用命令行编译java并生成可执行的jar包

    用命令行编译java并生成可执行的jar包 1.编写源代码. 编写源文件:CardLayoutDemo.java并保存,例如:I:\myApp\CardLayoutDemo.java.程序结构如下: ...

  9. poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)

    LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...

  10. 《BI那点儿事》Microsoft 决策树算法

    Microsoft 决策树算法是由 Microsoft SQL Server Analysis Services 提供的分类和回归算法,用于对离散和连续属性进行预测性建模.对于离散属性,该算法根据数据 ...