全排列问题

  1. package com.test.clickhouse.config;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. /**
  5. * @author jia
  6. * @since 2022-04-13 12:01
  7. */
  8. public class Permute {
  9. public static void main(String[] args) {
  10. List<List<Integer>> permute = permute(new int[]{1, 2, 3});
  11. System.out.println(permute);
  12. }
  13. public static List<List<Integer>> permute(int[] arr) {
  14. List<List<Integer>> resList = new LinkedList<>();
  15. LinkedList<Integer> res = new LinkedList<>();
  16. boolean [] used = new boolean[arr.length];
  17. backtrack(arr, used, res, resList);
  18. return resList;
  19. }
  20. private static void backtrack(int[] arr, boolean[] used, LinkedList<Integer> res,
  21. List<List<Integer>> resList) {
  22. if (res.size() == arr.length) {
  23. resList.add(new LinkedList<>(res));
  24. return;
  25. }
  26. for (int i = 0; i < arr.length; i++) {
  27. if (used[i]) {
  28. continue;
  29. }
  30. // 做选择
  31. res.add(arr[i]);
  32. used[i] = true;
  33. backtrack(arr, used, res, resList);
  34. // 取消选择
  35. res.removeLast();
  36. used[i] = false;
  37. }
  38. }
  39. }

n皇后问题

  1. package com.test.clickhouse.config;
  2. import java.util.ArrayList;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. /**
  7. * @author jia
  8. * @since 2022-04-13 12:01
  9. */
  10. public class Permute {
  11. public static void main(String[] args) {
  12. List<String[][]> permute = queens(4);
  13. List<List<String>> collect = permute.stream().map(Permute::parse).collect(Collectors.toList());
  14. System.out.println(collect);
  15. }
  16. private static List<String> parse(String[][] arr) {
  17. List<String> list = new ArrayList<>();
  18. for (String[] strings : arr) {
  19. list.add(String.join("", strings));
  20. }
  21. return list;
  22. }
  23. private static List<String[][]> queens(int n) {
  24. String[][] arr = new String[n][n];
  25. for (int i = 0; i < n; i++) {
  26. for (int j = 0; j < n; j++) {
  27. arr[i][j] = ".";
  28. }
  29. }
  30. List<String[][]> res = new LinkedList<>();
  31. backtrack(0, arr, res);
  32. return res;
  33. }
  34. private static void backtrack(int row, String[][] arr, List<String[][]> res) {
  35. if (arr.length == row) {
  36. res.add(copy(arr));
  37. return;
  38. }
  39. for (int col = 0; col < arr[row].length; col++) {
  40. if (!isValid(arr, row, col)) {
  41. continue;
  42. }
  43. // 做选择
  44. arr[row][col] = "Q";
  45. backtrack(row + 1, arr, res);
  46. // 取消选择
  47. arr[row][col] = ".";
  48. }
  49. }
  50. private static boolean isValid(String[][] arr, int row, int col) {
  51. int n = arr.length;
  52. // 判断左上
  53. for (int i = row-1, j = col-1; i >= 0 && j >= 0; i--, j--) {
  54. if (arr[i][j].equals("Q")) {
  55. return false;
  56. }
  57. }
  58. // 判断上
  59. for (int i = row-1; i >= 0; i--) {
  60. if (arr[i][col].equals("Q")) {
  61. return false;
  62. }
  63. }
  64. // 判断右上
  65. for (int i = row-1, j = col+1; i >= 0 && j < n; i--, j++) {
  66. if (arr[i][j].equals("Q")) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. private static String[][] copy(String[][] arr) {
  73. int length = arr.length;
  74. String[][] arrRes = new String[length][length];
  75. for (int i = 0; i < length; i++) {
  76. for (int j = 0; j < length; j++) {
  77. arrRes[i][j] = arr[i][j];
  78. }
  79. }
  80. return arrRes;
  81. }
  82. }