问题
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
给定 word = "ABCCED", 返回 true.
给定 word = "SEE", 返回 true.
给定 word = "ABCB", 返回 false.
题目链接:https://leetcode-cn.com/problems/word-search
解法:
深度优先算法
代码
#include<iostream>
#include<vector>
//#include<algorithm>
using namespace std;
class Solution{
public:
bool cross_exist(vector<vector<char>>& board, string word, pair<int, int> start, int& row, int& col)
{
/*
* 从给出的起始点 start 上下左右 寻找符合条件的单词
*/
int p1 = start.first, p2 = start.second;
char c = word[0];
if (word.empty() != true) word.erase(word.begin(), word.begin() + 1);
else return true;
// 下
if (p1 != row - 1) {
pair<int, int> v = { p1 + 1, p2 };
if (board[v.first][v.second] == c) {
board[v.first][v.second] = '.';
bool ans = cross_exist(board, word, v, row, col); // 递归
board[v.first][v.second] = c;
if (ans) return true;
}
}
// 上
if (p1 != 0) {
pair<int, int> v = { p1 - 1, p2 };
if (board[v.first][v.second] == c) {
board[v.first][v.second] = '.';
bool ans = cross_exist(board, word, v, row, col);
board[v.first][v.second] = c;
if (ans) return true;
}
}
// 左
if (p2 != 0) {
pair<int, int> v = { p1 , p2 - 1 };
if (board[v.first][v.second] == c) {
board[v.first][v.second] = '.';
bool ans = cross_exist(board, word, v, row, col);
board[v.first][v.second] = c;
if (ans) return true;
}
}
// 右
if (p2 != col - 1) {
pair<int, int> v = { p1 , p2 + 1 };
if (board[v.first][v.second] == c) {
board[v.first][v.second] = '.';
bool ans = cross_exist(board, word, v, row, col);
board[v.first][v.second] = c;
if (ans) return true;
}
}
return false;
}
bool exist(vector<vector<char>>& board, string word)
{
int row = board.size();//行数
int col = board[0].size(); // 列数
vector<vector<int>> pos1 = {};
if (word.size() > (row * col)) return false; //如果 string太长,一定是找不到单词的
// 遍历找第一个字母, 所有结果保存起来
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == word[0])
pos1.push_back({ i,j });
}
}
if (pos1.empty()) return false; // 找不到第一个字母,自然是找不到单词的
else {
char c = word[0];
word.erase(word.begin(), word.begin() + 1);
// 对所有满足第一单词的位置进行遍历 调用cross_exist()
for (auto x : pos1) {
pair<int, int> start;
start.first = x[0];
start.second = x[1];
// 修改 board, 防止单词被使用多次
board[start.first][start.second] = '.';
bool ans = cross_exist(board, word, start, row, col);
// 改回来
board[start.first][start.second] = c;
if (ans == true) return true;
}
return false;
}
}
};
int main() {
Solution solv;
// 测试1
//vector<vector<char>> board = { {'A','B','C','E'},
// {'S','F','E','S'},
// {'A','D','E','E'} };
//string word = "CESEEE";
// 测试2
vector<vector<char>> board = { {'A','A','A','A'},
{'A','A','A','A'},
{'A','A','A','A'},
{'A','A','A','A'},
{'A','A','A','B'} };
string word = "AAAAAAAAAAAAAAAAAAAA";
auto ans = solv.exist(board, word);
cout << ans << endl;
return 0;
}
本文详细介绍了一种解决LeetCode单词搜索问题的深度优先搜索算法。通过对给定的二维网格和单词,查找单词是否能在网格中按字母顺序,通过相邻单元格的字母构成。文章提供了详细的算法解释和C++代码实现。
546

被折叠的 条评论
为什么被折叠?



