Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- /*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 |__ |__|__|
- *
- * ***work this going from top down--from input to 0,0. when
- * 0 is reached that is a base case
- * n+ m = number of steps total
- (n+m)!/ (n!m!) is the total paths possible
- ex: (2,3) --> 5!/3!2!--> 5*4*3*2*1/3*2*1*2*1
- --> 5*2= 10 possible paths
- */
- class MyCode {
- public static void main(String[] args){
- int x = 10;
- int y = 10;
- System.out.println(countPaths(x,y));
- }
- public static int countPaths(int x, int y){
- if(x == 0 && y == 0){
- return 1; //only 1 way to start at start
- }
- if(x == -1 || y == -1){
- return 0; //not possible to be out of bounds
- }
- int countLeft = countPaths(x, y-1);// width/y-axis chgs
- int countUp = countPaths(x-1, y); // height/x-axis chgs
- return countUp + countLeft;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement