2016 northeast Changchun competition --- Coconuts (discretization + DFS), --- coconutsdfs
Question Link
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5925
Problem DescriptionTanBig, 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 wocould eat each time (the area of each 4-connected component ).
InputThe 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 ≤0 ≤ 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.
OutputFor 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 wocould eat each time, you shoshould output them in increasing order.
Sample Input23 321 22 13 312 2 Sample OutputCase #6 Case #
Source2016CCPC northeast College Student Program Design Competition-reproduction Competition
RecommendWange2014 | We have carefully selected several similar problems for you: 5932 5931 5930 5929 5928 question: there is an R * C checkboard with n obstacles on it, find the number of squares in each area after the Board is split by these barrier points, and output the number of squares in each area from small to large. Idea: obtain the number of squares in each area, the DFS can be searched in depth, but R * C is too large and Will time out. Therefore, discretization is required to reduce the board size; you can use two arrays x [] y [] to store the horizontal and vertical coordinates of obstacle points, and sort them in ascending order to separate x y. The two discrete processes are the same, for example, if x [I] = x [I-1] is discrete to the same row as x [I-1] in the Process of discretization x, if x [I] = x [I-1] + 1 then discrete to the next row of x [I-1, the rest is discrete to the next row of the corresponding row of x [I-1]; the Code is as follows:
# Include <algorithm> # include <iostream> # include <cstring> # include <vector> # include <cstdio> # include <cmath> using namespace std; typedef long LL; const int N = 405; LL dx [N], dy [N]; // indicates the compression length of each grid; bool v [N] [N]; int dir [4] [2] = {}, {-}, {}, {0,-1}; LL r, c; int n; struct Node {long v, p; int id;} x [N], y [N]; bool cmp1 (const Node s1, const Node s2) {return s1.v <s2.v;} bool cmp2 (const Node s1, const Node s 2) {return s1.id <s2.id;} LL dfs (int sx, int sy) {LL sum = dx [sx] * dy [sy]; v [sx] [sy] = true; for (int I = 0; I <4; I ++) {int nx = sx + dir [I] [0]; int ny = sy + dir [I] [1]; if (nx> 0 & ny> 0 & nx <= r & ny <= c) if (! V [nx] [ny]) {sum + = dfs (nx, ny) ;}} return sum ;}int main () {int T, Case = 1; cin> T; while (T --) {printf ("Case # % d: \ n", Case ++); scanf ("% lld % d ", & r, & c, & n); for (int I = 1; I <= n; I ++) {scanf ("% lld ", & x [I]. v, & y [I]. v); x [I]. id = I; y [I]. id = I;} sort (x + 1, x + n + 1, cmp1); sort (y + 1, y + n + 1, cmp1 ); x [n + 1]. v = r; y [n + 1]. v = c; x [n + 1]. id = y [n + 1]. id = n + 1; x [0]. v = y [0]. v = 1; x [0]. id = y [0]. id = 0; int tot = 1; dx [1] = 1; for (int I = 1; I <= n + 1; I ++) {if (x [I]. v = x [I-1]. v) {x [I]. p = tot;} else if (x [I]. v = x [I-1]. v + 1) {x [I]. p = ++ tot; dx [tot] = 1;} else {x [I]. p = tot + 2; dx [tot + 2] = 1; dx [tot + 1] = x [I]. v-x [I-1]. v-1; tot + = 2 ;}}r = tot; tot = 1; dy [1] = 1; for (int I = 1; I <= n + 1; I ++) {if (y [I]. v = y [I-1]. v) {y [I]. p = tot;} else if (y [I]. v = y [I-1]. v + 1) {y [I]. p = ++ tot; dy [tot] = 1;} else {y [I]. p = tot + 2; dy [tot + 2] = 1; dy [tot + 1] = y [I]. v-y [I-1]. v-1; tot + = 2; }}c = tot; m Emset (v, 0, sizeof (v); sort (x + 1, x + n + 1, cmp2); sort (y + 1, y + n + 1, cmp2); for (int I = 1; I <= n; I ++) v [x [I]. p] [y [I]. p] = true; long ans [N]; tot = 0; for (LL I = 1; I <= r; I ++) for (LL j = 1; j <= c; j ++) if (! V [I] [j]) ans [tot] = dfs (I, j), tot ++; sort (ans, ans + tot ); printf ("% d \ n", tot); for (int I = 0; I <tot; I ++) printf ("% lld % c ", ans [I], (I + 1 = tot )? '\ N': '');} return 0 ;}