Назад к лучшим решениям Status: AC, problem ZDIP, contest ZEL07. By nerevar (Dmitry Matov), 2007-02-25 16:46:07.
  1. #pragma comment(linker, "/STACK:64000000")
  2. #define _CRT_SECURE_NO_DEPRECATE
  3. #include <iostream>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <set>
  7. #include <map>
  8. #include <cmath>
  9. #include <cstdio>
  10. #include <cassert>
  11. #include <string>
  12. #include <queue>
  13. #include <stack>
  14. #include <deque>
  15. #include <numeric>
  16. #include <sstream>
  17.  
  18. using namespace std;
  19.  
  20. #define CIN_FILE "input.txt"
  21. #define COUT_FILE "output.txt"
  22.  
  23. #define forn(i, n) for(int i = 0; i < int(n); i++)
  24. #define for1(i, n) for(int i = 1; i <= int(n); i++)
  25. #define forv(i, v) forn(i, v.size())
  26.  
  27. #define VI vector<int>
  28. #define pb push_back
  29. #define pii pair<int, int>
  30. #define mp make_pair
  31. #define all(v) v.begin(), v.end()
  32.  
  33. #define NMAX 205
  34. #define ZMAX 52
  35.  
  36. const int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1 };
  37. const int dy[8] = {1, 0, -1, 0, -1, 1, -1, 1};
  38.  
  39. const int Delta[5] = {28, 27, 26, 20, 18};
  40.  
  41. int delta = 26;
  42. const int magic = 7;
  43. const int minSize = 10;
  44.  
  45. struct Point
  46. {
  47. int x, y;
  48. };
  49.  
  50. char s[5];
  51. int a[NMAX][NMAX], b[NMAX][NMAX];
  52. int col[NMAX][NMAX];
  53. vector<Point> comp[NMAX * NMAX];
  54. int size[NMAX * NMAX];
  55. int sum[NMAX * NMAX];
  56. int cZone[ZMAX];
  57. int t, q, w, h, cntC;
  58.  
  59. inline bool valid(int x, int y)
  60. {
  61. return (x >= 0 && y >= 0 && x < h && y < w);
  62. }
  63.  
  64. void dfs(int x, int y, int c)
  65. {
  66. if(col[x][y] != - 1) return;
  67. Point p = {x, y};
  68. comp[c].push_back(p);
  69. col[x][y] = c;
  70. size[c]++;
  71. sum[c]+= a[x][y];
  72. forn(i, 8)
  73. {
  74. int xx = x + dx[i];
  75. int yy = y + dy[i];
  76. if(!valid(xx, yy)) continue;
  77. if(abs(sum[c] / size[c] - a[xx][yy]) > magic) continue;
  78. dfs(xx, yy, c);
  79. }
  80. }
  81.  
  82.  
  83. int main()
  84. {
  85. scanf("%d", &t);
  86. forn(it, t)
  87. {
  88. scanf("%d %d %d", &q, &h, &w);
  89. forn(i, h)
  90. {
  91. forn(j, w)
  92. {
  93. scanf("%s", s);
  94. a[i][j] = atoi(s);
  95. }
  96. }
  97. forn(iter, 3)
  98. {
  99. memset(size, 0, sizeof(size));
  100. memset(col, 255, sizeof(col));
  101. cntC = 0;
  102. forn(i, h)
  103. {
  104. forn(j, w)
  105. {
  106. if(col[i][j] != - 1) continue;
  107. comp[cntC].clear();
  108. dfs(i, j, cntC);
  109. ++cntC;
  110. }
  111. }
  112. delta = Delta[iter];
  113. forn(i, h)
  114. {
  115. forn(j, w)
  116. {
  117. if(size[col[i][j]] >= minSize)
  118. {
  119. b[i][j] = a[i][j];
  120. continue;
  121. }
  122. int cnt = 0;
  123. double S = 0;
  124. forn(l, 4)
  125. {
  126. int ii = i + dx[l];
  127. int jj = j + dy[l];
  128. if(!valid(ii, jj)) continue;
  129. S+= a[ ii][jj];
  130. ++cnt;
  131. }
  132.  
  133. double mid = S / cnt;
  134. double midDif = 0;
  135. forn(l, 4)
  136. {
  137. int ii = i + dx[l];
  138. int jj = j + dy[l];
  139. if(!valid(ii, jj)) continue;
  140. midDif+= abs(a[ii][jj] - mid);
  141. }
  142. midDif/= cnt;
  143. if(abs(a[i][j] - mid) > midDif + delta)
  144. {
  145. b[i][j] = int(mid);
  146. }
  147. else
  148. {
  149. b[i][j] = a[i][j];
  150. }
  151. }
  152. }
  153.  
  154. memcpy(a, b, sizeof(b));
  155. }
  156.  
  157.  
  158.  
  159. printf ("%d %d\n", h, w);
  160. forn(i, h)
  161. {
  162. forn(j, w)
  163. {
  164. printf(" ");
  165. if(b[i][j] < 100) printf("0");
  166. if(b[i][j] < 10) printf("0");
  167. printf("%d", b[i][j]);
  168. }
  169. printf("\n");
  170. }
  171. }
  172. return 0;
  173. }