Advertisement
Guest User

Prob15

a guest
Jan 26th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. public class Problem15 {
  2.  
  3.     public static void main(String[] args) {
  4.         //long startTime = System.currentTimeMillis();
  5.         System.out.println(lattice(20));
  6.         //double elapseTime = (double)(System.currentTimeMillis() - startTime)/1000.0;
  7.         //System.out.println(elapseTime);
  8.     }
  9.    
  10.     static long lattice(int size){
  11.         long[][] lattice = new long[size+1][size+1];
  12.         boolean firstIteration = true;
  13.        
  14.         for(int i = 0; i<=size;i++)
  15.             lattice[0][i] = 1;
  16.        
  17.         for(int y = 1; y<=size;y++){
  18.             firstIteration = true;
  19.             for(int x = y; x <=size; x++){
  20.                 if(firstIteration) {
  21.                     lattice[y][x] = lattice[y-1][x] *2;
  22.                     firstIteration = false;
  23.                 }else {
  24.                     lattice[y][x] = lattice[y][x-1] + lattice[y-1][x];
  25.                 }
  26.             }
  27.         }
  28.         return lattice[size][size];
  29.     }
  30.    
  31.     @Deprecated
  32.     static long latticeRecurse(int size, int dir, int step, long counter){
  33.         if (step == size*2 && dir == 0)
  34.             return counter+1;
  35.        
  36.         if ( dir < size && step < size*2 && ((step+1)*(dir+1)) <= (size*size) )
  37.             counter = latticeRecurse(size,dir+1,step+1,counter);
  38.        
  39.         if( dir > -size && step < size*2 && ((step+1)*(dir-1)) >= -(size*size) )
  40.             counter = latticeRecurse(size,dir-1,step+1,counter);
  41.        
  42.         return counter;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement