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));

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:

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:

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).