基础的C++入门练习题,完成五个函数的编写,以及一个迷宫寻路小程序。
Problem 1
Here are five functions, with descriptions of what they are supposed to do. They are incorrectly implemented. Replace the incorrect implementations of these functions with correct ones that use recursion in a useful way; your solution must not use the keywords while, for, or goto. You must not use global variables or variables declared with the keyword static, and you must not modify the function parameter lists. You must not use any references or pointers as parameters except for the parameters representing arrays.
1 | // Returns the product of two positive integers, m and n, |
Problem 2
Write a C++ function named pathExists that determines whether or not a theres a path from start to finish in a rectangular maze. Here is the prototype:
1 | bool pathExists(string maze[], int nRows, int nCols, int sr, int sc, int er, int ec); |
The parameters are
- A rectangular maze of Xs and dots that represents the maze. Each string of the array is a row of the maze.
- Each X represents a wall, each @ represents a bottomless trap hole, and each . represents a walkway.
- The number of rows in the maze.
- The number of columns in the maze. Each string in the maze parameter must be this length.
- The starting coordinates in the maze: sr, sc; the row number is in the range 0 through nRows-1, and the column number is in the range 0 through nCols-1.
- The ending coordinates in the maze: er, ec; the row number is in the range 0 through nRows-1, and the column number is in the range 0 through nCols-1.
Here is an example of a simple maze with 5 rows and 7 columns:
"XXXXXXX""X...X@X""XXX.X.X""[email protected]""XXXXXXX"
The function must return true if in the maze as it was when the function was called, there is a path from maze[sr][sc] to maze[er][ec] that includes only walkways, no walls or bottomless trap holes; otherwise it must return false. The path may turn north, east, south, and west, but not diagonally. When the function returns, it is allowable for the maze to have been modified by the function.
(Our convention is that (0,0) is the northwest (upper left) corner, with south (down) being the increasing r direction and east (right) being the increasing c direction.)
Here is pseudocode for your function:
If the start location is equal to the ending location, then we'vesolved the maze, so return true.Mark the start location as visted.For each of the four directions,If the location one step in that direction (from the startlocation) is unvisited,then call pathExists starting from that location (andending at the same ending location as in thecurrent call).If that returned true,then return true.Return false.
(If you wish, you can implement the pseudocode for loop with a series of four if statements instead of a loop.)
Here is how a client might use your function:
1 | int main() |
Because the focus of this homework is on practice with recursion, we wont demand that your function be as robust as we normally would. In particular, your function may make the following simplifying assumptions (i.e., you do not have to check for violations):
- the maze array contains nRows rows (you couldnt check for this anyway);
- each string in the maze is of length nCols;
- the maze contains only Xs, @s, and dots when passed in to the function;
- the top and bottom rows of the maze contain only Xs, as do the left and right columns;
- sr and er are between 0 and nRows-1, and sc and ec are between 0 and nCols-1;
- maze[sr][sc] and maze[er][ec] are . (i.e., not walls or bottomless trap holes)
(Of course, since your function is not checking for violations of these conditions, make sure you dont pass bad values to the function when you test it.)