fork download
  1. #include <iostream>
  2.  
  3. int count_regions( int *arr, int rows, int cols ) {
  4. int region_count = 0;
  5.  
  6. for ( int first_index = 0; first_index != rows * cols; ++ first_index ) {
  7. if ( arr[ first_index ] == 0 ) continue;
  8.  
  9. ++ region_count;
  10.  
  11. int first_row = first_index / cols, first_col = first_index % cols;
  12. int last_col;
  13. for ( last_col = first_col;
  14. last_col != cols && arr[ first_row * cols + last_col ] != 0;
  15. ++ last_col ) ;
  16.  
  17. for ( int last_row = first_row;
  18. last_row != rows && arr[ last_row * cols + first_col ] != 0;
  19. ++ last_row ) {
  20. for ( int col = first_col; col != last_col; ++ col ) {
  21. arr[ last_row * cols + col ] = 0;
  22. }
  23. }
  24. }
  25. return region_count;
  26. }
  27.  
  28. int main() {
  29. int arr[] = {
  30. 1, 1, 1, 0, 3, 3, 3, 0, 2, 2,
  31. 1, 1, 1, 0, 3, 3, 3, 0, 2, 2,
  32. 0, 0, 0, 0, 3, 3, 3, 0, 0, 0,
  33. 2, 2, 0, 0, 3, 3, 3, 0, 4, 4,
  34. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  35. 1, 1, 1, 1, 1, 0, 4, 4, 4, 0
  36. };
  37.  
  38. std::cout << count_regions( arr, 6, 10 ) << '\n';
  39. }
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
7