() {
- public int compare(int[] a, int[] b) {
- return a[2] - b[2];
- }
- });
+ neighbor.sort(Comparator.comparingInt(a -> a[2]));
for (int[] nb : neighbor) {
row = nb[0];
diff --git a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java
index d66d1482..23fc0689 100644
--- a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java
+++ b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java
@@ -37,16 +37,14 @@ public class MazeRecursion {
// clone another map for setWay2 method
for (int i = 0; i < map.length; i++) {
- for (int j = 0; j < map[i].length; j++) {
- map2[i][j] = map[i][j];
- }
+ System.arraycopy(map[i], 0, map2[i], 0, map[i].length);
}
// By using recursive backtracking to let your ball(target) find its way in the
// maze
// The first parameter is the map
// Second parameter is x coordinate of your target
- // Thrid parameter is the y coordinate of your target
+ // Third parameter is the y coordinate of your target
setWay(map, 1, 1);
setWay2(map2, 1, 1);
@@ -107,14 +105,14 @@ public class MazeRecursion {
return true;
} else {
// means that the current point is the dead end, the ball cannot proceed, set
- // the current point to 3 and return false, the backtraking will start, it will
+ // the current point to 3 and return false, the backtracking will start, it will
// go to the previous step and check for feasible path again
map[i][j] = 3;
return false;
}
} else { // if the map[i][j] != 0 , it will probably be 1,2,3, return false because the
// ball cannot hit the wall, cannot go to the path that has gone though before,
- // and cannot head to deadend.
+ // and cannot head to deadened.
return false;
}
}
@@ -138,13 +136,13 @@ public class MazeRecursion {
return true;
} else {
// means that the current point is the dead end, the ball cannot proceed, set
- // the current point to 3 and return false, the backtraking will start, it will
+ // the current point to 3 and return false, the backtracking will start, it will
// go to the previous step and check for feasible path again
map[i][j] = 3;
return false;
}
} else { // if the map[i][j] != 0 , it will probably be 1,2,3, return false because the
- // ball cannot hit the wall, cannot go to the path that has gone though before,
+ // ball cannot hit the wall, cannot go to the path that has gone through before,
// and cannot head to deadend.
return false;
}
diff --git a/src/main/java/com/thealgorithms/backtracking/NQueens.java b/src/main/java/com/thealgorithms/backtracking/NQueens.java
index 632d5441..e40b8291 100644
--- a/src/main/java/com/thealgorithms/backtracking/NQueens.java
+++ b/src/main/java/com/thealgorithms/backtracking/NQueens.java
@@ -8,18 +8,18 @@ import java.util.List;
* which N queens can be placed on the board such no two queens attack each
* other. Ex. N = 6 Solution= There are 4 possible ways Arrangement: 1 ".Q....",
* "...Q..", ".....Q", "Q.....", "..Q...", "....Q."
- *
+ *
* Arrangement: 2 "..Q...", ".....Q", ".Q....", "....Q.", "Q.....", "...Q.."
- *
+ *
* Arrangement: 3 "...Q..", "Q.....", "....Q.", ".Q....", ".....Q", "..Q..."
- *
+ *
* Arrangement: 4 "....Q.", "..Q...", "Q.....", ".....Q", "...Q..", ".Q...."
*
* Solution: Brute Force approach:
*
* Generate all possible arrangement to place N queens on N*N board. Check each
* board if queens are placed safely. If it is safe, include arrangement in
- * solution set. Otherwise ignore it
+ * solution set. Otherwise, ignore it
*
* Optimized solution: This can be solved using backtracking in below steps
*
@@ -51,10 +51,10 @@ public class NQueens {
} else {
System.out.println("Arrangement for placing " + queens + " queens");
}
- arrangements.forEach(arrangement -> {
- arrangement.forEach(row -> System.out.println(row));
+ for (List arrangement : arrangements) {
+ arrangement.forEach(System.out::println);
System.out.println();
- });
+ }
}
/**
diff --git a/src/main/java/com/thealgorithms/backtracking/Permutation.java b/src/main/java/com/thealgorithms/backtracking/Permutation.java
index 9ad18e1e..5d88c846 100644
--- a/src/main/java/com/thealgorithms/backtracking/Permutation.java
+++ b/src/main/java/com/thealgorithms/backtracking/Permutation.java
@@ -5,7 +5,7 @@ import java.util.List;
/**
* Finds all permutations of given array
- * @author Alan Piao (https://github.com/cpiao3)
+ * @author Alan Piao (Git-Alan Piao)
*/
public class Permutation {