简体   繁体   中英

How to convert for loops to recursion?

void FireSimulator::spread()
{
    int numberLoops;
    if(x>y)
        numberLoops=x;
    else
        numberLoops=y;

    for(int k=0; k<=numberLoops+1; k++)
   {
        for(int i=1; i<x-1; i++)
        {
            for(int j=1; j<y-1; j++)
            {
                if((forest[i][j].getState()==2) && (forest[i][j+1].getState()==1)) 
                    {
                        forest[i][j+1]=2;
                        Print();
                    }

                if((forest[i][j].getState()==2) && (forest[i+1][j].getState()==1)) 
                    {
                        forest[i+1][j]=2;
                        Print();
                    }

                if((forest[i][j].getState()==2) && (forest[i][j-1].getState()==1)) 
                    {
                        forest[i][j-1]=2;
                        Print();
                    }

                if((forest[i][j].getState()==2) && (forest[i-1][j].getState()==1)) 
                    {
                        forest[i-1][j]=2;
                        Print();
                    }           
            } 
    }  }
}

The class FireSimulator simulates the spread of a fire. A 2 represents a burning tree, a 1 represents a tree and a 0 represents an empty spot.This function checks on the neighbors of the current cell. If a tree is burning, and there's a tree next to it, then the tree next to it will burn. It needs to check on all the cells in the forest(array). I did it with 3 for loops but how to do it with recursion?

If you want to obtain exactly the same logic but without loops, you need to replace each loop with a recursive function. Then instead of a loop variable you'll have a function parameter. And do not forget to check for recursion termination condition in each function. I whipped up a quick solution just replacing your loops with recursion:

void CheckCellForIgnition(int col,int row) { // col and row designate a cell to check for ignition
    if (row < y - 1) {
        if ((forest[col][row]].getState() == 2) && (forest[col][row + 1].getState() == 1))
        {
            forest[col][row + 1] = 2;
            Print();
        }

        if ((forest[col][row].getState() == 2) && (forest[col + 1][row].getState() == 1))
        {
            forest[col + 1][row] = 2;
            Print();
        }

        if ((forest[col][row].getState() == 2) && (forest[col][row - 1].getState() == 1))
        {
            forest[col][row - 1] = 2;
            Print();
        }

        if ((forest[col][row].getState() == 2) && (forest[col - 1][row].getState() == 1))
        {
            forest[col - 1][row] = 2;
            Print();
        }
        CheckCellForIgnition(col, row + 1);
    }
}

void CheckColumnForIgnition(int col) { // col - column to check for ignition
    if (col < x - 1) {
        CheckCellForIgnition(col,1);
        CheckColumnForIgnition(col + 1);
    }
}

void IgniteIteration(int iterationsLeft) { // iterationsLeft - the number of iterations left to perform
    if (iterationsLeft>0) {
        CheckColumnForIgnition(1);
        IgniteIteration(iterationsLeft - 1);
    }
}

void spread()
{
    IgniteIteration(max(x, y));
}

The execution logic should be exactly the same as with your loop code. However, if your fire spreading logic is not so fixed, you may consider using recursion in another way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM