LintCode 123 · Word Search (DFS字符处理经典题!)

123 · Word Search

Algorithms

Medium

Description

Given a 2D board and a string word, find if the string word exists in the grid.

The string 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.

The dimension of the letter matrix does not exceed 100, and the length of the string does not exceed 100.

Example

Example 1:

Input:

board = ["ABCE","SFCS","ADEE"]

word = "ABCCED"

Output:

true

Explanation:

[

A B C E

S F C S

A D E E

]

(0,0)->(0,1)->(0,2)->(1,2)->(2,2)->(2,1)

Example 1:

Input:

board = ["z"]

word = "z"

Output:

true

Explanation:

[ z ]

(0,0)

解法1:DFS+hashset。

很多小地方需要注意,特别是visited[][]数组什么时候clear掉。

cpp 复制代码
class Solution {
public:
    /**
     * @param board: A list of lists of character
     * @param word: A string
     * @return: A boolean
     */
    bool exist(vector<vector<char>> &board, string &word) {
        int m = board.size();
        if (m == 0) return word.empty();
        int n = board[0].size();
        string sol = "";
        for (int i = 0; i < word.size(); i++) {
            string tmpStr = word.substr(0, i + 1);
            s.insert(tmpStr);
        }
        vector<vector<bool>> visited;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                sol.clear(); //这里每次都要clear!不然后面的操作就append到s上面!
                visited.resize(m, vector<bool>(n, false));
                sol += board[i][j];
                if (check(board, word, sol, visited, i, j)) return true;
            }
        }
        return false;
    }
private:
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    set<string> s;
    bool check(vector<vector<char>> &board, string &word, string sol, vector<vector<bool>> &visited, int x, int y) {
        if (s.find(sol) == s.end()) return false;
        if (sol == word) {
            return true;
        }
        if (sol.size() >= word.size()) return false;
        visited[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int newX = x + dx[i];
            int newY = y + dy[i];
            if (newX >= 0 && newX < board.size() && newY >= 0 && newY < board[0].size() && !visited[newX][newY]) {
                sol += board[newX][newY];
                if (check(board, word, sol, visited, newX, newY)) return true;
                else sol.pop_back();  //这一行重要!因为sol还要被for循环的其它i用到!
            }
        }
        visited[x][y] = false; //这一行重要,只有当dfs能够一直往下进行,visited[][]才不用动,否则如果dfs中途退出,visited[][]要clear。不然就跟后面操作冲突!
        return false; //这里别忘了,不然默认可能会返回true!!!
    }
};

二刷:不需要set。每次比较当前位置的字符就可以了。

cpp 复制代码
class Solution {
public:
    /**
     * @param board: A list of lists of character
     * @param word: A string
     * @return: A boolean
     */
    bool exist(vector<vector<char>> &board, string &word) {
        int m = board.size();
        if (m == 0) return word.empty();
        int n = board[0].size();
        string sol = "";
        vector<vector<bool>> visited;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                sol.clear();
                visited.resize(m, vector<bool>(n, false));
                sol += board[i][j];
                if (check(board, word, sol, 0, visited, i, j)) return true;
            }
        }
        return false;
    }
private:
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    bool check(vector<vector<char>> &board, string &word, string sol, int pos, vector<vector<bool>> &visited, int x, int y) {
        if (sol[pos] != word[pos]) return false;
        if (sol == word) {
            return true;
        }
        //if (sol.size() >= word.size()) return false; //这一行不需要!如果不匹配早就返回了,匹配上面也会返回true。
        visited[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int newX = x + dx[i];
            int newY = y + dy[i];
            if (newX >= 0 && newX < board.size() && newY >= 0 && newY < board[0].size() && !visited[newX][newY]) {
                sol += board[newX][newY];
                if (check(board, word, sol, pos + 1, visited, newX, newY)) return true;
                else sol.pop_back();  //这一行重要!因为sol还要被for循环的其它i用到!
            }
        }
        visited[x][y] = false; //这一行重要,只有当dfs能够一直往下进行,visited[][]才不用动,否则如果dfs中途退出,visited[][]要clear。不然就跟后面操作冲突!
        return false;
    }
};

三刷: DFS+Trie

TBD

四刷:BFS

TBD

相关推荐
PythonFun1 小时前
让Word插上AI的翅膀:如何把DeepSeek装进Word
开发语言·c#·word
shepherd枸杞泡茶2 小时前
第4章 4.4 EF Core数据库迁移 Add-Migration UpDate-Database
数据库·c#·asp.net·.net·.netcore
C#Thread4 小时前
C#上位机--简述
开发语言·c#
幻想趾于现实5 小时前
C#文件操作
开发语言·c#
C66668889 小时前
C#最新语言特性
开发语言·数据库·c#
Nita.19 小时前
设计模式| 观察者模式 Observer Pattern详解
观察者模式·设计模式·c#
旅僧20 小时前
代码随想录-- 第一天图论 --- 岛屿的数量
算法·深度优先·图论
C#Thread1 天前
C#上位机--循环语句
开发语言·c#
软件黑马王子1 天前
Unity游戏制作中的C#基础(3)加减乘除算术操作符,比较运算符,逻辑与,或运算符
开发语言·unity·c#
Erik_LinX1 天前
算法日记27:完全背包(DFS->记忆化搜索->倒叙DP->顺序DP->空间优化)
算法·深度优先