package de.sebhn.algorithm.excersise4;

import java.math.BigInteger;

/**
 * Prints all path to a certain n
 */
public class PathNodes {

  public static void main(String[] args) {
    int n = 2;
    BigInteger numberOfPaths = b(n, n, 0, "");
    System.out.println(numberOfPaths);
  }

  private static BigInteger b(int x, int y, int t, String s) {
    if (x < 0 || y < 0) {
      return BigInteger.ZERO;
    } else if (x == 0 && y == 0) {
      System.out.println(" " + s + "."); // print path
      return BigInteger.ONE;
    }

    BigInteger r = BigInteger.ZERO;

    if (y < x) {
      r = r.add(b(x - 1, y, 0, "R" + s)); // right
    }
    if (y <= x) {
      r = r.add(b(x, y - 1, 0, "U" + s)); // up
    }
    if (y >= x) {
      r = r.add(b(x - 1, y - 1, 0, "F" + s)); // upRight
    }
    if (y > x + 1 && t != 2) {
      r = r.add(b(x + 1, y - 1, 1, "L" + s)); // upLeft
    }
    if (y >= x && t != 1) {
      r = r.add(b(x - 1, y + 1, 2, "D" + s)); // downRight
    }

    return r;
  }

}

@@ -131,16 +131,50 @@
 odes %7B%0A%0A
+  private static int sumOfPaths;%0A%0A
   public
@@ -220,17 +220,37 @@
 int n = 
-2
+1;%0A    sumOfPaths = 0
 ;%0A    Bi
@@ -274,17 +274,30 @@
 Paths = 
-b
+calculatePaths
 (n, n, 0
@@ -342,16 +342,62 @@
 Paths);%0A
+    System.out.println(%22sum: %22 + sumOfPaths);%0A
   %7D%0A%0A  p
@@ -421,17 +421,30 @@
 Integer 
-b
+calculatePaths
 (int x, 
@@ -454,17 +454,20 @@
  y, int 
-t
+step
 , String
@@ -467,17 +467,20 @@
  String 
-s
+path
 ) %7B%0A    
@@ -562,24 +562,63 @@
 & y == 0) %7B%0A
+      sumOfPaths += path.length() + 1;%0A
       System
@@ -636,17 +636,20 @@
 n(%22 %22 + 
-s
+path
  + %22.%22);
@@ -714,17 +714,29 @@
 Integer 
-r
+numberOfPaths
  = BigIn
@@ -767,35 +767,72 @@
  x) %7B%0A      
-r = r.add(b
+numberOfPaths = numberOfPaths.add(calculatePaths
 (x - 1, y, 0
@@ -835,25 +835,28 @@
 y, 0, %22R%22 + 
-s
+path
 )); // right
@@ -886,27 +886,64 @@
 %7B%0A      
-r = r.add(b
+numberOfPaths = numberOfPaths.add(calculatePaths
 (x, y - 
@@ -950,25 +950,28 @@
 1, 0, %22U%22 + 
-s
+path
 )); // up%0A  
@@ -994,35 +994,72 @@
  x) %7B%0A      
-r = r.add(b
+numberOfPaths = numberOfPaths.add(calculatePaths
 (x - 1, y - 
@@ -1066,25 +1066,28 @@
 1, 0, %22F%22 + 
-s
+path
 )); // upRig
@@ -1112,25 +1112,28 @@
  %3E x + 1 && 
-t
+step
  != 2) %7B%0A   
@@ -1135,27 +1135,64 @@
 %7B%0A      
-r = r.add(b
+numberOfPaths = numberOfPaths.add(calculatePaths
 (x + 1, 
@@ -1203,25 +1203,28 @@
 1, 1, %22L%22 + 
-s
+path
 )); // upLef
@@ -1249,17 +1249,20 @@
 %3E= x && 
-t
+step
  != 1) %7B
@@ -1272,19 +1272,56 @@
     
-r = r.add(b
+numberOfPaths = numberOfPaths.add(calculatePaths
 (x -
@@ -1340,17 +1340,20 @@
 , %22D%22 + 
-s
+path
 )); // d
@@ -1379,17 +1379,29 @@
  return 
-r
+numberOfPaths
 ;%0A  %7D%0A%0A%7D

add sumOfPath and beautify code