目录
[12. 矩阵中的路径](#12. 矩阵中的路径)
[13. 机器人的运动范围](#13. 机器人的运动范围)
[38. 字符串的排列](#38. 字符串的排列)
12. 矩阵中的路径
java
package Search;
public class HasPathInMatrix {
public boolean hasPath(String matrix, int rows, int cols, String str) {
if (rows == 0 || cols == 0) return false;
if (str.isEmpty()) return true;
// 转换为矩阵
char[] charArray = matrix.toCharArray();
char[][] matrixArray = buildMatrix(rows, cols, charArray);
int[][] flag = new int[rows][cols];
char[] pathArray = str.toCharArray();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (dfs(matrixArray, flag, pathArray, rows, cols, 0, i, j)) return true;
}
}
return false;
}
private boolean dfs(char[][] matrixArray, int[][] flag, char[] pathArray, int rows, int cols, int now, int i, int j) {
if (now == pathArray.length) return true;
if (i < 0 || i >= rows || j < 0 || j >= cols || matrixArray[i][j] != pathArray[now] || flag[i][j] == 1)
return false;
flag[i][j] = 1;
now++;
if (dfs(matrixArray, flag, pathArray, rows, cols, now, i - 1, j)
|| dfs(matrixArray, flag, pathArray, rows, cols, now, i + 1, j)
|| dfs(matrixArray, flag, pathArray, rows, cols, now, i, j + 1)
|| dfs(matrixArray, flag, pathArray, rows, cols, now, i, j - 1))
return true;
flag[i][j] = 0;
return false;
}
private static char[][] buildMatrix(int rows, int cols, char[] charArray) {
char[][] matrix1 = new char[rows][cols];
for (int i = 0, idx = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix1[i][j] = charArray[idx++];
}
}
return matrix1;
}
public static void main(String[] args) {
String matrix = "ABCESFCSADEE";
int rows = 3;
int cols = 4;
String str1 = "ABCCED";
String str2 = "ABCB";
HasPathInMatrix test = new HasPathInMatrix();
System.out.println(test.hasPath(matrix, rows, cols, str1));
System.out.println(test.hasPath(matrix, rows, cols, str2));
}
}
13. 机器人的运动范围
java
package Search;
public class CountReachableGrids {
private int res = 0;
public int movingCount(int threshold, int rows, int cols) {
if (rows == 0 || cols == 0) return 0;
if (threshold == 0) return 1;
int[][] matrix = new int[rows][cols];
dfs(matrix, threshold, rows, cols, 0, 0);
return res;
}
private void dfs(int[][] matrix, int threshold, int rows, int cols, int i, int j) {
if (i < 0 || i >= rows || j < 0 || j >= cols || matrix[i][j] == 1) return;
matrix[i][j] = 1;
if (i / 10 + i % 10 + j / 10 + j % 10 > threshold) return;
res++;
dfs(matrix, threshold, rows, cols, i - 1, j);
dfs(matrix, threshold, rows, cols, i + 1, j);
dfs(matrix, threshold, rows, cols, i, j + 1);
dfs(matrix, threshold, rows, cols, i, j - 1);
}
public static void main(String[] args) {
CountReachableGrids test = new CountReachableGrids();
System.out.println(test.movingCount(10, 1, 100));
}
}
38. 字符串的排列
java
package Search;
import java.util.ArrayList;
import java.util.Arrays;
public class PermuteString {
private final ArrayList<String> res = new ArrayList<>();
public ArrayList<String> Permutation(String str) {
// write code here
if (str.isEmpty()) return new ArrayList<>();
char[] charArray = str.toCharArray();
Arrays.sort(charArray); // ⭐ 必须排序!
char[] array = new char[str.length()];
int[] flag = new int[str.length()];
dfs(0, charArray, array, flag);
return res;
}
private void dfs(int i, char[] charArray, char[] array, int[] flag) {
if (i == array.length) {
res.add(new String(array));
return;
}
for (int j = 0; j < charArray.length; j++) {
if (j > 0 && charArray[j] == charArray[j - 1] && flag[j - 1] == 0)
continue;
if (flag[j] == 0) {
array[i] = charArray[j];
flag[j] = 1;
dfs(i + 1, charArray, array, flag);
flag[j] = 0;
}
}
}
public static void main(String[] args) {
PermuteString test = new PermuteString();
System.out.println(test.Permutation("aba"));
}
}