Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int MAX_SIZE = 100;
- bool isOccupied(char forestTerrain[][100], int i, int j, int r, int l, int counter)
- {
- if (r == i + 1 && l == j + 1) {
- if (forestTerrain[r][l] == '4')
- ++counter;
- if (counter >= 3)
- return true;
- else return false;
- }
- else if (r == i && l == j)
- {
- isOccupied(forestTerrain,i,j ,r, l + 1,counter);
- }
- else if (l == j + 1)
- {
- if (forestTerrain[r][l] == '4')
- ++counter;
- l = j - 1;
- isOccupied(forestTerrain, i, j ,r + 1, l,counter);
- }
- else
- {
- if (forestTerrain[r][l] == '4')
- ++counter;
- isOccupied(forestTerrain,i, j, r , l+1, counter);
- }
- }
- void changeTheDensity(char newForestTerrain[][100], char forestTerrain[][100], int i, int j)
- {
- if (forestTerrain[i][j] == '1')
- newForestTerrain[i][j] = '2';
- else if (forestTerrain[i][j] == '2')
- newForestTerrain[i][j] = '3';
- else if (forestTerrain[i][j] == '3')
- newForestTerrain[i][j] = '4';
- else if (forestTerrain[i][j] == '4')
- {
- if (isOccupied(forestTerrain, i, j, i-1, j-1, 0))
- {
- newForestTerrain[i][j] = '3';
- }
- }
- }
- void loopWithRecursion(char forestTerrain[][100], char newForestTerrain[][100], int row, int col,int n, int m)
- {
- if (row == n - 1 && col == m - 1)
- changeTheDensity(newForestTerrain, forestTerrain, row, col);
- else if (col == m - 1)
- {
- changeTheDensity(newForestTerrain, forestTerrain, row, col);
- col = 0;
- loopWithRecursion(forestTerrain, newForestTerrain, row + 1, col, n, m);
- }
- else
- {
- changeTheDensity(newForestTerrain, forestTerrain, row, col);
- loopWithRecursion(forestTerrain, newForestTerrain, row, col + 1, n, m);
- }
- }
- void printForestTerrain(char forestTerrain[][100], int n, int m)
- {
- for (size_t i = 0; i < n; i++)
- {
- cout << endl;
- for (size_t j = 0; j < m; j++)
- {
- cout << forestTerrain[i][j] << " ";
- }
- }
- }
- void fulfillTheTerrain(char newForestTerrain[][100], char forestTerrain[][100], int n, int m)
- {
- for (size_t i = 0; i < n; i++)
- {
- for (size_t j = 0; j < m; j++)
- {
- cin >> forestTerrain[i][j];
- newForestTerrain[i][j] = forestTerrain[i][j];
- }
- }
- }
- void copyForNextYears(char newForestTerrain[][100], char forestTerrain[][100],int row, int col, int n, int m)
- {
- if (row == n - 1 && col == m - 1)
- {
- forestTerrain[row][col] = newForestTerrain[row][col];
- }
- else if (col == m - 1)
- {
- forestTerrain[row][col] = newForestTerrain[row][col];
- col = 0;
- copyForNextYears(newForestTerrain, forestTerrain, row + 1, col, n, m);
- }
- else
- {
- forestTerrain[row][col] = newForestTerrain[row][col];
- copyForNextYears(newForestTerrain, forestTerrain, row, col + 1, n, m);
- }
- }
- void yearsRecursion(char newForestTerrain[][100], char forestTerrain[][100] , const int n, const int m, int years)
- {
- if (years == 10)
- {
- loopWithRecursion(forestTerrain, newForestTerrain, 0, 0, n, m);
- }
- else
- {
- loopWithRecursion(forestTerrain,newForestTerrain, 0, 0, n, m);
- copyForNextYears(newForestTerrain, forestTerrain, 0, 0, n, m);
- yearsRecursion(newForestTerrain, forestTerrain, n, m, years - 10);
- }
- }
- int main()
- {
- char forestTerrain[MAX_SIZE][MAX_SIZE];
- char newForestTerrain[MAX_SIZE][MAX_SIZE];
- int m, n, years;
- cout << "Enter n: ";
- cin >> n;
- cout << "Enter m: ";
- cin >> m;
- cout << "Enter the years: ";
- cin >> years;
- if (years % 10 != 0)
- years = years - (years % 10);
- fulfillTheTerrain(newForestTerrain, forestTerrain, n, m);
- yearsRecursion(newForestTerrain, forestTerrain, n, m, years);
- printForestTerrain(newForestTerrain, n, m);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement