美文网首页
矩阵中的路径(剑指offer 面试题66)

矩阵中的路径(剑指offer 面试题66)

作者: 抬头挺胸才算活着 | 来源:发表于2020-03-11 17:24 被阅读0次

在矩阵中找到路径,很容易,递归就可以解决。

    public static boolean isPathInMatrix(String string, char[][] charMatrix){
        if(string==null||string.length()==0||charMatrix==null)
            return false;

        int H = charMatrix.length;
        int W = charMatrix[0].length;

        boolean[][] pathMatrix = new boolean[H][W];
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                pathMatrix[i][j] = false;
            }
        }

        for (int Y = 0; Y < H; Y++) {
            for (int X = 0; X < W; X++) {
                if(isPathInMatrix(string, 0, Y, X, charMatrix, pathMatrix)) {
                    System.out.println(Arrays.deepToString(pathMatrix));
                    return true;
                }
            }
        }

        System.out.println(Arrays.deepToString(pathMatrix));
        return false;
    }

    public static boolean isPathInMatrix(String string, int currentIndex, int Y, int X, char[][] charMatrix, boolean[][] pathMatrix) {
        if(currentIndex>=string.length()) {
            return true;
        }

        int H = charMatrix.length;
        int W = charMatrix[0].length;

        if((Y>=0&&Y<H&&X>=0&&X<W) && string.charAt(currentIndex)==charMatrix[Y][X]){
            pathMatrix[Y][X] = true;

            currentIndex += 1;
            if(currentIndex>=string.length()) {
                return true;
            }else{
                boolean ret = isPathInMatrix(string, currentIndex, Y-1, X, charMatrix, pathMatrix)
                        || isPathInMatrix(string, currentIndex, Y+1, X, charMatrix, pathMatrix)
                        || isPathInMatrix(string, currentIndex, Y, X-1, charMatrix, pathMatrix)
                        || isPathInMatrix(string, currentIndex, Y, X+1, charMatrix, pathMatrix);

                if(!ret) {
                    pathMatrix[Y][X] = false;
                }
                return ret;
            }
        }else{
            return false;
        }
    }

测试代码:

//        String string = "bcced";
        String string = "abcb";
        char[][] charMatrix = new char[][]{
                {'a','b','c','e'},
                {'s','f','c','s'},
                {'a','d','e','e'},
        };
        boolean ret = isPathInMatrix(string, charMatrix);
        System.out.println(ret);

相关文章

  • 矩阵中的路径(剑指offer 面试题66)

    在矩阵中找到路径,很容易,递归就可以解决。 测试代码:

  • 矩阵中的路径

    《剑指offer》面试题12:矩阵中的路径 题目:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有...

  • 剑指offer第二版-12.矩阵中的路径(回溯法)

    本系列导航:剑指offer(第二版)java实现导航帖 面试题12:矩阵中的路径 题目要求:设计一个函数,用来判断...

  • [剑指offer] 矩阵中的路径

    本文首发于我的个人博客:尾尾部落 题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的...

  • 剑指offer——矩阵中的路径

    题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格...

  • 剑指offer 矩阵中的路径

    思想:标准的回溯法实现: 使用hash表标记元素是否已经被访问 使用全局变量以及在函数内定义函数尽量减少代码量

  • ARTS 20201218-1231

    Algorithm: 每周至少做一个 LeetCode 的算法题剑指 Offer 12. 矩阵中的路径[https...

  • 机器人的运动范围

    《剑指offer》面试题13:矩阵中的路径 题目:地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动...

  • 《剑指Offer》回溯 矩阵中的路径

    题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格...

  • 剑指Offer——矩阵中的路径 Java

    题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格...

网友评论

      本文标题:矩阵中的路径(剑指offer 面试题66)

      本文链接:https://www.haomeiwen.com/subject/rnhhjhtx.html