mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-07 01:23:56 -05:00
Fixed issue with problem 15 not saving number of routes
This commit is contained in:
@@ -29,13 +29,14 @@ public class Problem15 extends Problem{
|
|||||||
private static final Integer WIDTH = 20;
|
private static final Integer WIDTH = 20;
|
||||||
//The height of the box to traverse
|
//The height of the box to traverse
|
||||||
private static final Integer LENGTH = 20;
|
private static final Integer LENGTH = 20;
|
||||||
|
//The number of routes from 0, 0 to 20, 20
|
||||||
|
private Long numOfRoutes = 0L;
|
||||||
|
|
||||||
public Problem15(){
|
public Problem15(){
|
||||||
super("How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?");
|
super("How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?");
|
||||||
}
|
}
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//Setup the rest of the variables
|
//Setup the rest of the variables
|
||||||
Long numOfRoutes = 0L; //The number of routes from 0, 0, to 20, 20
|
|
||||||
Integer currentX = 0; //The current x location on the grid
|
Integer currentX = 0; //The current x location on the grid
|
||||||
Integer currentY = 0; //The current y location on the grid
|
Integer currentY = 0; //The current y location on the grid
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ public class Problem15 extends Problem{
|
|||||||
|
|
||||||
//We write this as a recursive function
|
//We write this as a recursive function
|
||||||
//When in a location it always moves right first, then down
|
//When in a location it always moves right first, then down
|
||||||
move(currentX, currentY, numOfRoutes);
|
move(currentX, currentY);
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
@@ -54,7 +55,7 @@ public class Problem15 extends Problem{
|
|||||||
}
|
}
|
||||||
//This function acts as a handler for moving the position on the grid and counting the distance
|
//This function acts as a handler for moving the position on the grid and counting the distance
|
||||||
//It moves right first, then down
|
//It moves right first, then down
|
||||||
private static void move(Integer currentX, Integer currentY, Long numOfRoutes){
|
private void move(Integer currentX, Integer currentY){
|
||||||
//Check if you are at the end and act accordingly
|
//Check if you are at the end and act accordingly
|
||||||
if((currentX == WIDTH) && (currentY == LENGTH)){
|
if((currentX == WIDTH) && (currentY == LENGTH)){
|
||||||
++numOfRoutes;
|
++numOfRoutes;
|
||||||
@@ -63,12 +64,12 @@ public class Problem15 extends Problem{
|
|||||||
|
|
||||||
//Move right if possible
|
//Move right if possible
|
||||||
if(currentX < WIDTH){
|
if(currentX < WIDTH){
|
||||||
move(currentX + 1, currentY, numOfRoutes);
|
move(currentX + 1, currentY);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Move down if possible
|
//Move down if possible
|
||||||
if(currentY < LENGTH){
|
if(currentY < LENGTH){
|
||||||
move(currentX, currentY + 1, numOfRoutes);
|
move(currentX, currentY + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user