[Leetcode][DFS]79.WordSearch

77 阅读1分钟

Descrption:

Given an m x n grid of characters board and a string word, return true if word exists in the grid.

The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

Example 1:

Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true

Solution:

This problem can be solved by traversing the grid and performing a depth-first search(DFS) for each possible starting position. if current character matches the corresponding character of the word, we explore all four direction recursively until we find the complete word or exhaust all possibilities.

fun exist(board: Array<CharArray>, word: String): Boolean {
    // record current visited points
    val visited = Array(board.size){BooleanArray(board[0].size){false}}
    
    for (i in board.indices) {
        for (j in board[i].indices) {
            // correct start searching point
            if (board[i][j] == word[0]) {
                visited[i][j] = true
                val find = dfsExist(board, i, j,  1, visited, word)
                visited[i][j] = false
                if (find) {
                    return true
                }
            }
        }
    }
    return false
}

// index is the point of word
fun dfsExist(board: Array<CharArray>, currentX: Int, currentY: Int,
             index: Int, visited: Array<BooleanArray>, word: String): Boolean {
    if (index == word.length) {
        return true
    }
    
    // if we find the word in any direction, we won't search the others directions.
    var result = false
    
    // up
    if (currentX > 0 && board[currentX - 1][currentY] == word[index] && !visited[currentX - 1][currentY]) {
        visited[currentX - 1][currentY] = true
        result = dfsExist(board, currentX - 1, currentY, index + 1, visited, word)
        visited[currentX - 1][currentY] = false
    }
    // right
    if (!result && currentY < board[0].size - 1 && board[currentX][currentY + 1] == word[index] && !visited[currentX][currentY + 1]) {
        visited[currentX][currentY + 1] = true
        result = result || dfsExist(board, currentX, currentY + 1, index + 1, visited, word)
        visited[currentX][currentY + 1] = false
    }

    // down
    if (!result && currentX < board.size - 1 && board[currentX + 1][currentY] == word[index] && !visited[currentX + 1][currentY]) {
        visited[currentX + 1][currentY] = true
        result = result ||dfsExist(board, currentX + 1, currentY, index + 1, visited, word)
        visited[currentX + 1][currentY] = false
    }

    // left
    if (!result && currentY > 0 && board[currentX][currentY - 1] == word[index] && !visited[currentX][currentY - 1]) {
        visited[currentX][currentY - 1] = true
        result = result ||dfsExist(board, currentX, currentY - 1, index + 1, visited, word)
        visited[currentX][currentY - 1] = false
    }

    return result
}