fork(1) download
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <algorithm>
  7. #include <stdlib.h>
  8. using namespace std;
  9. //setlocale( LC_ALL, "russian_russia.1251" );
  10. /*
  11. int main()
  12. {
  13.   setlocale( LC_ALL, "russian_russia.1251" );
  14. }
  15. */
  16.  
  17. //*
  18. const int ROWS = 7;
  19. const int COLS = 10;
  20. int array[ ROWS ][ COLS ] = {
  21. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  22. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  23. {0, 0, 1, 1, 1, 1, 0, 0, 0, 0 },
  24. {0 ,0 ,1 ,1 ,0 ,1 ,0 ,1 ,0 ,0 },
  25. {0, 0, 1, 1, 1, 1, 0, 1, 1, 0 },
  26. {1, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
  27. {0, 1, 1, 1, 0, 0, 0, 0, 0, 0 }
  28. };
  29. bool watched[ ROWS ][ COLS ] = { 0 };
  30.  
  31.  
  32. struct Rect
  33. {
  34. int left;
  35. int top;
  36. int right;
  37. int bottom;
  38. };
  39. typedef vector< Rect > Objects;
  40.  
  41. void go( int y, int x, int y_inc, int x_inc, Rect & rect )
  42. {
  43. y = y + y_inc;
  44. x = x + x_inc;
  45. //cout << y << " " << x << endl;
  46. if ( y >= 0 && y < ROWS && x >= 0 && x < COLS )
  47. {
  48. if ( array[ y ][ x ] != 0 && watched[ y ][ x ] == false )
  49. {
  50. //cout << "+" << endl;
  51. watched[ y ][ x ] = true;
  52. rect.left = std::min( rect.left, x );
  53. rect.top = std::min( rect.top, y );
  54. rect.right = std::max( rect.right, x );
  55. rect.bottom = std::max( rect.bottom, y );
  56. go( y, x, -1, -1, rect ); // up left
  57. go( y, x, -1, 0, rect ); // up
  58. go( y, x, -1, 1, rect ); // up right
  59. go( y, x, 0, -1, rect ); // left
  60. go( y, x, 0, 1, rect ); // right
  61. go( y, x, 1, 1, rect ); // down right
  62. go( y, x, 1, 0, rect ); // down
  63. go( y, x, 1, -1, rect ); // down left
  64. }
  65. }
  66. }
  67.  
  68. int main()
  69. {
  70. Objects objects;
  71. cout << " |";
  72. for ( int x = 0; x < COLS; x++ )
  73. {
  74. cout << " " << x;
  75. }
  76. cout << endl;
  77. cout << "--|";
  78. for ( int x = 0; x < COLS; x++ )
  79. {
  80. cout << "--";
  81. }
  82. cout << endl;
  83. for ( int y = 0; y < ROWS; y++ )
  84. {
  85. cout << y << " | ";
  86. for ( int x = 0; x < COLS; x++ )
  87. {
  88. if ( array[ y ][ x ] != 0 && watched[ y ][ x ] == false )
  89. {
  90. Rect rect;
  91. rect.left = x;
  92. rect.top = y;
  93. rect.right = x;
  94. rect.bottom = y;
  95. go( y, x, 0, 0, rect );
  96. objects.push_back( rect );
  97. }
  98. cout << array[ y ][ x ] << ' ';
  99. }
  100. cout << endl;
  101. }
  102. cout << endl;
  103. for ( Objects::const_iterator it = objects.begin(); it != objects.end(); ++it )
  104. {
  105. const Rect & rect = (*it);
  106. cout << "left = " << rect.left << " top = " << rect.top << " "
  107. << "right = " << rect.right << " bottom = " << rect.bottom
  108. << endl;
  109. }
  110. }
  111. //*/
  112.  
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
  | 0 1 2 3 4 5 6 7 8 9
--|--------------------
0 | 0 0 0 0 0 0 0 0 0 0 
1 | 0 0 0 0 0 0 0 0 0 0 
2 | 0 0 1 1 1 1 0 0 0 0 
3 | 0 0 1 1 0 1 0 1 0 0 
4 | 0 0 1 1 1 1 0 1 1 0 
5 | 1 0 0 1 0 0 0 0 0 0 
6 | 0 1 1 1 0 0 0 0 0 0 

left = 0 top = 2 right = 5 bottom = 6
left = 7 top = 3 right = 8 bottom = 4