Advertisement
Garey

[CPS] Homework 1

Feb 26th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. const int rows = 5;
  4. const int cols = 8;
  5.  
  6. int arr[rows][cols] = {
  7.     { 0, 0, 0, 0, 1, 1, 0, 0, },
  8.     { 1, 0, 0, 1, 1, 1, 0, 0, },
  9.     { 1, 1, 0, 1, 0, 1, 1, 0, },
  10.     { 0, 0, 0, 1, 1, 1, 1, 0, },
  11.     { 0, 1, 1, 0, 0, 0, 0, 0, },
  12. };
  13.  
  14. int fill(int [][cols], size_t, size_t);
  15. void print_array(int [][cols]);
  16.  
  17. int main() {
  18.   print_array(arr);
  19.  
  20.   system("pause");
  21.   return 0;
  22. }
  23.  
  24. void print_array(int arr[rows][cols]) {
  25.   std::cout << "Default array: \n\n";
  26.  
  27.   for(size_t i = 0; i < rows; i++) {
  28.       for(size_t j = 0; j < cols; j++)    
  29.           std::cout << " " << arr[i][j];
  30.     std::cout << std::endl;
  31.   }
  32.  
  33.   std::cout << "\n\nFilled array: \n\n";
  34.  
  35.   for(size_t i = 0; i < rows; i++) {
  36.       for(size_t j = 0; j < cols; j++) {
  37.           if(arr[i][j])
  38.             fill(arr, i, j);
  39.                  
  40.           std::cout << " " << arr[i][j];
  41.       }
  42.     std::cout << std::endl;
  43.   }
  44. }
  45.  
  46. int fill(int arr[rows][cols], size_t row, size_t col) {
  47.   int count = 0;
  48.  
  49.   if (row < rows && arr[row][col]) {
  50.     for (size_t i = col; i >= 0 && arr[row][i]; --i) {
  51.       arr[row][i] = 2;
  52.       count += fill(arr, row + 1, i) + 1; // count of max area covered
  53.     }
  54.     for (size_t i = col + 1; i < cols && arr[row][i]; ++i) {
  55.       arr[row][i] = 2;
  56.       count += fill(arr, row + 1, i) + 1; // count of max area covered
  57.     }
  58.   }
  59.   return count;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement