LeetCode:60.单词搜索

目录

1.单词搜索


1.单词搜索

cpp 复制代码
class Solution {
    bool check[7][7];
    int m, n;
public:
    bool exist(vector<vector<char>>& board, string word) 
    {
        m = board.size(), n = board[0].size();
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                if(board[i][j] == word[0])
                {
                    check[i][j] = true;
                    if(dfs(board, i, j, word, 1) == true) return true;
                    check[i][j] = false;
                }
        return false;
    }

    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};

    bool dfs(vector<vector<char>>& board, int i, int j, string word, int pos)
    {
        
        if(pos == word.size()) return true;
        for(int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && check[x][y] == false && board[x][y] == word[pos])
            {
                check[x][y] = true;
                if(dfs(board, x, y, word, pos + 1) == true) return true;
                check[x][y] = false;
            }
        }
        return false;
    }
};
相关推荐
文丑颜不良啊21 小时前
LeetCode2981~3000题目解析
java·leetcode
罗西的思考1 天前
【轻量化复刻大模型 / KIMI K3】Multi-Token Prediction(MTP / EAGLE-3)(2)
人工智能·算法·机器学习
罗西的思考1 天前
【轻量化复刻大模型 / KIMI K3】Multi-Token Prediction(MTP / EAGLE-3)(1)
人工智能·算法·机器学习
武子康1 天前
H3-World 接入 Game Agent:谁来判断门真的打开了?
人工智能·算法·机器学习
文丑颜不良啊1 天前
LeetCode3001~3020题目解析
java·leetcode
武子康1 天前
H3-World 扩展跳跃与开门:新动作怎样进入训练数据?
大数据·人工智能·算法
卡尔曼的BD SLAMer1 天前
北斗导航 |GNSS ION论文集汇总1997到2022(链接在文末)
算法·北斗导航
时间的拾荒人1 天前
Redis Cluster 集群详解:分片算法、搭建、故障处理与扩容实战
数据库·redis·算法·集群
老师我太想进步了20261 天前
【数据结构-8】栈的实现
c语言·数据结构·算法
kyle~1 天前
因子图优化(Factor Graph Optimization)
c++·算法·机器人·传感器