Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- input: int m, int n-> integers of a lattice where m = height and n = width of lattice,
- output: number or ways/paths to get from 0,0 to bottom right
- example: input: 2,3
- output :10
- (2 x 3 lattice of squares)
- * 0 __1 _2 __3
- * 1 |__ |__|__|
- * 2 |__ |__|__|
- import java.io.*;
- class MyCode {
- public static void main (String[] args) {
- int m = 10;
- int n = 10;
- //where m = height and n = width
- System.out.println(latticePaths(m,n));
- }
- public static int latticePaths(int m, int n) {
- //approach going from input to 0,0
- //if input is 0,0 -> return 1
- if(n == 0 && m == 0){
- return 1;
- }
- //check to see that neither n /m are out of bounds
- //if either is out of bounds then return 0
- //as there is no way to get to -1 in any case
- if(n == -1 || m == -1){
- return 0;
- }
- //decrement m (go upwards)
- int recurseUp = latticePaths(m-1, n);
- //decrement n (go to the left)
- int recurseLeft = latticePaths(m, n-1);
- //get sum of both recursions and return
- return recurseUp + recurseLeft;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement