Find the longest path in a matrix where each step has entries that differ by 1

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
Given an N*N matrix that all numbers are distinct in it, the function should find the maximum length path (starting from any cell) such that all the cells along the path are in increasing order with a difference of 1.
It can move in 4 directions from the given cell (i, j), It can move to (i-1, j) or (i, j-1) or (i+1, j) or (i, j+1) with the condition that the adjacent cells have a difference of 1.
I wrote the following code that works in all the cases that I have checked.
I know that this is not optimal writing, with your permission, I would love to hear insights and ways to shorten the code. The program must be pure recursion without loops at all.
I'm not satisfied with the form and structure of my code because I went through the recursion step by step over all the options. I'm trying to write this in the form of backtracking with markings cells and stop conditions.
In the original question, the complexity of place, time, and the code are irrelevant.
At the moment, the discussion deals with the complexity of the code. I want to write it in a shorter and more elegant way while implementing backtracking.
public static int longestWorm(int mat)
return longestWorm(mat, 0,0,0);
private static int longestWorm(int mat,int i, int j, int max)
if (i == mat.length) return max;
if (j == mat[i].length-1)
return longestWorm(mat, i + 1, 0, max);
if (wormCount(mat,i,j,0) > max)
max = wormCount(mat,i,j,0);
return longestWorm(mat, i, j + 1, max);
private static int wormCount(int mat, int i, int j,int count)
if(i < mat.length-1 && mat[i][j] == mat[i+1][j]+1)
return 1+ wormCount(mat, i+1, j, count+1);
if(j < mat[i].length-1 && mat[i][j] == mat[i][j+1]+1)
return 1+ wormCount(mat, i, j+1, count+1);
if(i > 0 && mat[i][j] == mat[i-1][j]+1)
return 1+ wormCount(mat, i-1, j, count+1);
if(j > 0 && mat[i][j] == mat[i][j-1]+1)
return 1+ wormCount(mat, i, j-1, count+1);
return count;
java recursion matrix pathfinding
add a comment |Â
up vote
2
down vote
favorite
Given an N*N matrix that all numbers are distinct in it, the function should find the maximum length path (starting from any cell) such that all the cells along the path are in increasing order with a difference of 1.
It can move in 4 directions from the given cell (i, j), It can move to (i-1, j) or (i, j-1) or (i+1, j) or (i, j+1) with the condition that the adjacent cells have a difference of 1.
I wrote the following code that works in all the cases that I have checked.
I know that this is not optimal writing, with your permission, I would love to hear insights and ways to shorten the code. The program must be pure recursion without loops at all.
I'm not satisfied with the form and structure of my code because I went through the recursion step by step over all the options. I'm trying to write this in the form of backtracking with markings cells and stop conditions.
In the original question, the complexity of place, time, and the code are irrelevant.
At the moment, the discussion deals with the complexity of the code. I want to write it in a shorter and more elegant way while implementing backtracking.
public static int longestWorm(int mat)
return longestWorm(mat, 0,0,0);
private static int longestWorm(int mat,int i, int j, int max)
if (i == mat.length) return max;
if (j == mat[i].length-1)
return longestWorm(mat, i + 1, 0, max);
if (wormCount(mat,i,j,0) > max)
max = wormCount(mat,i,j,0);
return longestWorm(mat, i, j + 1, max);
private static int wormCount(int mat, int i, int j,int count)
if(i < mat.length-1 && mat[i][j] == mat[i+1][j]+1)
return 1+ wormCount(mat, i+1, j, count+1);
if(j < mat[i].length-1 && mat[i][j] == mat[i][j+1]+1)
return 1+ wormCount(mat, i, j+1, count+1);
if(i > 0 && mat[i][j] == mat[i-1][j]+1)
return 1+ wormCount(mat, i-1, j, count+1);
if(j > 0 && mat[i][j] == mat[i][j-1]+1)
return 1+ wormCount(mat, i, j-1, count+1);
return count;
java recursion matrix pathfinding
Why are you restricted to using pure recursion? Is this a homework problem?
â 200_success
Mar 2 at 21:16
I have an exam in Java soon and I decided to write every recursive function of every exam since 2005 to the last semester, this question has appeared in one of the exams.
â BOIDEM
Mar 2 at 21:31
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Given an N*N matrix that all numbers are distinct in it, the function should find the maximum length path (starting from any cell) such that all the cells along the path are in increasing order with a difference of 1.
It can move in 4 directions from the given cell (i, j), It can move to (i-1, j) or (i, j-1) or (i+1, j) or (i, j+1) with the condition that the adjacent cells have a difference of 1.
I wrote the following code that works in all the cases that I have checked.
I know that this is not optimal writing, with your permission, I would love to hear insights and ways to shorten the code. The program must be pure recursion without loops at all.
I'm not satisfied with the form and structure of my code because I went through the recursion step by step over all the options. I'm trying to write this in the form of backtracking with markings cells and stop conditions.
In the original question, the complexity of place, time, and the code are irrelevant.
At the moment, the discussion deals with the complexity of the code. I want to write it in a shorter and more elegant way while implementing backtracking.
public static int longestWorm(int mat)
return longestWorm(mat, 0,0,0);
private static int longestWorm(int mat,int i, int j, int max)
if (i == mat.length) return max;
if (j == mat[i].length-1)
return longestWorm(mat, i + 1, 0, max);
if (wormCount(mat,i,j,0) > max)
max = wormCount(mat,i,j,0);
return longestWorm(mat, i, j + 1, max);
private static int wormCount(int mat, int i, int j,int count)
if(i < mat.length-1 && mat[i][j] == mat[i+1][j]+1)
return 1+ wormCount(mat, i+1, j, count+1);
if(j < mat[i].length-1 && mat[i][j] == mat[i][j+1]+1)
return 1+ wormCount(mat, i, j+1, count+1);
if(i > 0 && mat[i][j] == mat[i-1][j]+1)
return 1+ wormCount(mat, i-1, j, count+1);
if(j > 0 && mat[i][j] == mat[i][j-1]+1)
return 1+ wormCount(mat, i, j-1, count+1);
return count;
java recursion matrix pathfinding
Given an N*N matrix that all numbers are distinct in it, the function should find the maximum length path (starting from any cell) such that all the cells along the path are in increasing order with a difference of 1.
It can move in 4 directions from the given cell (i, j), It can move to (i-1, j) or (i, j-1) or (i+1, j) or (i, j+1) with the condition that the adjacent cells have a difference of 1.
I wrote the following code that works in all the cases that I have checked.
I know that this is not optimal writing, with your permission, I would love to hear insights and ways to shorten the code. The program must be pure recursion without loops at all.
I'm not satisfied with the form and structure of my code because I went through the recursion step by step over all the options. I'm trying to write this in the form of backtracking with markings cells and stop conditions.
In the original question, the complexity of place, time, and the code are irrelevant.
At the moment, the discussion deals with the complexity of the code. I want to write it in a shorter and more elegant way while implementing backtracking.
public static int longestWorm(int mat)
return longestWorm(mat, 0,0,0);
private static int longestWorm(int mat,int i, int j, int max)
if (i == mat.length) return max;
if (j == mat[i].length-1)
return longestWorm(mat, i + 1, 0, max);
if (wormCount(mat,i,j,0) > max)
max = wormCount(mat,i,j,0);
return longestWorm(mat, i, j + 1, max);
private static int wormCount(int mat, int i, int j,int count)
if(i < mat.length-1 && mat[i][j] == mat[i+1][j]+1)
return 1+ wormCount(mat, i+1, j, count+1);
if(j < mat[i].length-1 && mat[i][j] == mat[i][j+1]+1)
return 1+ wormCount(mat, i, j+1, count+1);
if(i > 0 && mat[i][j] == mat[i-1][j]+1)
return 1+ wormCount(mat, i-1, j, count+1);
if(j > 0 && mat[i][j] == mat[i][j-1]+1)
return 1+ wormCount(mat, i, j-1, count+1);
return count;
java recursion matrix pathfinding
edited Mar 3 at 1:37
200_success
123k14142399
123k14142399
asked Mar 2 at 21:07
BOIDEM
133
133
Why are you restricted to using pure recursion? Is this a homework problem?
â 200_success
Mar 2 at 21:16
I have an exam in Java soon and I decided to write every recursive function of every exam since 2005 to the last semester, this question has appeared in one of the exams.
â BOIDEM
Mar 2 at 21:31
add a comment |Â
Why are you restricted to using pure recursion? Is this a homework problem?
â 200_success
Mar 2 at 21:16
I have an exam in Java soon and I decided to write every recursive function of every exam since 2005 to the last semester, this question has appeared in one of the exams.
â BOIDEM
Mar 2 at 21:31
Why are you restricted to using pure recursion? Is this a homework problem?
â 200_success
Mar 2 at 21:16
Why are you restricted to using pure recursion? Is this a homework problem?
â 200_success
Mar 2 at 21:16
I have an exam in Java soon and I decided to write every recursive function of every exam since 2005 to the last semester, this question has appeared in one of the exams.
â BOIDEM
Mar 2 at 21:31
I have an exam in Java soon and I decided to write every recursive function of every exam since 2005 to the last semester, this question has appeared in one of the exams.
â BOIDEM
Mar 2 at 21:31
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
So a couple of remarks:
If I understand the required solution, you seem to have a bug, e.g.
longestWorm(new int0, 1, 3, 9, 8 ,7, 4, 5, 6)returns 10, although there are only 9 numbers. The wormcount calculation is then wrong because you increment the counter twice instead of once. Recursively defining the problem, you can say that the length of a path is the length of the current cell (which is one), summed by the length of the remaining path. Which would make the length function something like:private static int wormCount(int mat, int i, int j)
if (i < mat.length - 1 && mat[i][j] == mat[i + 1][j] + 1)
return 1 + wormCount(mat, i + 1, j);
....
return 1;
This also makes the base case very clear. You also do not necessarily need to pass the count variable. Only perhaps if you would want to make it a tail-recursive function, but AFAIK in Java it will not help you eliminate a stack overflow.
The problem is defined as finding a path of increasing order, but your code seems to calculate it the other way around. Not wrong, but slightly confusing.
In the
longestWormfunction, you calculate the same function twice. This is not necessary:max = Math.max(max, wormCount(mat, i, j, 0));
You could save it in a local variable if it helps the readeability.
I do not know the optimal way to solve this problem. My guess would be you could at each index calculate the path length, and save it in a table. Because each value is unique, it could simply be a map between the value and the length of the path starting at that value (else, it could simply be a key generated using the indices i and j). Before calculating the length for a particular value (or starting at a particular index), you first look if you already calculated it, which you could then reuse.
Your response is very detailed and helpful. for the matrix you presented as an example, the function should return the value 6 because the longest sequence is 4,5,6,7,8,9. you were right, there was a bug. Your idea about the maximum is very simple. Prevents duplication of calculations and multiplication of code writing. I will use it from now on forever. I edited the function according to your great suggestions.
â BOIDEM
Mar 3 at 1:25
@BOIDEM Please see What to do when someone answers. I have rolled back Rev 5 â 3.
â 200_success
Mar 3 at 1:37
add a comment |Â
up vote
0
down vote
After consulting with other students and after endless discussions about this question, the final solution of this code is:
public static int worm(int a,int i,int j,int lastValue,boolean start)
j==a[0].length)
return 0;
if(lastValue == a[i][j] - 1
public static int longestWorm2(int a,int i,int j,int max)
if(i==a.length)
return max;
if(j==a[0].length)
return longestWorm2(a,i+1,0,max);
max = Math.max(max, worm(a,i,j,a[i][j],true));
return longestWorm2(a,i,j+1,max);
public static int longestWorm2(int a)
return longestWorm2(a,0,0,1);
add a comment |Â
up vote
0
down vote
In accordance with @Koekje interesting recommendations, I edited the code so that the base case is more understandable, and also because a bug has been found in the code.
It was also recommended by Koekje, of course, to use the current MAX to avoid recoding and recalculation. Now, the code seems shorter and more readable.
Of course, I'd like to see more comments/insights.
public static int longestWorm(int mat)
return longestWorm(mat, 0,0,0);
private static int longestWorm(int mat,int i, int j, int max)
if (i == mat.length) return max;
if (j == mat[i].length)
return longestWorm(mat, i + 1, 0, max);
max= Math.max(max,wormCount(mat,i,j));
return longestWorm(mat, i, j + 1, max);
private static int wormCount(int mat, int i, int j)
if(i < mat.length-1 && mat[i][j] == mat[i+1][j]+1)
return 1+ wormCount(mat, i+1, j);
if(j < mat[i].length-1 && mat[i][j] == mat[i][j+1]+1)
return 1+ wormCount(mat, i, j+1);
if(i > 0 && mat[i][j] == mat[i-1][j]+1)
return 1+ wormCount(mat, i-1, j);
if(j > 0 && mat[i][j] == mat[i][j-1]+1)
return 1+ wormCount(mat, i, j-1);
return 1;
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
So a couple of remarks:
If I understand the required solution, you seem to have a bug, e.g.
longestWorm(new int0, 1, 3, 9, 8 ,7, 4, 5, 6)returns 10, although there are only 9 numbers. The wormcount calculation is then wrong because you increment the counter twice instead of once. Recursively defining the problem, you can say that the length of a path is the length of the current cell (which is one), summed by the length of the remaining path. Which would make the length function something like:private static int wormCount(int mat, int i, int j)
if (i < mat.length - 1 && mat[i][j] == mat[i + 1][j] + 1)
return 1 + wormCount(mat, i + 1, j);
....
return 1;
This also makes the base case very clear. You also do not necessarily need to pass the count variable. Only perhaps if you would want to make it a tail-recursive function, but AFAIK in Java it will not help you eliminate a stack overflow.
The problem is defined as finding a path of increasing order, but your code seems to calculate it the other way around. Not wrong, but slightly confusing.
In the
longestWormfunction, you calculate the same function twice. This is not necessary:max = Math.max(max, wormCount(mat, i, j, 0));
You could save it in a local variable if it helps the readeability.
I do not know the optimal way to solve this problem. My guess would be you could at each index calculate the path length, and save it in a table. Because each value is unique, it could simply be a map between the value and the length of the path starting at that value (else, it could simply be a key generated using the indices i and j). Before calculating the length for a particular value (or starting at a particular index), you first look if you already calculated it, which you could then reuse.
Your response is very detailed and helpful. for the matrix you presented as an example, the function should return the value 6 because the longest sequence is 4,5,6,7,8,9. you were right, there was a bug. Your idea about the maximum is very simple. Prevents duplication of calculations and multiplication of code writing. I will use it from now on forever. I edited the function according to your great suggestions.
â BOIDEM
Mar 3 at 1:25
@BOIDEM Please see What to do when someone answers. I have rolled back Rev 5 â 3.
â 200_success
Mar 3 at 1:37
add a comment |Â
up vote
2
down vote
accepted
So a couple of remarks:
If I understand the required solution, you seem to have a bug, e.g.
longestWorm(new int0, 1, 3, 9, 8 ,7, 4, 5, 6)returns 10, although there are only 9 numbers. The wormcount calculation is then wrong because you increment the counter twice instead of once. Recursively defining the problem, you can say that the length of a path is the length of the current cell (which is one), summed by the length of the remaining path. Which would make the length function something like:private static int wormCount(int mat, int i, int j)
if (i < mat.length - 1 && mat[i][j] == mat[i + 1][j] + 1)
return 1 + wormCount(mat, i + 1, j);
....
return 1;
This also makes the base case very clear. You also do not necessarily need to pass the count variable. Only perhaps if you would want to make it a tail-recursive function, but AFAIK in Java it will not help you eliminate a stack overflow.
The problem is defined as finding a path of increasing order, but your code seems to calculate it the other way around. Not wrong, but slightly confusing.
In the
longestWormfunction, you calculate the same function twice. This is not necessary:max = Math.max(max, wormCount(mat, i, j, 0));
You could save it in a local variable if it helps the readeability.
I do not know the optimal way to solve this problem. My guess would be you could at each index calculate the path length, and save it in a table. Because each value is unique, it could simply be a map between the value and the length of the path starting at that value (else, it could simply be a key generated using the indices i and j). Before calculating the length for a particular value (or starting at a particular index), you first look if you already calculated it, which you could then reuse.
Your response is very detailed and helpful. for the matrix you presented as an example, the function should return the value 6 because the longest sequence is 4,5,6,7,8,9. you were right, there was a bug. Your idea about the maximum is very simple. Prevents duplication of calculations and multiplication of code writing. I will use it from now on forever. I edited the function according to your great suggestions.
â BOIDEM
Mar 3 at 1:25
@BOIDEM Please see What to do when someone answers. I have rolled back Rev 5 â 3.
â 200_success
Mar 3 at 1:37
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
So a couple of remarks:
If I understand the required solution, you seem to have a bug, e.g.
longestWorm(new int0, 1, 3, 9, 8 ,7, 4, 5, 6)returns 10, although there are only 9 numbers. The wormcount calculation is then wrong because you increment the counter twice instead of once. Recursively defining the problem, you can say that the length of a path is the length of the current cell (which is one), summed by the length of the remaining path. Which would make the length function something like:private static int wormCount(int mat, int i, int j)
if (i < mat.length - 1 && mat[i][j] == mat[i + 1][j] + 1)
return 1 + wormCount(mat, i + 1, j);
....
return 1;
This also makes the base case very clear. You also do not necessarily need to pass the count variable. Only perhaps if you would want to make it a tail-recursive function, but AFAIK in Java it will not help you eliminate a stack overflow.
The problem is defined as finding a path of increasing order, but your code seems to calculate it the other way around. Not wrong, but slightly confusing.
In the
longestWormfunction, you calculate the same function twice. This is not necessary:max = Math.max(max, wormCount(mat, i, j, 0));
You could save it in a local variable if it helps the readeability.
I do not know the optimal way to solve this problem. My guess would be you could at each index calculate the path length, and save it in a table. Because each value is unique, it could simply be a map between the value and the length of the path starting at that value (else, it could simply be a key generated using the indices i and j). Before calculating the length for a particular value (or starting at a particular index), you first look if you already calculated it, which you could then reuse.
So a couple of remarks:
If I understand the required solution, you seem to have a bug, e.g.
longestWorm(new int0, 1, 3, 9, 8 ,7, 4, 5, 6)returns 10, although there are only 9 numbers. The wormcount calculation is then wrong because you increment the counter twice instead of once. Recursively defining the problem, you can say that the length of a path is the length of the current cell (which is one), summed by the length of the remaining path. Which would make the length function something like:private static int wormCount(int mat, int i, int j)
if (i < mat.length - 1 && mat[i][j] == mat[i + 1][j] + 1)
return 1 + wormCount(mat, i + 1, j);
....
return 1;
This also makes the base case very clear. You also do not necessarily need to pass the count variable. Only perhaps if you would want to make it a tail-recursive function, but AFAIK in Java it will not help you eliminate a stack overflow.
The problem is defined as finding a path of increasing order, but your code seems to calculate it the other way around. Not wrong, but slightly confusing.
In the
longestWormfunction, you calculate the same function twice. This is not necessary:max = Math.max(max, wormCount(mat, i, j, 0));
You could save it in a local variable if it helps the readeability.
I do not know the optimal way to solve this problem. My guess would be you could at each index calculate the path length, and save it in a table. Because each value is unique, it could simply be a map between the value and the length of the path starting at that value (else, it could simply be a key generated using the indices i and j). Before calculating the length for a particular value (or starting at a particular index), you first look if you already calculated it, which you could then reuse.
answered Mar 3 at 0:35
Koekje
1,017211
1,017211
Your response is very detailed and helpful. for the matrix you presented as an example, the function should return the value 6 because the longest sequence is 4,5,6,7,8,9. you were right, there was a bug. Your idea about the maximum is very simple. Prevents duplication of calculations and multiplication of code writing. I will use it from now on forever. I edited the function according to your great suggestions.
â BOIDEM
Mar 3 at 1:25
@BOIDEM Please see What to do when someone answers. I have rolled back Rev 5 â 3.
â 200_success
Mar 3 at 1:37
add a comment |Â
Your response is very detailed and helpful. for the matrix you presented as an example, the function should return the value 6 because the longest sequence is 4,5,6,7,8,9. you were right, there was a bug. Your idea about the maximum is very simple. Prevents duplication of calculations and multiplication of code writing. I will use it from now on forever. I edited the function according to your great suggestions.
â BOIDEM
Mar 3 at 1:25
@BOIDEM Please see What to do when someone answers. I have rolled back Rev 5 â 3.
â 200_success
Mar 3 at 1:37
Your response is very detailed and helpful. for the matrix you presented as an example, the function should return the value 6 because the longest sequence is 4,5,6,7,8,9. you were right, there was a bug. Your idea about the maximum is very simple. Prevents duplication of calculations and multiplication of code writing. I will use it from now on forever. I edited the function according to your great suggestions.
â BOIDEM
Mar 3 at 1:25
Your response is very detailed and helpful. for the matrix you presented as an example, the function should return the value 6 because the longest sequence is 4,5,6,7,8,9. you were right, there was a bug. Your idea about the maximum is very simple. Prevents duplication of calculations and multiplication of code writing. I will use it from now on forever. I edited the function according to your great suggestions.
â BOIDEM
Mar 3 at 1:25
@BOIDEM Please see What to do when someone answers. I have rolled back Rev 5 â 3.
â 200_success
Mar 3 at 1:37
@BOIDEM Please see What to do when someone answers. I have rolled back Rev 5 â 3.
â 200_success
Mar 3 at 1:37
add a comment |Â
up vote
0
down vote
After consulting with other students and after endless discussions about this question, the final solution of this code is:
public static int worm(int a,int i,int j,int lastValue,boolean start)
j==a[0].length)
return 0;
if(lastValue == a[i][j] - 1
public static int longestWorm2(int a,int i,int j,int max)
if(i==a.length)
return max;
if(j==a[0].length)
return longestWorm2(a,i+1,0,max);
max = Math.max(max, worm(a,i,j,a[i][j],true));
return longestWorm2(a,i,j+1,max);
public static int longestWorm2(int a)
return longestWorm2(a,0,0,1);
add a comment |Â
up vote
0
down vote
After consulting with other students and after endless discussions about this question, the final solution of this code is:
public static int worm(int a,int i,int j,int lastValue,boolean start)
j==a[0].length)
return 0;
if(lastValue == a[i][j] - 1
public static int longestWorm2(int a,int i,int j,int max)
if(i==a.length)
return max;
if(j==a[0].length)
return longestWorm2(a,i+1,0,max);
max = Math.max(max, worm(a,i,j,a[i][j],true));
return longestWorm2(a,i,j+1,max);
public static int longestWorm2(int a)
return longestWorm2(a,0,0,1);
add a comment |Â
up vote
0
down vote
up vote
0
down vote
After consulting with other students and after endless discussions about this question, the final solution of this code is:
public static int worm(int a,int i,int j,int lastValue,boolean start)
j==a[0].length)
return 0;
if(lastValue == a[i][j] - 1
public static int longestWorm2(int a,int i,int j,int max)
if(i==a.length)
return max;
if(j==a[0].length)
return longestWorm2(a,i+1,0,max);
max = Math.max(max, worm(a,i,j,a[i][j],true));
return longestWorm2(a,i,j+1,max);
public static int longestWorm2(int a)
return longestWorm2(a,0,0,1);
After consulting with other students and after endless discussions about this question, the final solution of this code is:
public static int worm(int a,int i,int j,int lastValue,boolean start)
j==a[0].length)
return 0;
if(lastValue == a[i][j] - 1
public static int longestWorm2(int a,int i,int j,int max)
if(i==a.length)
return max;
if(j==a[0].length)
return longestWorm2(a,i+1,0,max);
max = Math.max(max, worm(a,i,j,a[i][j],true));
return longestWorm2(a,i,j+1,max);
public static int longestWorm2(int a)
return longestWorm2(a,0,0,1);
answered Mar 5 at 17:43
BOIDEM
133
133
add a comment |Â
add a comment |Â
up vote
0
down vote
In accordance with @Koekje interesting recommendations, I edited the code so that the base case is more understandable, and also because a bug has been found in the code.
It was also recommended by Koekje, of course, to use the current MAX to avoid recoding and recalculation. Now, the code seems shorter and more readable.
Of course, I'd like to see more comments/insights.
public static int longestWorm(int mat)
return longestWorm(mat, 0,0,0);
private static int longestWorm(int mat,int i, int j, int max)
if (i == mat.length) return max;
if (j == mat[i].length)
return longestWorm(mat, i + 1, 0, max);
max= Math.max(max,wormCount(mat,i,j));
return longestWorm(mat, i, j + 1, max);
private static int wormCount(int mat, int i, int j)
if(i < mat.length-1 && mat[i][j] == mat[i+1][j]+1)
return 1+ wormCount(mat, i+1, j);
if(j < mat[i].length-1 && mat[i][j] == mat[i][j+1]+1)
return 1+ wormCount(mat, i, j+1);
if(i > 0 && mat[i][j] == mat[i-1][j]+1)
return 1+ wormCount(mat, i-1, j);
if(j > 0 && mat[i][j] == mat[i][j-1]+1)
return 1+ wormCount(mat, i, j-1);
return 1;
add a comment |Â
up vote
0
down vote
In accordance with @Koekje interesting recommendations, I edited the code so that the base case is more understandable, and also because a bug has been found in the code.
It was also recommended by Koekje, of course, to use the current MAX to avoid recoding and recalculation. Now, the code seems shorter and more readable.
Of course, I'd like to see more comments/insights.
public static int longestWorm(int mat)
return longestWorm(mat, 0,0,0);
private static int longestWorm(int mat,int i, int j, int max)
if (i == mat.length) return max;
if (j == mat[i].length)
return longestWorm(mat, i + 1, 0, max);
max= Math.max(max,wormCount(mat,i,j));
return longestWorm(mat, i, j + 1, max);
private static int wormCount(int mat, int i, int j)
if(i < mat.length-1 && mat[i][j] == mat[i+1][j]+1)
return 1+ wormCount(mat, i+1, j);
if(j < mat[i].length-1 && mat[i][j] == mat[i][j+1]+1)
return 1+ wormCount(mat, i, j+1);
if(i > 0 && mat[i][j] == mat[i-1][j]+1)
return 1+ wormCount(mat, i-1, j);
if(j > 0 && mat[i][j] == mat[i][j-1]+1)
return 1+ wormCount(mat, i, j-1);
return 1;
add a comment |Â
up vote
0
down vote
up vote
0
down vote
In accordance with @Koekje interesting recommendations, I edited the code so that the base case is more understandable, and also because a bug has been found in the code.
It was also recommended by Koekje, of course, to use the current MAX to avoid recoding and recalculation. Now, the code seems shorter and more readable.
Of course, I'd like to see more comments/insights.
public static int longestWorm(int mat)
return longestWorm(mat, 0,0,0);
private static int longestWorm(int mat,int i, int j, int max)
if (i == mat.length) return max;
if (j == mat[i].length)
return longestWorm(mat, i + 1, 0, max);
max= Math.max(max,wormCount(mat,i,j));
return longestWorm(mat, i, j + 1, max);
private static int wormCount(int mat, int i, int j)
if(i < mat.length-1 && mat[i][j] == mat[i+1][j]+1)
return 1+ wormCount(mat, i+1, j);
if(j < mat[i].length-1 && mat[i][j] == mat[i][j+1]+1)
return 1+ wormCount(mat, i, j+1);
if(i > 0 && mat[i][j] == mat[i-1][j]+1)
return 1+ wormCount(mat, i-1, j);
if(j > 0 && mat[i][j] == mat[i][j-1]+1)
return 1+ wormCount(mat, i, j-1);
return 1;
In accordance with @Koekje interesting recommendations, I edited the code so that the base case is more understandable, and also because a bug has been found in the code.
It was also recommended by Koekje, of course, to use the current MAX to avoid recoding and recalculation. Now, the code seems shorter and more readable.
Of course, I'd like to see more comments/insights.
public static int longestWorm(int mat)
return longestWorm(mat, 0,0,0);
private static int longestWorm(int mat,int i, int j, int max)
if (i == mat.length) return max;
if (j == mat[i].length)
return longestWorm(mat, i + 1, 0, max);
max= Math.max(max,wormCount(mat,i,j));
return longestWorm(mat, i, j + 1, max);
private static int wormCount(int mat, int i, int j)
if(i < mat.length-1 && mat[i][j] == mat[i+1][j]+1)
return 1+ wormCount(mat, i+1, j);
if(j < mat[i].length-1 && mat[i][j] == mat[i][j+1]+1)
return 1+ wormCount(mat, i, j+1);
if(i > 0 && mat[i][j] == mat[i-1][j]+1)
return 1+ wormCount(mat, i-1, j);
if(j > 0 && mat[i][j] == mat[i][j-1]+1)
return 1+ wormCount(mat, i, j-1);
return 1;
edited Mar 16 at 19:54
answered Mar 3 at 1:56
BOIDEM
133
133
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188701%2ffind-the-longest-path-in-a-matrix-where-each-step-has-entries-that-differ-by-1%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Why are you restricted to using pure recursion? Is this a homework problem?
â 200_success
Mar 2 at 21:16
I have an exam in Java soon and I decided to write every recursive function of every exam since 2005 to the last semester, this question has appeared in one of the exams.
â BOIDEM
Mar 2 at 21:31