I need to calculate the minimum number of jumps to reach the end of an Array with dice throw.
Array values may be negative/positive:
- When positive - move forward
- When negative - go back
The array may also contain an R value, which means that the player have to throw the dice again.
The start position is marked on our array with S and end position with E. The start position is not always the first element of the array and the end position is not always at the end (it can even be before S).
Example:
Array = {4, S, -2,1, R, 4,3,4,3,-5,2,-4, E}
The player starts on the S position the fastest way to reach E:
- Throwing the dice to have 3 and reach the R case (first move)
- Throwing the dice again and having 6 to reach the 2 case (second movement)
- Jumping 2 cases to reach E (third move)
So the best solution for this example is: 3 moves
I'm using a BufferReader
to read files that contain my array elements. I wrote this working solution but I need some advice to improve my code.
public static int compute(BufferedReader br) throws IOException{
//Retriving Total node Number
final int TotalCaseCount = GetCaseCount(br);
//pushing Bufferreader to an Array
final String CaseArray[]=new String[TotalCaseCount];
BufferToArray(br, CaseArray);
//Catching Start Position
StartPosition=GetStartPosition(CaseArray);
//Array to mark Visited node statut
final boolean visited[ ]=new boolean[CaseArray.length];
int MinimumNumberOfMove = 0;
//List that will contain all reachable route
final List<Integer> ReachableList = new ArrayList<Integer>();
//Calculating all reachable route
for (int i=1;i<=6;i++)
{
VisitedInitilise(visited);
if(StartPosition+i<TotalCaseCount&&StartPosition+i>0)
{
GetMinimumNumberOfMoves(CaseArray,visited, StartPosition+i,0,ReachableList);
}
}
//Retriving Minimum number of move from all reachble route
if(ReachableList.isEmpty())
{
MinimumNumberOfMove=Constants.IMPOSSIBLE;
}
else
{
MinimumNumberOfMove=ReachableList.get(0);
for (int i=0;i<ReachableList.size();i++)
{
if(ReachableList.get(i)<MinimumNumberOfMove)
{
MinimumNumberOfMove=ReachableList.get(i);
}
}
}
return MinimumNumberOfMove;
}
static List<Integer> GetMinimumNumberOfMoves( String[] Plateau,boolean[] visited ,final int current_index, int Current_N_of_moves, List<Integer> List )
{
Boolean EndIsReached=false;
Boolean Impossible=false;
visited[StartPosition]=true;
//Checking if the current index index is negativ
if(current_index<0)
{
Impossible=true;
}
while(EndIsReached==false&&Impossible==false&&visited[current_index]==false&¤t_index<Plateau.length)
{
try {
switch (Plateau[current_index]){
case "E":
{
//if end is reached , pushing number of move into our list
EndIsReached=true;
List.add(Current_N_of_moves+1);
break;
}
case "R":
{
//Marking node as visited
visited[current_index]=true;
for (int i=1;i<=6;i++)
{
//Marking all case after R case as non visited
for(int j=current_index+1;j<visited.length;j++)
{
visited[j]=false;
}
//Calculating number of move after R case
if(current_index+i<Plateau.length&¤t_index>0)
{
GetMinimumNumberOfMoves(Plateau,visited, current_index+i,Current_N_of_moves+1,List);
}
}
break;
}
default:
{
//Cheking if node was already visited
if(visited[current_index]==true)
{
//Marking all node as non visited
VisitedInitilise(visited);
Impossible=true;
break;
}
else
{
//when the node was not visited before , catch the jump value
int JumpValue=Integer.parseInt( Plateau[current_index]);
//cheking that the next node is not bigger than node number and not negativ
if(current_index+JumpValue>Plateau.length||current_index<0)
{
Impossible=true;
break;
}
else
{
//Marking node as visited
visited[current_index]=true;
//calculating minimum number of move starting from this node
GetMinimumNumberOfMoves(Plateau,visited,current_index + JumpValue, Current_N_of_moves + 1,List);
break;
}
}
}
}
}
catch (NumberFormatException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
if(Impossible==true)
{
Current_N_of_moves=0;
}
return List;
}
//A Method to count total Node
static int GetCaseCount (BufferedReader br)
{
int i = 0;
try
{
i = Integer.parseInt(br.readLine());
}
catch (NumberFormatException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return i;
}
//A method to push bufferreader to String Array
static void BufferToArray(BufferedReader br,String[] Plateau)
{
String line;
int i = 0;
try
{
while((line=br.readLine())!=null)
{
Plateau[i]=line;
i++;
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//A method to mark all node as non visited
static void VisitedInitilise(boolean visited[])
{
for(int i =0;i<=visited.length-1;i++)
{
visited[i]=false;
}
}
//A method to Get the S Position
static int GetStartPosition(String[] Plateau)
{
int Startindex = 0;
for(int i=0;i<=Plateau.length-1;i++)
{
if( Plateau[i].equals("S"))
{
Startindex=i;
break;
}
}
return Startindex;
}