单词搜索-

const int N = 20;

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

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

class Solution {

public:

bool dfs(int i, int j, int n, int m, vector<vector<char>>& board,

string& word, vector<vector<bool>>& st, int cnt) {

if (boardij != wordcnt)

return false;

if (cnt == word.size()-1)

return true;

stij=true;

for (int k = 0; k < 4; ++k) {

int x = dxk + i, y = dyk + j;

if (x < 0 || x >= n || y < 0 || y >= m || stxy)

continue;

if (

dfs(x, y, n, m, board, word, st, cnt+1))

return true;

}

stij=false;

return false;

}

bool exist(vector<vector<char>>& board, string word) {

int n = board.size(), m = board0.size();

string path;

for (int i = 0; i < n; ++i) {

for (int j = 0; j < m; ++j) {

vector<vector<bool>> st(n, vector<bool>(m, false));

if (boardij == word0 &&dfs(i, j, n, m, board, word, st, 0))

return true;

}

}

return false;

}

};

相关推荐
信奥卷王3 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵3 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG5 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z5 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛6 小时前
cpp学习笔记--常量指针
java·开发语言·算法
GPU实战笔记6 小时前
云端 GPU 训练的账户余额提醒:它能说明什么,不能说明什么?
深度学习·算法·成本管理·gpu云计算·云端训练
浮链序6 小时前
怎么证明"你这个模型是偷我的"——把蒸馏变成取证工具
算法·安全·llm
小O的算法实验室6 小时前
IEEE TMC,面向异构物联网的多目标量子行为拓扑优化算法
算法
liliangcsdn7 小时前
派息率指标的应用探索和分析
大数据·算法