剑指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"));
    }
}
相关推荐
3DVisionary5 分钟前
蓝光三维扫描:医疗制造的精度焦虑怎么解
人工智能·算法·制造·蓝光三维扫描·医疗制造·三维检测·义齿检测
好评笔记13 分钟前
机器学习面试八股——常用损失函数
人工智能·深度学习·算法·机器学习·校招
weixin_4684668515 分钟前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
_日拱一卒37 分钟前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
珂朵莉MM1 小时前
第七届全球校园人工智能算法精英大赛-算法巅峰赛产业命题赛第3赛季优化题--束搜索
人工智能·算法
Omics Pro2 小时前
首个!外源天然产物综合性代谢图谱
数据库·人工智能·算法·机器学习·r语言
voidmort2 小时前
3. 微调(Fine-tuning)与强化学习(RL)的核心思想
python·深度学习·算法
人道领域3 小时前
【LeetCode刷题日记】669.修剪二叉搜索树
开发语言·python·算法
2401_868534783 小时前
【无标题】
数据结构·r语言
Mr. zhihao3 小时前
Redis五大高级数据结构:原理-场景-底层-横向对比
数据结构·redis