You are in a field of sugar cane, represented as a grid array. You will represent a maze of paths through the field using a 2 dimensional array (details provided later). At some location in the field, there is a Dog. At another place in the field, there is at least one bone. The problem is for you to explore the paths recursively to find a path from the Dog to a Bone.
The following strategies are to be followed:
Using the power of recursion, you will implement a method: gridSearch() to explore possible paths through the maze of paths. As you explore the field, you will print (to screen or output file) helpful messages to explain what you are doing (sample output below). When you have found a path, you will print the successful path (in reverse order).
The field grid will be a 2 dimensional character array with at most 12 rows and 12 columns, char **table = new char*[12];
for(int i = 0; i < n; i++){ table[i] = new char[12]; }
You are provided with the following test grid arrays that you will use (at least) to test your algorithm. The first test case is shown here: See image.
Legend:
The algorithm for your main() function:
The recursive algorithm is to try to move one position, then from the new position recursively search for a valid path from that position, if fail, backtrack out of the recursion and try a different position. More details are below. When you find a bone, stop, print the location of the found bone, print the path and exit.
The algorithm for your recursive gridSearch function:
Parameters for your gridSearch function:
int Grid::gridSearch (int startx, int starty, char *direction)
Other functions:
When designing your solution, please consider good design principles. You will need to write some functions and methods to help organize your code and enable code reuse. You are expected to (at least) include the following functions:
Your Output:
Here is Sample output for a very small 4 rows x 4 columns test grid: See image.
You should complete your project in stages as follows. After completing each stage, you are encouraged to discuss your answer with your tutor for feedback, before continuing to the next stage.
Given the algorithm above, draw on paper a simple maze grid and work through how the algorithm works using recursion. What is stored at each step? Why do you need to ‘leave a trail of #s’? Elaborate detailed algorithms for your solution, for each method you think you need. Think about the objects you will need. For each objects, what methods are needed for input/output? What methods will you need to search through the field? How do you calculate what ‘look right’ will mean? Write up your design in a text/word document.
Data structures – think about the details, elaborate on your design. How will you represent your grid? Devise your Program, include comments to ‘tell the story’ of how your code works. Write a main function that creates a grid object, sets values into the grid (‘-’, ‘0’, ‘B’ ,’D’), prints the grid as per the sample output provided to an output file. Write methods for your grid object to create grid, print the grid, get the grid size. Write your recursive method to search the field grid.
Devise appropriate test cases to verify your solution is correct. Test for a larger grid up to 12x12 cells Review your original algorithm, update it if necessary.