回溯法小结

回溯法

字符串中查找一个单词

public class aa{
    public static void main(String[] args) {
        char[] xz= {'a','b','c','e','s','f','t','j','b','w','n','r','v','o','p','g','x','u'};
        char[] xx = {'c','e','s','f','t','q'};

        aa zx = new aa();
        System.out.println(zx.hasPath(xz,xz.length,xx));

    }

    public boolean hasPath(char[] matrix,int length,char[] str){
        if (matrix == null || str == null || str.length == 0 || str.length >matrix.length)return false;
        boolean[] visited = new boolean[matrix.length];
        for (int i = 0; i <matrix.length ; i++) {
            if (DPS(matrix,length,str,i,0,visited))return true;
        }
        return false;
    }

    private boolean DPS(char[] matrix,int length,char[] str,int i, int k,boolean[] visited){
        if (i<0 || i>matrix.length || visited[i] || matrix[i]!=str[k])return false;
        if (k==str.length-1)return true;
        if (DPS(matrix,length,str,i+1,k+1,visited))return true;
        visited[i] = false;
        return false;
    }
}

地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

public class Solution {
public int movingCount(int threshold, int rows, int cols)
    {
        if(rows <= 0 || cols <= 0 || threshold < 0) return 0;
        boolean[] visited = new boolean[rows * cols];
        return dfs(threshold,rows,cols,visited,0,0);
    }
 private  int dfs(int threshold, int rows, int cols, boolean[] visited, int x, int y) {
        if(x < 0 || x >= cols || y < 0 || y >= rows 
                || getDigitSum(x) + getDigitSum(y) > threshold || visited[x + y * cols])
            return 0;//出口
        visited[x + y * cols] = true;//标记
        return 1 + dfs(threshold, rows, cols, visited, x, y - 1)//归
                 + dfs(threshold, rows, cols, visited, x + 1, y)
                 + dfs(threshold, rows, cols, visited, x, y + 1)
                 + dfs(threshold, rows, cols, visited, x - 1, y);
    }
private int getDigitSum(int i) {
        int sum = 0;
        while(i > 0) {
            sum += i % 10;
            i /= 10;
        }
        return sum;
    }
}

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

/**
 * Created by xx on 2018/9/28.
 */
public class news {
    public static void main(String[] args) {
        int n=20;
        news obj = new news();
        char[] xz= {'a','b','c','b','s','f','c','s','a','d','e','e'};
        char[] xx = {'b','c','c','e','d'};
        int rows = 3;
        int cols = 4;
        System.out.println(obj.hasPath(xz,rows,cols,xx));
    }

    public boolean hasPath(char[] matrix,int rows,int cols,char[] str){

        if(matrix == null || matrix.length != rows * cols
                || str == null || str.length == 0
                || str.length > matrix.length) return false;
        boolean[] visited = new boolean[matrix.length];
        for (int j = 0; j < rows; j++) {
            for (int i = 0; i < cols; i++) {//每个节点都有可能是起点
                if(DFS(matrix,rows,cols,str,i,j,0,visited)) return true;
            }
            //这里多了个k=0来充当str的索引
        }
        return false;
    }

        private boolean DFS(char[] matrix, int rows, int cols, char[] str, int i, int j, int k,boolean[] visited){
        if (i < 0 || i >= cols || j < 0 || j >= rows || visited[i + j * cols] || matrix[i + j * cols] != str[k])return false;
        if(k == str.length - 1) return true;
        visited[i + j * cols] = true;
        if(DFS(matrix, rows, cols, str, i, j - 1, k + 1, visited)
                || DFS(matrix, rows, cols, str, i + 1, j, k + 1, visited)
                || DFS(matrix, rows, cols, str, i, j + 1, k + 1, visited)
                || DFS(matrix, rows, cols, str, i - 1, j, k + 1, visited))
            return true;
        visited[i + j * cols] = false;//归
        return false;
    }
}
    原文作者:回溯法
    原文地址: https://blog.csdn.net/xuezhan123/article/details/82960602
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞