Find minimum no of Dice roll require to win in given Snack and Ladder board



package javaFixer;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Program #7 Program To find minimum no of Dice roll required 
 * to win in given Snack and Ladder board 
 * 
 * @author JavaFixer
 *
 */
public class SnakeAndLadder {

 public static void main(String[] args) {
  int n = 100;
  int[] board = new int[n];
  
  for (int i = 0; i < n; i++) {
   board[i] = -1;
  }
  // Ladder
  board[3] = 20;
  board[4] = 2;
  board[19] = 55;
  board[25] = 86;
  board[38] = 48;
  board[47] = 91;
 
  // Snakes
  board[98] = 8;
  board[88] = 28;
  board[70] = 60;
  board[55] = 41;
  board[37] = 7;
  board[25] = 10;
        
  System.out.println("Min Dice Roll required to win: "
           +getMinStepToWin(board,n));
  
 }
 
 static int getMinStepToWin(int[] board, int n){
  int[] visited = new int[n];
  for(int i:visited){
   visited[i] = 0;
  }
  
  CustomNode c = new CustomNode();
  c.node = 0;
  c.distance = 0;
  Queue<CustomNode> q = new LinkedList<CustomNode>();
  q.add(c);
  visited[0] = 1;
  
  while(!q.isEmpty()){
   c = q.remove();
   int currNode = c.node;
   int currDist = c.distance;
   
   if(currNode==n-1) {
    break;
   }
   
   for(int i=(currNode+1); i<=(currNode+6) && i<n;++i) {
    if(visited[i]==0) {
     
     CustomNode temp1 = new CustomNode();
     if(board[i]!=-1) {
      temp1.node = board[i];
      temp1.distance = currDist+1;
     }else {
      temp1.node = i;
      temp1.distance = currDist+1;
     }
     visited[i]=1;
     q.add(temp1);
    }    
   }
   
  }
  
  return c.distance;
  
 }
 
 static class CustomNode{
  int node;
  int distance;
 }

}


Min Dice Roll required to win: 8

Program to Find given No is Fancy or not


package javaFixer;

/**
 * Program #6 Program To find give number is Fancy or not. 
 * 
 * Number is Fancy if it satisfy any one of below conditions:
 * 1) 4 Continuous Same digit Ex. 1111,9999 etc.
 * 2) 4 Continuous digit Increment by 1 Ex. 2345,6789 etc.
 * 3) 4 Continuous digit Decrement by 1 Ex. 4321,8765 etc.
 * 
 * @author JavaFixer
 *
 */
public class FancyNumber {
 
 public static void main(String[] args) {
  Long a = 12343215555L;// Given No
  char[] c = a.toString().toCharArray();
  char rN = c[0];
  int rCount = 1;
  char aN = c[0];
  int aCount = 1;
  char dN = c[0];
  int dCount = 1;
  boolean isFancy = false;
  for (int i = 1; i < c.length; i++) {
   
   // Condition 1
   if (rN == c[i]) {
    rCount++;
    if (rCount == 4) {
     System.out.println("Fancy with " + c[i - 3] + c[i - 2] + c[i - 1] + c[i]);
     isFancy = true;
    }
   } else {
    rN = c[i];
    rCount = 1;
   }
   
   // Condition 2
   if ((aN + 1) == c[i]) {
    aCount++;
    aN = c[i];
    if (aCount == 4) {
     System.out.println("Fancy with " + c[i - 3] + c[i - 2] + c[i - 1] + c[i]);
     isFancy = true;
    }
   } else {
    aN = c[i];
    aCount = 1;
   }
   
   // Condition 3
   if ((dN - 1) == c[i]) {
    dCount++;
    dN = c[i];
    if (dCount == 4) {
     System.out.println("Fancy with " + c[i - 3] + c[i - 2] + c[i - 1] + c[i]);
     isFancy = true;
    }
   } else {
    dN = c[i];
    dCount = 1;
   }
  }
  if (isFancy)
   System.out.println("Given No is FANCY");
  else
   System.out.println("Given No is not FANCY");
 }
}


Fancy with 1234
Fancy with 4321
Fancy with 5555
Given No is FANCY

Find all combination which follow Pythagorean theorem for given Hypotenuse Length


package javaFixer;

/**
 * Program #5 Program To find all combination of sides which follow Pythagorean
 * Theorem for given Hypotenuse length
 * 
 * @author JavaFixer
 *
 */
public class Pythagorean {

 public static void main(String[] args) {
  int n = 100; // Length of Hypotenuse

  for (int i = 0; i < n; i++) {
   for (int j = i; j < n; j++) {
    if ((i * i) + (j * j) == (n * n)) {
     System.out.println(i + "-" + j + "-" + n);
    }
   }
  }
 }
}


28-96-100
60-80-100

Program to Print Pascal Triangle


package javaFixer;

/**
 * Program #4 Program To Print Pascal Triangle
 * @author JavaFixer
 *
 */
public class PascalTriangle {

 public static void main(String[] args) {
  int n = 5;// No of Rows
  for (int i = 0; i < n; i++) {
   for (int j = n - 1; j > i; j--) {
    System.out.print(" ");
   }
   for (int j = 0; j <= i; j++) {
    System.out.print(getPascal(i, j) + " ");
   }
   System.out.println();
  }
 }

 private static int getPascal(int i, int j) {
  if (j == 0 || j == i) {
   return 1;
  }
  return getPascal(i - 1, j - 1) + getPascal(i - 1, j);
 }
}


    1 
   1 1 
  1 2 1 
 1 3 3 1 
1 4 6 4 1 

Program to Solve Tower of Hanoi Problem


package javaFixer;

/**
 * Program #3 Program To Solve Tower of Hanoi
 * @author JavaFixer
 *
 */
public class TowerOfHanoi {
 public static void main(String[] args) {
  int n = 3;//No of Disk
  String start = "A";
  String mid = "B";
  String end = "C";
  getStep(n,start,mid,end);
 }

 private static void getStep(int n, String start, String mid, String end) {
  if(n==1){
   System.out.println(start+"->"+end);
  }
  else {
   getStep(n-1,start,end,mid);
   System.out.println(start+"->"+end);
   getStep(n-1,mid,start,end);
  }
 }
}


A->C
A->B
C->B
A->C
B->A
B->C
A->C