So the situation is:
1. There is NxN grid so it's a square grid
2. There will be a maximum amount of step that will be given
3. Every cell inside the grid have certain amount of value that will reduce the maximum amount of step
4. We can only move to right and below.
5. The starting point is top left of the grid and the goal is the bottom right of the grid.
6. We need to determine the longest path ( The one with least maximum amount of step left )
7. If there is no path possible the result will -1
So currently I already wrote a code that will work with some case but still not the optimum one.
What I'm doing right now are :
1. Check if the next right value and next below value.
2. Go to the bigger value.
3. If the Maximum amount of step become 0 backtrack to previous cell and move to another direction.
4. If the right value and below value is same I will check the next cell after the next cell.
Looks like the problem is with the 4th point.
this is my code for the 4th point :
private static int determineBestNext(int[][] grid, int currentX, int currentY) {
int nextRight = 0;
int nextBelow = 0;
int numberOfRows = grid.length - 1;
for(int i=currentX+1;i<numberOfRows-1;i++) {
nextRight += grid[currentY][i+1];
if(currentY != numberOfRows) {
nextRight += grid[currentY+1][i+1];
}
}
for(int i=currentY+1;i<numberOfRows-1;i++) {
nextBelow = grid[i+1][currentX];
if(currentX != numberOfRows) {
nextBelow += grid[i+1][currentX+1];
}
}
if(nextRight > nextBelow) {
return 1;
} else if (nextBelow > nextRight) {
return 2;
} else {
return determineBestNext(grid, currentX+1,currentY+1);
}
}
I guess the fallback is when the X is bigger than Y the number of step in Y is bigger so the chance Right value will be bigger than X and vice versa.
Do you guys have another idea? Thanks!
Thanks!