LeetCode 79. Word Search

问题描述

  • Given a 2D board and a word, find if the word exists in the grid.
  • The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
  • Example :

board =
[
[‘A’,’B’,’C’,’E’],
[‘S’,’F’,’C’,’S’],
[‘A’,’D’,’E’,’E’]
]
Given word = “ABCCED”, return true.
Given word = “SEE”, return true.
Given word = “ABCB”, return false.

问题分析

  • 这是一道二维DFS + 回溯 的题目。类似于用DFS走迷宫的问题。
  • 首先判断当前决策的可行性,若不可行,则不继续dfs,若可行,则当前决策由有四种选择,上下左右。进行四个方向的dfs,只要有一个方向找到即可,所以是或的关系
  • 因为一个元素不能多次使用,就相当于一个元素只能遍历一次,所以要对已遍历过的元素设置一个已遍历标志。以下三种方法可以做到
    • 方法1 :设置一个visit[i][j] 数组,用来标志 board[i][j] 是否已经遍历过。
    • 方法2 :不用额外空间,若 board[i][j] 已经遍历过,那么board[i][j] = "#",*dfs之后再恢复,但这样做有局限性*,必须保证原二维数组中不含 "#",防止造成冲突。
    • 方法3 : 不用额外空间,若一个字符 ch(码值小于256) 与 256, 512..异或, ch ^= 256,那么 ch 的 码值一定大于等于了 256,变成了一个非法字符,这样也起到了设置标志的作用,dfs之后再异或回来即可。
  • 注意dfs之后,都要利用回溯恢复 当前元素是否遍历过的标志。

经验教训

  • 二维DFS + 回溯 的套路
  • 如何设置已遍历标志的三种做法

代码实现

  • visit[i][j] 数组法
    public boolean exist(char[][] board, String word) {
        if (board == null || board.length == 0 || board[0] == null || board[0].length == 0 || word == null || word.length() == 0) {
            return false;
        }
        int row = board.length;
        int col = board[0].length;
        boolean[][] visited = new boolean[row][col];
        char[] words = word.toCharArray();
        //从任一点出发
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) {
                if (isExistd(board, visited, words, i ,j , 0)) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isExistd(char[][] board, boolean[][] visited, char[] words, int x, int y, int k) {
        if (k == words.length) {
            return true;
        }
        //若[x,y]已经越界或者 [x,y]已经遍历或者 [x,y]不等于words[k],便停止dfs
        if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || visited[x][y] || board[x][y] != words[k]) {
            return false;
        }
        //将当前元素置为已遍历状态
        visited[x][y] = true;
        //四个方向dfs
        boolean res =  isExistd(board, visited, words, x - 1, y, k + 1) || isExistd(board, visited, words, x + 1, y, k + 1) ||
                        isExistd(board, visited, words, x, y + 1, k + 1) || isExistd(board, visited, words, x, y - 1, k + 1);
        //利用回溯恢复[x,y]的状态
        visited[x][y] = false;
        return res;
    }
  • 不用额外空间法
    public boolean exist(char[][] board, String word) {
        if (board == null || board.length == 0 || board[0] == null || board[0].length == 0 || word == null || word.length() == 0) {
            return false;
        }
        int row = board.length;
        int col = board[0].length;
        char[] words = word.toCharArray();
        //从任一点出发
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) {
                if (isExistd(board, words, i ,j , 0)) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isExistd(char[][] board, char[] words, int x, int y, int k) {
        if (k == words.length) {
            return true;
        }
        if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || board[x][y] != words[k]) {
            return false;
        }
        //将当前设为已遍历状态
        //board[x][y] ^= 256;
        board[x][y] = '#';
        boolean res =  isExistd(board, words, x - 1, y, k + 1) || isExistd(board, words, x + 1, y, k + 1) ||
                        isExistd(board, words, x, y + 1, k + 1) || isExistd(board, words, x, y - 1, k + 1);
        //利用回溯恢复[x,y]的状态
       // board[x][y] ^= 256;
        board[x][y] = words[k];
        return res;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值