logo CodeStepByStep logo

xRegions

Language/Type: C++ Grid traversals
Related Links:

Write a function named xRegions that accepts a reference to a Grid of characters as a parameter, where every character in the grid is guaranteed to be either an uppercase 'X' or dash '-', and modify its state so that any dashes or groups of dashes that are entirely surrounded by Xes in the original grid will be changed to 'X' themselves. For example, suppose the grid contains the following characters:

Grid<char> board {
    {'X', 'X', 'X', 'X', 'X', 'X', '-'},
    {'X', '-', '-', '-', 'X', 'X', 'X'},
    {'X', 'X', 'X', '-', 'X', 'X', 'X'},
    {'X', '-', 'X', '-', 'X', '-', 'X'},
    {'X', 'X', 'X', 'X', 'X', '-', 'X'},
    {'X', '-', '-', 'X', 'X', '-', 'X'},
    {'X', '-', '-', 'X', 'X', 'X', 'X'}
};

Then the call of xRegions(board); should modify it to store the following:

{
    {'X', 'X', 'X', 'X', 'X', 'X', '-'},
    {'X', 'X', 'X', 'X', 'X', 'X', 'X'},
    {'X', 'X', 'X', 'X', 'X', 'X', 'X'},
    {'X', 'X', 'X', 'X', 'X', 'X', 'X'},
    {'X', 'X', 'X', 'X', 'X', 'X', 'X'},
    {'X', '-', '-', 'X', 'X', 'X', 'X'},
    {'X', '-', '-', 'X', 'X', 'X', 'X'}
}

Notice that two groups of dashes are not changed to 'X': the ones that touch the top-right and bottom borders of the grid.

Your code should work for a board of any size, even one with 0 rows or columns. You may assume that no characters appear in the board other than 'X' and '-'.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.