CS 111- Introduction to Computer Science Homework #12

Due: beginning of class Friday 4/21

1. Recursive Choose: In class `Choose`, create a method `public static long choose(int n, int k)` that computes the choose function ("n choose k") according to the recursive formula

Add the following main method to Choose for testing:

```	public static void main(String[] args) {
for (int n = 0; n < 10; n++) {
for (int k = 0; k <= n; k++)
System.out.printf("%d\t", choose(n, k));
System.out.println();
}
}
```

Optional challenge: Use a dynamic programming approach to store (i.e., "cache" or "memoize") each computed choose result so as to avoid redundant recomputation of results.  Observe the significant speedup in performance for values beyond the test code, e.g. n = 30, k = 15.

2. Maze Solving: Beginning with this Maze.java starter code, we will implement makeMaze() together in class.  For this assignment, you will implement the public void solveMaze() method and its recursive helper/auxialiary method private boolean solveMaze(int row, int col) according to this algorithm:

• Set grid[row][col] equal to SOLUTION_PATH.
• Base case: If we're in the bottom-right corner, return true.
• Recursive case:
• For each direction
• If we can travel from [row][col] to [row2][col2] in that direction and it's not already on the SOLUTION_PATH,
• If we can solve the maze from [row2][col2], return true.
• Failing to find a solution, we set grid[row][col] back to REACHED and return false.

Note: The default call stack size is limited, so if you want to make a truly eye-popping letter-size maze, run this class from the terminal window with "java -Xss4m Maze" for increased call stack size and use 380 rows and 285 columns.  Here are example maze and maze solution image files generated from correct code.

3. N Queens Puzzle: The n-Queens Puzzle is to place n Chess queens on an n-by-n board in such a way that no two queens can attack one another according to the rules of Chess.  Beginning with this NQueensPuzzle.java starter code, you will implement the private boolean solve(int row) according to this algorithm:

• If the row is equal to the number of queens, we've placed all queens and found a solution, so return true.
• For each column in the given row in increasing order:
• Check each preceding row queen placement to see if that queen would conflict with (i.e. attack) this row and column.
• If there is no conflict, place this row's queen in this column and recursively seek to solve a placement for the next rows.
• If that solution attempt is successful (i.e. the recursive solve call returns true), return true.
• If no column placement led to a successful solution, return false.

Hint: Since we're placing only one queen per row, the only possible conflicts are

1. two queens sharing the same column, and
2. two queens sharing a same diagonal (i.e. the absolute value of their row difference is equal to the absolute value of their column difference).