patternjavaMinor
Minimum number of moves to reach the end
Viewed 0 times
thenumbermovesreachminimumend
Problem
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:
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:
So the best solution for this example is: 3 moves
I'm using a
```
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 ReachableList = new ArrayList();
//Calculating all reachable route
for (int i=1;i0)
{
GetMinimumNumberOfMoves(CaseArray,visited, StartPosition+i,0,ReachableList);
}
}
//Retriving Minimum number of move from all reachble route
if(ReachableList.isEmpty())
{
MinimumNumberOfMove=Constants.IMPOSSIBLE;
}
else
{
MinimumNumberOfMov
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 ReachableList = new ArrayList();
//Calculating all reachable route
for (int i=1;i0)
{
GetMinimumNumberOfMoves(CaseArray,visited, StartPosition+i,0,ReachableList);
}
}
//Retriving Minimum number of move from all reachble route
if(ReachableList.isEmpty())
{
MinimumNumberOfMove=Constants.IMPOSSIBLE;
}
else
{
MinimumNumberOfMov
Solution
A few things to get started:
-
try to use self-documenting code. Instead of:
why not write:
and get rid of the comment?
-
indent your code properly and put opening braces on the same line (Java convention):
would look better like this:
- follow the language convention: in Java, variables and methods start in lower case:
final int TotalCaseCount = GetCaseCount(br);should befinal int totalCaseCount = getCaseCount(br);- as a side effect, once you do that, the auto-formatting on this site will work better.
-
try to use self-documenting code. Instead of:
//Retriving Total node Number
final int totalCaseCount = getCaseCount(br);why not write:
final int totalCaseCount = getTotalNodeNumber(br);and get rid of the comment?
-
indent your code properly and put opening braces on the same line (Java convention):
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;
}would look better like this:
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;
}Code Snippets
//Retriving Total node Number
final int totalCaseCount = getCaseCount(br);final int totalCaseCount = getTotalNodeNumber(br);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;
}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;
}Context
StackExchange Code Review Q#123232, answer score: 3
Revisions (0)
No revisions yet.