fork download
  1. #include <algorithm>
  2. #include <iostream>
  3.  
  4. const int data[10][10] =
  5. {{0,0,3,0,1,0,0,0,0,1},
  6. {0,0,0,2,0,0,0,0,0,0},
  7. {0,0,0,0,0,0,1,1,0,0},
  8. {0,0,0,0,0,0,0,3,0,0},
  9. {0,4,2,0,0,0,0,0,0,0},
  10. {0,1,0,0,0,0,0,0,0,0},
  11. {0,0,0,0,0,0,0,0,0,0},
  12. {0,0,0,0,0,8,1,1,0,0},
  13. {0,0,0,0,3,6,0,0,2,0},
  14. {2,0,0,0,0,0,0,0,0,0}};
  15.  
  16. int dataMax[10][10] = {};
  17.  
  18. int getMax(int minx, int maxx, int miny, int maxy)
  19. {
  20. minx = std::max(minx, 0);
  21. maxx = std::min(maxx, 10);
  22. miny = std::max(miny, 0);
  23. maxy = std::min(maxy, 10);
  24. int res = data[minx][miny];
  25.  
  26. for (int x = minx; x != maxx; ++x) {
  27. for (int y = miny; y != maxy; ++y) {
  28. res = std::max(res, data[x][y]);
  29. }
  30. }
  31. return res;
  32. }
  33.  
  34. void compute()
  35. {
  36. const int h = 3; // Size of region to compare
  37.  
  38. for (int x = 0; x != 10; ++x) {
  39. for (int y = 0; y != 10; ++y) {
  40. dataMax[x][y] = getMax(x - h / 2, x + h - h / 2,
  41. y - h / 2, y + h - h / 2);
  42. }
  43. }
  44. }
  45.  
  46. void filter()
  47. {
  48. for (int x = 0; x != 10; ++x) {
  49. for (int y = 0; y != 10; ++y) {
  50. if (dataMax[x][y] != data[x][y]) {
  51. dataMax[x][y] = 0;
  52. }
  53. }
  54. }
  55. }
  56.  
  57. void print(const int (&a)[10][10])
  58. {
  59. for (int x = 0; x != 10; ++x) {
  60. for (int y = 0; y != 10; ++y) {
  61. std::cout << a[x][y] << " ";
  62. }
  63. std::cout << std::endl;
  64. }
  65. }
  66.  
  67. int main(int argc, char *argv[])
  68. {
  69. compute();
  70. print(data);
  71. std::cout << std::endl;
  72. print(dataMax);
  73. std::cout << std::endl;
  74. filter();
  75. print(dataMax);
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
0 0 3 0 1 0 0 0 0 1 
0 0 0 2 0 0 0 0 0 0 
0 0 0 0 0 0 1 1 0 0 
0 0 0 0 0 0 0 3 0 0 
0 4 2 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 8 1 1 0 0 
0 0 0 0 3 6 0 0 2 0 
2 0 0 0 0 0 0 0 0 0 

0 3 3 3 2 1 0 0 1 1 
0 3 3 3 2 1 1 1 1 1 
0 0 2 2 2 1 3 3 3 0 
4 4 4 2 0 1 3 3 3 0 
4 4 4 2 0 0 3 3 3 0 
4 4 4 2 0 0 0 0 0 0 
1 1 1 0 8 8 8 1 1 0 
0 0 0 3 8 8 8 2 2 2 
2 2 0 3 8 8 8 2 2 2 
2 2 0 3 6 6 6 2 2 2 

0 0 3 0 0 0 0 0 0 1 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 3 0 0 
0 4 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 8 0 0 0 0 
0 0 0 0 0 0 0 0 2 0 
2 0 0 0 0 0 0 0 0 0