//version 3.0
//date 18.11.2007
//author SymTec
public class Proj1 {
//static variable declaration (vars will be accessible throughout the program)
static byte MaximumWay=5;
static byte[][] InitialArray = {
{1,2,0,0,0,0,0,0,0,0},
{2,0,0,0,0,0,0,0,0,0},
{1,4,3,0,0,0,0,0,0,0},
{2,4,0,0,0,0,0,0,0,0},
{2,3,5,0,0,0,0,0,0,0},
{4,0,0,0,0,0,0,0,0,0}};
static String returnPath = "";
static int numWays=0;
public static void main(String[] args) {
//variable declaration for the main method
byte i;
//go through first line and find the different paths from there
for(i=0;i<InitialArray[0].length;i++){
if (InitialArray[0][i]>0) {
new Proj1((byte)0,i,(byte)0,"");
}
}
//declaration of the return variable (makes more sense here than in the
//normal declaration part)
int[][] returnArray = new int[numWays][];
//in this loop, the path that had been stored as String is converted back
//to integers.
int start=0,stop;
String s;
for(int j=0;j<numWays;j++) {
stop=returnPath.indexOf((char)127,start);
s=returnPath.substring(start,stop);
int[] sArray = new int[s.length()];
for(i=0;i<s.length();i++){
sArray[i]=(int)s.charAt(i);
}
returnArray[j]=sArray;
start=stop+1;
}
//Debug output:
String str="Solutions to this problem:\n";
for(int k=0;k<returnArray.length;k++){
for(byte j=0;j<returnArray[k].length;j++){
str=str+returnArray[k][j]+" ";
}
str=str+"\n";
}
System.out.println(str);
//End Debug
}
public Proj1(byte line, byte entry, byte s, String path){
//variable declaration for this instance of method Proj1
byte news=(byte)(s+1),i,j;
byte number=InitialArray[line][entry];
String newpath=path+(char)number;
boolean EndOfBranch=true,LinkWorks;
//go through line, look for working links
for(i=0;i<InitialArray[line].length;i++){
LinkWorks=true;
//if link is zero, it does not work
if(InitialArray[number][i]==0) LinkWorks=false;
//if link is already somewhere in the path, it does not work
for(j=0;j<InitialArray.length;j++){
if(path.indexOf((char)InitialArray[number][i])>=0) LinkWorks=false;
}
//if link works, call next instance of Proj1.
//if there is a next instance, this obviously is not the end of this branch.
if((LinkWorks)&&(news<MaximumWay)){
new Proj1(number,i,news,newpath);
EndOfBranch=false;
}
}
//if no further instance has been called, add the path you took to get here
//to the list of possible solutions for this problem.
if (EndOfBranch) {
returnPath=returnPath+newpath+(char)127;
numWays++;
}
}
}