剑指Offer算法题(九)搜索

目录

[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"));
    }
}
相关推荐
JosieBook3 小时前
【C#】C# 所有关键字总结
开发语言·算法·c#
无忧智库3 小时前
算力、算法、数据三位一体:构建城市级AI大模型算力池的全景式解构与未来展望(WORD)
大数据·人工智能·算法
蓝天守卫者联盟13 小时前
2026乙酸乙酯回收设备厂家选型与技术实践
java·jvm·python·算法
DDzqss3 小时前
3.25打卡day45
c++·算法
爆更小小刘3 小时前
2.3.1_2 浮点数的表示 IEEE 754(例题训练)
算法
赫瑞3 小时前
Java中的 Dijkstra 算法
java·算法
小羊羔heihei4 小时前
Python编程实战:12道趣味算法题
笔记·python·学习·其他·算法·学习方法·交友
三维重建-光栅投影4 小时前
PCL之RANSAC实践
算法
weixin_457760004 小时前
深入解析 Beam Search:从原理到实践的高效解码算法
python·算法