单词搜索-

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 (board[i][j] != word[cnt])

return false;

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

return true;

st[i][j]=true;

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

int x = dx[k] + i, y = dy[k] + j;

if (x < 0 || x >= n || y < 0 || y >= m || st[x][y])

continue;

if (

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

return true;

}

st[i][j]=false;

return false;

}

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

int n = board.size(), m = board[0].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 (board[i][j] == word[0] &&dfs(i, j, n, m, board, word, st, 0))

return true;

}

}

return false;

}

};

相关推荐
bIo7lyA8v20 小时前
算法稳定性分析中的输入扰动建模的技术9
算法
CoderCodingNo20 小时前
【GESP】C++三级真题 luogu-B4499, [GESP202603 三级] 二进制回文串
数据结构·c++·算法
sinat_2869451920 小时前
AI Coding 时代的 TDD:从理念到工程落地
人工智能·深度学习·算法·tdd
ASKED_201921 小时前
从排序到生成:腾讯广告算法大赛 2025 baseline解读
人工智能·算法
田梓燊21 小时前
leetcode 160
算法·leetcode·职场和发展
_深海凉_21 小时前
LeetCode热题100-颜色分类
python·算法·leetcode
hetao17338371 天前
2026-04-09~12 hetao1733837 的刷题记录
c++·算法
6Hzlia1 天前
【Hot 100 刷题计划】 LeetCode 136. 只出现一次的数字 | C++ 哈希表&异或基础解法
c++·算法·leetcode
MWWZ1 天前
最近的一些软件更新
opencv·算法·计算机视觉
CoovallyAIHub1 天前
视频理解新范式:Agent不再被动看视频,LensWalk让它自己决定看哪里
算法·架构·github