79力扣:单词搜索

cpp 复制代码
class Solution {
public:
    bool exist(vector<vector<char>>& board, string word) {
        int key=0;
        int row=board.size();
        int col=board[0].size();
        int wSize=word.size();
        function<void(int,int,int)> dfs=[&](int m,int n,int index){
            if(key==1){return ;}
            if(board[m][n]==word[index]){
                char tmp=board[m][n];
                board[m][n]='*';
                if(index==(word.size()-1)){
                    key=1;
                }else{
                if(m-1>=0){dfs(m-1,n,index+1);}
                if(m+1<=row-1){dfs(m+1,n,index+1);}
                if(n-1>=0){dfs(m,n-1,index+1);}
                if(n+1<=col-1){dfs(m,n+1,index+1);}
                }
                board[m][n]=tmp;
            }
        };
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(board[i][j]==word[0]){
                    dfs(i,j,0);
                    if(key==1) return true;
                }
            }
        }
        return false;
    }
};

对于字符串中的某个字符直接用wordindex表示。

在 C++ 中,std::to_string 函数用于将各种基本类型转换为对应的 std::string 类型。其具体行为取决于参数的类型,如下所示:

  • 整数类型int, long, long long, unsigned int, 等等)将被转换为其十进制表示的字符串。
  • 浮点数类型float, double, long double)将被转换为其小数形式的字符串,通常包括小数点和指数部分。
  • 字符类型char, wchar_t, char16_t, char32_t)将被转换为长度为 1 的字符串,包含字符本身。
  • 布尔类型bool)将被转换为 "true""false" 的字符串。
相关推荐
有点。6 小时前
C++03阶段练习(练习题)
数据结构·算法·图论
周末也要写八哥7 小时前
经典算法实例:游戏中弱角色的数量(二)
算法
是Yu欸7 小时前
鸿蒙PC移植:2048 从网页小游戏到 AI 桌面应用
大数据·人工智能·算法·数据挖掘·openharmony·codex
鹿角片ljp7 小时前
KV Cache 解析
java·算法
liliangcsdn8 小时前
IVOL与偏度因子的对比测量分析
算法
threerocks9 小时前
Jev 入门第一课
算法
西柚研究生12345610 小时前
论文分析17:YOLOv11_UAVNet:无人机航拍图像专用目标检测算法
人工智能·python·深度学习·算法·目标检测
hetao173383711 小时前
2026-09-17 hetao1733837 的刷题记录
c++·算法
午彦琳12 小时前
2026.9.17
数据结构·算法·leetcode
木井巳12 小时前
【记忆化搜索】不同路径
java·算法·leetcode·深度优先·剪枝·推荐算法