Advertisement
totobac

Untitled

Jan 18th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int MAX_SIZE = 100;
  4. bool isOccupied(char forestTerrain[][100], int i, int j, int r, int l, int counter)
  5. {
  6.    
  7.     if (r == i + 1 && l == j + 1) {
  8.         if (forestTerrain[r][l] == '4')
  9.             ++counter;
  10.         if (counter >= 3)
  11.             return true;
  12.         else return false;
  13.  
  14.     }
  15.     else if (r == i && l == j)
  16.     {
  17.         isOccupied(forestTerrain,i,j ,r, l + 1,counter);
  18.     }
  19.     else if (l == j + 1)
  20.     {
  21.         if (forestTerrain[r][l] == '4')
  22.             ++counter;
  23.         l = j - 1;
  24.         isOccupied(forestTerrain, i, j ,r + 1, l,counter);
  25.        
  26.     }
  27.     else
  28.     {
  29.         if (forestTerrain[r][l] == '4')
  30.             ++counter;
  31.         isOccupied(forestTerrain,i, j,  r , l+1, counter);
  32.     }
  33. }
  34.  
  35. void changeTheDensity(char newForestTerrain[][100], char forestTerrain[][100], int i, int j)
  36. {
  37.     if (forestTerrain[i][j] == '1')
  38.         newForestTerrain[i][j] = '2';
  39.  
  40.     else if (forestTerrain[i][j] == '2')
  41.         newForestTerrain[i][j] = '3';
  42.  
  43.     else  if (forestTerrain[i][j] == '3')
  44.         newForestTerrain[i][j] = '4';
  45.  
  46.     else if (forestTerrain[i][j] == '4')
  47.     {
  48.  
  49.         if (isOccupied(forestTerrain, i, j, i-1, j-1, 0))
  50.         {
  51.             newForestTerrain[i][j] = '3';
  52.         }
  53.     }
  54. }
  55.  
  56. void loopWithRecursion(char forestTerrain[][100], char newForestTerrain[][100], int row, int col,int n, int m)
  57. {
  58.     if (row == n - 1 && col == m - 1)
  59.         changeTheDensity(newForestTerrain, forestTerrain, row, col);
  60.  
  61.     else if (col == m - 1)
  62.     {
  63.         changeTheDensity(newForestTerrain, forestTerrain, row, col);
  64.         col = 0;
  65.         loopWithRecursion(forestTerrain, newForestTerrain, row + 1, col, n, m);
  66.        
  67.     }
  68.     else
  69.     {
  70.         changeTheDensity(newForestTerrain, forestTerrain, row, col);
  71.         loopWithRecursion(forestTerrain, newForestTerrain, row, col + 1, n, m);
  72.     }
  73.        
  74. }
  75.  
  76. void printForestTerrain(char forestTerrain[][100], int n, int m)
  77. {
  78.     for (size_t i = 0; i < n; i++)
  79.     {
  80.         cout << endl;
  81.         for (size_t j = 0; j < m; j++)
  82.         {
  83.             cout << forestTerrain[i][j] << " ";
  84.         }
  85.     }
  86. }
  87.  
  88. void fulfillTheTerrain(char newForestTerrain[][100], char forestTerrain[][100], int n, int m)
  89. {
  90.     for (size_t i = 0; i < n; i++)
  91.     {
  92.         for (size_t j = 0; j < m; j++)
  93.         {
  94.             cin >> forestTerrain[i][j];
  95.             newForestTerrain[i][j] = forestTerrain[i][j];
  96.         }
  97.     }
  98. }
  99.  
  100. void copyForNextYears(char newForestTerrain[][100], char forestTerrain[][100],int row, int col, int n, int m)    
  101. {
  102.     if (row == n - 1 && col == m - 1)
  103.     {
  104.         forestTerrain[row][col] = newForestTerrain[row][col];
  105.     }
  106.     else if (col == m - 1)
  107.     {
  108.         forestTerrain[row][col] = newForestTerrain[row][col];
  109.         col = 0;
  110.         copyForNextYears(newForestTerrain, forestTerrain, row + 1, col, n, m);
  111.  
  112.     }
  113.     else
  114.     {
  115.         forestTerrain[row][col] = newForestTerrain[row][col];
  116.         copyForNextYears(newForestTerrain, forestTerrain, row, col + 1, n, m);
  117.     }  
  118. }
  119.  
  120. void yearsRecursion(char newForestTerrain[][100], char forestTerrain[][100] , const int n, const int m, int years)
  121. {
  122.     if (years == 10)
  123.     {
  124.         loopWithRecursion(forestTerrain, newForestTerrain, 0, 0, n, m);
  125.     }
  126.     else
  127.     {
  128.      loopWithRecursion(forestTerrain,newForestTerrain, 0, 0, n, m);
  129.      copyForNextYears(newForestTerrain, forestTerrain, 0, 0, n, m);
  130.      yearsRecursion(newForestTerrain, forestTerrain, n, m, years - 10);
  131.     }  
  132. }
  133.  
  134. int main()
  135. {
  136.     char forestTerrain[MAX_SIZE][MAX_SIZE];
  137.     char newForestTerrain[MAX_SIZE][MAX_SIZE];
  138.     int m, n, years;
  139.     cout << "Enter n: ";
  140.     cin >> n;
  141.    cout << "Enter m: ";
  142.     cin >> m;
  143.     cout << "Enter the years: ";
  144.     cin >> years;
  145.     if (years % 10 != 0)
  146.         years = years - (years % 10);
  147.     fulfillTheTerrain(newForestTerrain, forestTerrain, n, m);
  148.     yearsRecursion(newForestTerrain, forestTerrain, n, m, years);
  149.     printForestTerrain(newForestTerrain, n, m);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement