Problem : Find all possible paths through the array.
It is only allowed to go diagonally up or down, or go to the right. An example 4x4 matrix
`3  5  7  92  4  6  89  3  7  56  8  2  4`
The numbers in the matrix can be any arbitrary value. I would like to generate all possible routes through the matrix, starting in one of the four numbers in the first column. It is only allowed to move Northeast, East, and Southeast. An example route:
`3-5 7 9   \2 4 6-89 3 7 56 8 2 4`
Solution
`import java.util.*;class Paths{  static int arr[][] = {{3,5,7,9},{2,4,6,8},{9,3,7,5},{6,8,2,4}};      public static void main(String args[]){   for(int itr=0; itr<arr.length; itr++){      for(String path : new Paths().findPaths(itr, 0)){        if(path.split("->").length == 4)           System.out.println(path);       }   }  }  public ArrayList<String> findPaths(int row, int col){  ArrayList<String> paths = new ArrayList<String>();  if(goNorthEast(row,col)){    paths.add(arr[row][col]+"->"+arr[row-1][col+1]);    for(String path :  findPaths(row-1,col+1)){      paths.add(arr[row][col]+"->" + path);    }  }  if(goEast(row,col)){    paths.add(arr[row][col]+"->"+arr[row][col+1]);    for(String path :  findPaths(row,col+1)){      paths.add(arr[row][col]+"->" + path);    }  }  if(goSouthEast(row,col)){    paths.add(arr[row][col]+"->"+arr[row+1][col+1]);    for(String path :  findPaths(row+1,col+1)){      paths.add(arr[row][col]+"->" + path);    }  }  return paths; } public boolean goNorthEast(int row, int col){   if(row-1<0 || col+1 >= arr.length)     return false;   return true; } public boolean goEast(int row, int col){   if(col+1 >= arr.length)     return false;   return true; } public boolean goSouthEast(int row, int col){   if(row+1 >=arr.length || col+1 >= arr.length)     return false;   return true; }}`
`3->5->7->93->5->7->83->5->6->93->5->6->83->5->6->53->4->7->93->4->7->83->4->6->93->4->6->83->4->6->53->4->7->83->4->7->53->4->7->42->5->7->92->5->7->82->5->6->92->5->6->82->5->6->52->4->7->92->4->7->82->4->6->92->4->6->82->4->6->52->4->7->82->4->7->52->4->7->42->3->6->92->3->6->82->3->6->52->3->7->82->3->7->52->3->7->42->3->2->52->3->2->49->4->7->99->4->7->89->4->6->99->4->6->89->4->6->59->4->7->89->4->7->59->4->7->49->3->6->99->3->6->89->3->6->59->3->7->89->3->7->59->3->7->49->3->2->59->3->2->49->8->7->89->8->7->59->8->7->49->8->2->59->8->2->46->3->6->96->3->6->86->3->6->56->3->7->86->3->7->56->3->7->46->3->2->56->3->2->46->8->7->86->8->7->56->8->7->46->8->2->56->8->2->4`