面试题12 回溯法

/**
 * @ClassName Solution
 * @Author wangyudi
 * @Date 2019/7/14 16:13
 * @Version 1.0
 * @Description
 */

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }

}

public class Solution {
    public boolean findPath(char[][] matrix, String str) {
        //deal with invalid input
        if (str == null || matrix == null) return false;
        //下面默认输入是一个有效的矩形二维数组
        int rows = matrix.length;
        int cols = matrix[0].length;
        boolean[] visited = new boolean[rows * cols]; //存放该数组对应元素是否已被访问信息
        char[] charStr = str.toCharArray();
        int pathIndex = 0;//记录需要寻找字符串的始端
        boolean result = false;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                if (findPathCore(matrix, charStr, pathIndex, row, col, rows, cols, visited)) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    /**
     * 查找路径的迭代函数
     * @param matrix
     * @param charStr
     * @param pathIndex
     * @param row
     * @param col
     * @param rows
     * @param cols
     * @param visited
     * @return
     */
    private boolean findPathCore(char[][] matrix, char[] charStr, int pathIndex, int row, int col, int rows, int cols, boolean[] visited) {
        //1 判断是否已经找到字符串
        if (pathIndex == charStr.length) return true;
        boolean result = false;
        //2 判断二维数组的字符是否对应字符串中的字符
        if (row >= 0 && row < rows &&
                col >= 0 && col < cols &&
                charStr[pathIndex] == matrix[row][col] &&
                visited[row * cols + col] == false) {
            //对应
            //寻找下一个子字符串,并修改访问信息
            pathIndex++;
            visited[row * cols + col] = true;
            //从该元素所在位置的四个方向中寻找下一个字符串
            result = findPathCore(matrix, charStr, pathIndex, row + 1, col, rows, cols, visited) ||
                    findPathCore(matrix, charStr, pathIndex, row - 1, col, rows, cols, visited) ||
                    findPathCore(matrix, charStr, pathIndex, row, col + 1, rows, cols, visited) ||
                    findPathCore(matrix, charStr, pathIndex, row, col - 1, rows, cols, visited);
            //四个方向都没有找到相应的子字符串,则说明该位置不正确
            //修改访问信息,然后回归到上一个位置
            if (!result) {
                visited[row * cols + col] = false;
            }
        }
        return result;
    }

// Test input and output
    public static void main(String[] args) {
        Solution solution = new Solution();
        char[] char1 = {'a', 'b', 't', 'g'};
        char[] char2 = {'c', 'f', 'c', 's'};
        char[] char3 = {'j', 'd', 'e', 'h'};
        char[] char4 = {'a', 'b', 't', 'g'};
        char[][] chars = {char1, char2, char3, char4};
        String str = "bfcc";
        System.out.println(solution.findPath(chars, str));
    }

}

猜你喜欢

转载自www.cnblogs.com/youzoulalala/p/11300007.html
今日推荐