Format code with prettier (#3375)

This commit is contained in:
acbin
2022-10-03 17:23:00 +08:00
committed by GitHub
parent 32b9b11ed5
commit e96f567bfc
464 changed files with 11483 additions and 6189 deletions

View File

@ -7,7 +7,9 @@ import java.util.*;
* @author Alan Piao (https://github.com/cpiao3)
*/
public class Combination {
private static int length;
/**
* Find all combinations of given array using backtracking
* @param arr the array.
@ -26,6 +28,7 @@ public class Combination {
backtracking(array, 0, new TreeSet<T>(), result);
return result;
}
/**
* Backtrack all possible combinations of a given array
* @param arr the array.
@ -34,7 +37,12 @@ public class Combination {
* @param result the list contains all combination.
* @param <T> the type of elements in the array.
*/
private static <T> void backtracking(T[] arr, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
private static <T> void backtracking(
T[] arr,
int index,
TreeSet<T> currSet,
List<TreeSet<T>> result
) {
if (index + length - currSet.size() > arr.length) return;
if (length - 1 == currSet.size()) {
for (int i = index; i < arr.length; i++) {

View File

@ -13,13 +13,11 @@ public class FloodFill {
* @param x The x co-ordinate of which color is to be obtained
* @param y The y co-ordinate of which color is to be obtained
*/
public static int getPixel(int[][] image, int x, int y) {
return image[x][y];
}
public static int getPixel(int[][] image, int x, int y) {
return image[x][y];
}
/**
* Put the color at the given co-odrinates of a 2D image
*
@ -27,13 +25,10 @@ public class FloodFill {
* @param x The x co-ordinate at which color is to be filled
* @param y The y co-ordinate at which color is to be filled
*/
public static void putPixel(int[][] image, int x, int y, int newColor) {
image[x][y] = newColor;
}
public static void putPixel(int[][] image, int x, int y, int newColor) {
image[x][y] = newColor;
}
/**
* Fill the 2D image with new color
*
@ -44,26 +39,29 @@ public class FloodFill {
* @param oldColor The old color which is to be replaced in the image
* @return
*/
public static void floodFill(int[][] image, int x, int y, int newColor, int oldColor) {
public static void floodFill(
int[][] image,
int x,
int y,
int newColor,
int oldColor
) {
if (x < 0 || x >= image.length) return;
if (y < 0 || y >= image[x].length) return;
if (getPixel(image, x, y) != oldColor) return;
if(x < 0 || x >= image.length) return;
if(y < 0 || y >= image[x].length) return;
if(getPixel(image, x, y) != oldColor) return;
putPixel(image, x, y, newColor);
putPixel(image, x, y, newColor);
/* Recursively check for horizontally & vertically adjacent coordinates */
floodFill(image, x + 1, y, newColor, oldColor);
floodFill(image, x - 1, y, newColor, oldColor);
floodFill(image, x, y + 1, newColor, oldColor);
floodFill(image, x, y - 1, newColor, oldColor);
/* Recursively check for horizontally & vertically adjacent coordinates */
floodFill(image, x + 1, y, newColor, oldColor);
floodFill(image, x - 1, y, newColor, oldColor);
floodFill(image, x, y + 1, newColor, oldColor);
floodFill(image, x, y - 1, newColor, oldColor);
/* Recursively check for diagonally adjacent coordinates */
floodFill(image, x + 1, y - 1, newColor, oldColor);
floodFill(image, x - 1, y + 1, newColor, oldColor);
floodFill(image, x + 1, y + 1, newColor, oldColor);
floodFill(image, x - 1, y - 1, newColor, oldColor);
}
}
/* Recursively check for diagonally adjacent coordinates */
floodFill(image, x + 1, y - 1, newColor, oldColor);
floodFill(image, x - 1, y + 1, newColor, oldColor);
floodFill(image, x + 1, y + 1, newColor, oldColor);
floodFill(image, x - 1, y - 1, newColor, oldColor);
}
}

View File

@ -25,10 +25,19 @@ import java.util.*;
*/
public class KnightsTour {
private final static int base = 12;
private final static int[][] moves = {{1, -2}, {2, -1}, {2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}}; // Possible moves by knight on chess
private static int[][] grid; // chess grid
private static int total; // total squares in chess
private static final int base = 12;
private static final int[][] moves = {
{ 1, -2 },
{ 2, -1 },
{ 2, 1 },
{ 1, 2 },
{ -1, 2 },
{ -2, 1 },
{ -2, -1 },
{ -1, -2 },
}; // Possible moves by knight on chess
private static int[][] grid; // chess grid
private static int total; // total squares in chess
public static void main(String[] args) {
grid = new int[base][base];
@ -52,7 +61,6 @@ public class KnightsTour {
} else {
System.out.println("no result");
}
}
// Return True when solvable
@ -67,17 +75,23 @@ public class KnightsTour {
return false;
}
Collections.sort(neighbor, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[2] - b[2];
Collections.sort(
neighbor,
new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[2] - b[2];
}
}
});
);
for (int[] nb : neighbor) {
row = nb[0];
column = nb[1];
grid[row][column] = count;
if (!orphanDetected(count, row, column) && solve(row, column, count + 1)) {
if (
!orphanDetected(count, row, column) &&
solve(row, column, count + 1)
) {
return true;
}
grid[row][column] = 0;
@ -95,7 +109,7 @@ public class KnightsTour {
int y = m[1];
if (grid[row + y][column + x] == 0) {
int num = countNeighbors(row + y, column + x);
neighbour.add(new int[]{row + y, column + x, num});
neighbour.add(new int[] { row + y, column + x, num });
}
}
return neighbour;

View File

@ -2,157 +2,154 @@ package com.thealgorithms.backtracking;
public class MazeRecursion {
public static void mazeRecursion() {
// First create a 2 dimensions array to mimic a maze map
int[][] map = new int[8][7];
int[][] map2 = new int[8][7];
public static void mazeRecursion() {
// First create a 2 dimensions array to mimic a maze map
int[][] map = new int[8][7];
int[][] map2 = new int[8][7];
// We use 1 to indicate wall
// Set the ceiling and floor to 1
for (int i = 0; i < 7; i++) {
map[0][i] = 1;
map[7][i] = 1;
}
// We use 1 to indicate wall
// Set the ceiling and floor to 1
for (int i = 0; i < 7; i++) {
map[0][i] = 1;
map[7][i] = 1;
}
// Then we set the left and right wall to 1
for (int i = 0; i < 8; i++) {
map[i][0] = 1;
map[i][6] = 1;
}
// Then we set the left and right wall to 1
for (int i = 0; i < 8; i++) {
map[i][0] = 1;
map[i][6] = 1;
}
// Now we have created a maze with its wall initialized
// Now we have created a maze with its wall initialized
// Here we set the obstacle
map[3][1] = 1;
map[3][2] = 1;
// Here we set the obstacle
map[3][1] = 1;
map[3][2] = 1;
// Print the current map
System.out.println("The condition of the map ");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
// Print the current map
System.out.println("The condition of the map ");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
// 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];
}
}
// 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];
}
}
// 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
setWay(map, 1, 1);
setWay2(map2, 1, 1);
// 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
setWay(map, 1, 1);
setWay2(map2, 1, 1);
// Print out the new map1, with the ball footprint
System.out.println("After the ball goes through the map1show the current map1 condition");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
// Print out the new map1, with the ball footprint
System.out.println(
"After the ball goes through the map1show the current map1 condition"
);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
// Print out the new map2, with the ball footprint
System.out.println("After the ball goes through the map2show the current map2 condition");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map2[i][j] + " ");
}
System.out.println();
}
}
// Print out the new map2, with the ball footprint
System.out.println(
"After the ball goes through the map2show the current map2 condition"
);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map2[i][j] + " ");
}
System.out.println();
}
}
// Using recursive path finding to help the ball find its way in the maze
// Description
// 1. map (means the maze)
// 2. i, j (means the initial coordinate of the ball in the maze)
// 3. if the ball can reach the end of maze, that is position of map[6][5],
// means the we have found a path for the ball
// 4. Additional Information 0 in the map[i][j] means the ball has not gone
// through this position, 1 means the wall, 2 means the path is feasible, 3
// means the ball has gone through the path but this path is dead end
// 5. We will need strategy for the ball to pass through the maze for example:
// Down -> Right -> Up -> Left, if the path doesn't work, then backtrack
/**
*
* @Description
* @author OngLipWei
* @date Jun 23, 202111:36:14 AM
* @param map The maze
* @param i x coordinate of your ball(target)
* @param j y coordinate of your ball(target)
* @return If we did find a path for the ballreturn trueelse false
*/
public static boolean setWay(int[][] map, int i, int j) {
if (map[6][5] == 2) { // means the ball find its path, ending condition
return true;
}
if (map[i][j] == 0) { // if the ball haven't gone through this point
// then the ball follows the move strategy : down -> right -> up -> left
map[i][j] = 2; // we assume that this path is feasible first, set the current point to 2 first。
if (setWay(map, i + 1, j)) { // go down
return true;
} else if (setWay(map, i, j + 1)) { // go right
return true;
} else if (setWay(map, i - 1, j)) { // go up
return true;
} else if (setWay(map, i, j - 1)) { // go left
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
// 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.
return false;
}
}
// Using recursive path finding to help the ball find its way in the maze
// Description
// 1. map (means the maze)
// 2. i, j (means the initial coordinate of the ball in the maze)
// 3. if the ball can reach the end of maze, that is position of map[6][5],
// means the we have found a path for the ball
// 4. Additional Information 0 in the map[i][j] means the ball has not gone
// through this position, 1 means the wall, 2 means the path is feasible, 3
// means the ball has gone through the path but this path is dead end
// 5. We will need strategy for the ball to pass through the maze for example:
// Down -> Right -> Up -> Left, if the path doesn't work, then backtrack
/**
*
* @Description
* @author OngLipWei
* @date Jun 23, 202111:36:14 AM
* @param map The maze
* @param i x coordinate of your ball(target)
* @param j y coordinate of your ball(target)
* @return If we did find a path for the ballreturn trueelse false
*/
public static boolean setWay(int[][] map, int i, int j) {
if (map[6][5] == 2) {// means the ball find its path, ending condition
return true;
}
if (map[i][j] == 0) { // if the ball haven't gone through this point
// then the ball follows the move strategy : down -> right -> up -> left
map[i][j] = 2; // we assume that this path is feasible first, set the current point to 2 first。
if (setWay(map, i + 1, j)) { // go down
return true;
} else if (setWay(map, i, j + 1)) { // go right
return true;
} else if (setWay(map, i - 1, j)) { // go up
return true;
} else if (setWay(map, i, j - 1)) { // go left
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
// 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.
return false;
}
}
// Here is another move strategy for the ball: up->right->down->left
public static boolean setWay2(int[][] map, int i, int j) {
if (map[6][5] == 2) {// means the ball find its path, ending condition
return true;
}
if (map[i][j] == 0) { // if the ball haven't gone through this point
// then the ball follows the move strategy : up->right->down->left
map[i][j] = 2; // we assume that this path is feasible first, set the current point to 2 first。
if (setWay2(map, i - 1, j)) { // go up
return true;
} else if (setWay2(map, i, j + 1)) { // go right
return true;
} else if (setWay2(map, i + 1, j)) { // go down
return true;
} else if (setWay2(map, i, j - 1)) { // go left
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
// 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.
return false;
}
}
// Here is another move strategy for the ball: up->right->down->left
public static boolean setWay2(int[][] map, int i, int j) {
if (map[6][5] == 2) { // means the ball find its path, ending condition
return true;
}
if (map[i][j] == 0) { // if the ball haven't gone through this point
// then the ball follows the move strategy : up->right->down->left
map[i][j] = 2; // we assume that this path is feasible first, set the current point to 2 first。
if (setWay2(map, i - 1, j)) { // go up
return true;
} else if (setWay2(map, i, j + 1)) { // go right
return true;
} else if (setWay2(map, i + 1, j)) { // go down
return true;
} else if (setWay2(map, i, j - 1)) { // go left
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
// 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.
return false;
}
}
}

View File

@ -47,7 +47,14 @@ public class NQueens {
List<List<String>> arrangements = new ArrayList<List<String>>();
getSolution(queens, arrangements, new int[queens], 0);
if (arrangements.isEmpty()) {
System.out.println("There is no way to place " + queens + " queens on board of size " + queens + "x" + queens);
System.out.println(
"There is no way to place " +
queens +
" queens on board of size " +
queens +
"x" +
queens
);
} else {
System.out.println("Arrangement for placing " + queens + " queens");
}
@ -65,7 +72,12 @@ public class NQueens {
* @param columns: columns[i] = rowId where queen is placed in ith column.
* @param columnIndex: This is the column in which queen is being placed
*/
private static void getSolution(int boardSize, List<List<String>> solutions, int[] columns, int columnIndex) {
private static void getSolution(
int boardSize,
List<List<String>> solutions,
int[] columns,
int columnIndex
) {
if (columnIndex == boardSize) {
// this means that all queens have been placed
List<String> sol = new ArrayList<String>();
@ -99,7 +111,11 @@ public class NQueens {
* @param columnIndex: column in which queen is being placed
* @return true: if queen can be placed safely false: otherwise
*/
private static boolean isPlacedCorrectly(int[] columns, int rowIndex, int columnIndex) {
private static boolean isPlacedCorrectly(
int[] columns,
int rowIndex,
int columnIndex
) {
for (int i = 0; i < columnIndex; i++) {
int diff = Math.abs(columns[i] - rowIndex);
if (diff == 0 || columnIndex - i == diff) {

View File

@ -8,6 +8,7 @@ import java.util.List;
* @author Alan Piao (https://github.com/cpiao3)
*/
public class Permutation {
/**
* Find all permutations of given array using backtracking
* @param arr the array.
@ -20,6 +21,7 @@ public class Permutation {
backtracking(array, 0, result);
return result;
}
/**
* Backtrack all possible orders of a given array
* @param arr the array.
@ -37,6 +39,7 @@ public class Permutation {
swap(index, i, arr);
}
}
/**
* Swap two element for a given array
* @param a first index

View File

@ -18,10 +18,17 @@ public class PowerSum {
PowerSum ps = new PowerSum();
int count = ps.powSum(N, X);
//printing the answer.
System.out.println("Number of combinations of different natural number's raised to " + X + " having sum " + N + " are : ");
System.out.println(
"Number of combinations of different natural number's raised to " +
X +
" having sum " +
N +
" are : "
);
System.out.println(count);
sc.close();
}
private int count = 0, sum = 0;
public int powSum(int N, int X) {
@ -48,7 +55,7 @@ public class PowerSum {
}
}
//creating a separate power function so that it can be used again and again when required.
//creating a separate power function so that it can be used again and again when required.
private int power(int a, int b) {
return (int) Math.pow(a, b);
}