zoukankan      html  css  js  c++  java
  • 【Offer】[12] 【矩阵中的路径】

    题目描述

      请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入这个格子。 例如 [
      egin{matrix}a & b & t &g \c & f & c & s\j & d & e & h end{matrix}
      ]
    这样的3 X 4矩阵中包含一条字符串bfce的路径,但是矩阵中不包含abfb路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。

    思路分析

    1. 题目中输入的矩阵是一维数组,但是我们一般将其看成二维数组
    2. 回溯法:遍历整个矩阵,判断当前格子是否符合给出的路径,如果是继续搜寻它的上下左右的格子中 是否符合给出的路径,这样递归搜索判断 。其中要给出一个数组来标记当前格子是否被访问过,每次进入格子中时要标记为true,如果这个格子中的上下左右都没有符合路径,则回溯到上一个格子,并将此格子标记为false

    Java代码

    public class Offer012 {
        public static void main(String[] args) {
            char[] matrix = "ABTGCFCSJDEH".toCharArray();
            int rows = 3;
            int cols = 4;
            char[] str = "ATCE".toCharArray();
            if (hasPath(matrix, rows, cols, str))
                System.out.println(" existed.");
            else
                System.out.println(" no existed.");
        }
    
        public static boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
            return Solution1(matrix, rows, cols, str);
        }
    
        /**
         * 
         * @param matrix 矩阵
         * @param rows   行数
         * @param cols   列数
         * @param str    给出的路径
         * @return
         */
        public static boolean Solution1(char[] matrix, int rows, int cols, char[] str) {
            if (matrix == null || rows < 1 || cols < 1 || str == null) {
                return false;
            }
            boolean[] isVisited = new boolean[rows * cols];
            for (boolean b : isVisited) {
                b = false;
            }
            int pathLength = 0; // 代表自己搜寻的路径 所组成字符串 的下标
            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                    if (hasPathCore(matrix, rows, cols, row, col, str, pathLength, isVisited)) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        /**
         * 
         * @param matrix     矩阵
         * @param rows       行数
         * @param cols       列数
         * @param row        当前格子的行
         * @param col        当前格子的列
         * @param str        给出的路径
         * @param pathLength 路径的下标
         * @param isVisited  访问标记数组
         * @return
         */
        private static boolean hasPathCore(char[] matrix, int rows, int cols, int row, int col, char[] str, int pathLength,
                boolean[] isVisited) {
            if (row < 0 || col < 0 || row >= rows || col >= cols || isVisited[row * cols + col] == true
                    || str[pathLength] != matrix[row * cols + col]) {
                // 如果搜寻的格子被访问过 ,如果搜寻的格子 不符合给出的路径
                return false;
            }
            if (pathLength == str.length - 1) {
                return true;
            }
            boolean hasPath = false;
            isVisited[row * cols + col] = true;// 第一个元素设置为已经访问
            // 搜寻下一个元素(上--下--左--右)
            hasPath = hasPathCore(matrix, rows, cols, row, col - 1, str, pathLength + 1, isVisited)
                    || hasPathCore(matrix, rows, cols, row, col + 1, str, pathLength + 1, isVisited)
                    || hasPathCore(matrix, rows, cols, row - 1, col, str, pathLength + 1, isVisited)
                    || hasPathCore(matrix, rows, cols, row + 1, col, str, pathLength + 1, isVisited);
    
            if (!hasPath) { // 回溯标记为false
                isVisited[row * cols + col] = false;
            }
            return hasPath;
        }
    }
    

    代码链接

    剑指Offer代码-Java

  • 相关阅读:
    jquery封装的时间轴
    openlayers实现多图显示
    wms常用操作
    教你如何拔取百度地图POI兴趣点
    北京市地铁线路及站点数据
    Arcgis for js实现北京地铁的展示
    Openlayers 2.X加载高德地图
    Arcgis for js加载百度地图
    常用公共服务接口与java调用实现
    Openlayers 2.X加载天地图
  • 原文地址:https://www.cnblogs.com/haoworld/p/offer12-ju-zhen-zhong-de-lu-jing.html
Copyright © 2011-2022 走看看