Homework 12

CS 111- Introduction to Computer Science
Homework #12

Due: beginning of class Friday 4/21

1. Recursive Choose: In class , create a method 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).

Homework #12

Reminder - you're going to need these webpages: http://www.continuummechanics.org/techforms/index.html to do this homework.... unless you prefer to use Matlab, Mathematica, etc. Don't bother doing anything by hand anymore. Take advantage of software programs to do all the matrix multiplication and other procedures when you have a chance.

Given the following stress tensor

\[ \boldsymbol{\sigma} = \left[ \matrix{ 10 & 20 & 30 \\ 20 & 40 & 50 \\ 30 & 50 & 60 } \right] \]
  1. What is the value of the von Mises stress?

  2. Propose 2 other stress tensors that will have the same von Mises stress?

  3. Do all stress tensors having the same von Mises stress also have the same principal stresses?

  4. Do all stress tensors having the same principal stresses also have the same von Mises stress?

  5. Start with the relationship between Caucy stress and 2nd Piola-Kirchhoff stress and take the time derivative to figure out the relationship between \( \dot{\boldsymbol{\sigma}} \) and \( \dot{\boldsymbol{\sigma}}^\text{PK2} \).
    Hint - You will get a Lie derivative along the way.

    \[ \boldsymbol{\sigma} = {1 \over J} \, {\bf F} \cdot \boldsymbol{\sigma}^\text{PK2} \cdot {\bf F}^T \]

0 Replies to “Homework 12”

Lascia un Commento

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *