回溯Leetcode79

定义辅助变量mark标记访问过的元素,设置方向矩阵direct,辅助矩阵inMatrix判断当前的坐标是否越界,可以先采用简单的矩阵实验

回溯回来要重置mark的当前位置为false

//遍历如果当前的字母和字符串的字母相等,则向四个方向拓展
        if(board[x][y] == word.charAt(count)){
            mark[x][y] = true;
            for (int i = 0; i < 4; i++) {
                int newX = x + direct[i][0];
                int newY = y + direct[i][1];
                //下一个方向不越界且未访问过
                if(inMatrix(newX,newY) && !mark[newX][newY]){
                    //终止循环递归
                    if(dfs(mark,newX,newY,count + 1))
                        return true;
                }
            }
            //回溯
            mark[x][y] = false;
        }

代码

/**
 * 需要用一个矩阵标记是否访问过
 *
 * @author :hodor007
 * @date :Created in 2020/11/8
 * @description :
 * @version: 1.0
 */
public class Solution {
    private char[][] board = null;
    private String word = null;
    private int[][] direct = null;

    public boolean exist(char[][] board, String word) {
        this.board = board;
        this.word = word;
        //上右下左
        int[][] _direct = {
                {-1,0},
                {0,1},
                {1,0},
                {0,-1}
        };
        this.direct = _direct;
        int rows = board.length;
        int columns = board[0].length;
        //特判
        if(board == null || columns == 0 || columns == 0){
            return false;
        }
        //判断是否访问过
        boolean[][] mark = new boolean[rows][columns];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if(dfs(mark,i,j,0))
                    return true;
            }
        }
        return false;
    }

    /**
     * @param x     当前坐标
     * @param y     当前坐标
     * @param count 走过的路径
     * @return
     */
    private boolean dfs(boolean[][] mark, int x, int y, int count) {
        //设置循环结束条件
        if (count == word.length() - 1) {
            return board[x][y] == word.charAt(count);
        }

        //遍历如果当前的字母和字符串的字母相等,则向四个方向拓展
        if(board[x][y] == word.charAt(count)){
            mark[x][y] = true;
            for (int i = 0; i < 4; i++) {
                int newX = x + direct[i][0];
                int newY = y + direct[i][1];
                //下一个方向不越界且未访问过
                if(inMatrix(newX,newY) && !mark[newX][newY]){
                    //终止循环
                    if(dfs(mark,newX,newY,count + 1))
                        return true;
                }
            }
            //回溯
            mark[x][y] = false;
        }
        return false;
    }

    /**
     * 判断是否越界
     * @param x
     * @param y
     * @return
     */
    boolean inMatrix(int x,int y){
        int rows = board.length;
        int columns = board[0].length;
        if(x >= 0 && x <= rows - 1 && y >=0 && y <= columns - 1){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
//        char[][] matrix = {
//                {'A','B','C','E'},
//                {'S','F','C','S'},
//                {'A','D','E','E'}
//        };

        char[][] matrix = {
                {'A','B','C','E'},
        };
        Solution solution = new Solution();
        boolean ans = solution.exist(matrix, "ABCE");
        System.out.println(ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值