LeetCode 79 Word Search (DFS)

原创 2016年08月31日 10:35:15

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.

For example,
Given board =

[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.


题目链接:https://leetcode.com/problems/word-search/

题目分析:很基础的深度优先搜索,从第一个匹配的点开始搜

public class Solution {
    
    public static int[] dx = {0, 1, 0, -1};
    public static int[] dy = {1, 0, -1, 0};
    
    public boolean DFS(int n, int m, int x, int y, int pos, int len, boolean[][] vis, char[][]board, String word) {
        if (pos > len) {
            return false;
        }
        if (pos == len) {
            return true;
        }
        for (int i = 0; i < 4; i ++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(xx >= 0 && yy >= 0 && xx < n && yy < m && !vis[xx][yy] && board[xx][yy] == word.charAt(pos)) {
                vis[xx][yy] = true;
                if(DFS(n, m, xx, yy, pos + 1, len, vis, board, word)) {
                    return true;
                }
                vis[xx][yy] = false;
            } 
        }
        return false;
    }
    
    public boolean exist(char[][] board, String word) {
        int n = board.length;
        int m = board[0].length;
        int len = word.length();
        if(len == 0) {
            return false;
        }
        boolean[][] vis = new boolean[n][m];
        for (int i = 0; i < n; i ++) {
            Arrays.fill(vis[i], false);
        }
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < m; j ++) {
                if (board[i][j] == word.charAt(0)) {
                    vis[i][j] = true;
                    if (DFS(n, m, i, j, 1, len, vis, board, word)) {
                        return true;
                    }
                    vis[i][j] = false;
                }
            }
        }
        return false;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Leetcode 79 - Word Search(dfs)

题意求一个字符串是否能从矩形内某一点出发,沿上下左右走得到。思路dfs。算法1用use[i][j]记录位置[i, j]是否被访问过,dfs就好。算法2我们访问一个位置的时候,把它记为’#’,访问完还原...
  • Lzedo
  • Lzedo
  • 2017年02月17日 22:27
  • 115

矩阵中的路径--79 word Search--DFS回溯

请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格...

[leetcode] 79.Word Search

题目: Given a 2D board and a word, find if the word exists in the grid.The word can be constructed fr...

leetcode || 79、Word Search

problem: Given a 2D board and a word, find if the word exists in the grid. The word can be con...

【 LeetCode】79. Word Search

问题描述https://leetcode.com/problems/word-search/#/descriptionGiven a 2D board and a word, find if the ...

LeetCode-79.Word Search

https://leetcode.com/problems/word-search/ Given a 2D board and a word, find if the word exists...

Leetcode #79 Word Search

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro...

LeetCode 79. Word Search 二维平面使用回溯法

79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word ca...

(java)leetcode-79:Word Search

Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co...

LeetCode79——Word Search

LeetCode79——Word Search 题意: 在一个字母板(二维数组)上找到连续的一跳折线(或者直线),线上字母连起来可以可以凑成我们的目标单词。 思路: DFS应该没什么问题了。 往上...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 79 Word Search (DFS)
举报原因:
原因补充:

(最多只允许输入30个字)