剑指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"));
    }
}
相关推荐
~|Bernard|25 分钟前
二.go语言中map的底层原理(2026-5-8)
算法·golang·哈希算法
AbandonForce33 分钟前
哈希表(HashTable,散列表)个人理解
开发语言·数据结构·c++·散列表
mask哥37 分钟前
力扣算法java实现汇总整理(下)
java·算法·leetcode
代码中介商38 分钟前
栈结构完全指南:顺序栈实现精讲
c语言·开发语言·数据结构
样例过了就是过了1 小时前
LeetCode热题100 编辑距离
数据结构·c++·算法·leetcode·动态规划
wearegogog1231 小时前
MATLAB椭圆参数检测算法实现
数据库·算法·matlab
secondyoung1 小时前
Markdown数学公式语法速查手册
算法·编辑器·markdown·latex
君义_noip1 小时前
CSP-S 2025 提高级 第一轮(初赛) 阅读程序(1)
算法·深度优先·信息学奥赛·初赛
小O的算法实验室1 小时前
2026年IEEE TEVC,知识引导的竞争进化算法用于多解传感器-武器-目标分配问题,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
khalil10202 小时前
代码随想录算法训练营Day-46 动态规划13 | 647. 回文子串、516.最长回文子序列、动态规划总结
数据结构·c++·算法·leetcode·动态规划·回文子串·回文子序列