当前位置:网站首页>力扣练习——单词搜索
力扣练习——单词搜索
2022-08-02 04:18:00 【qq_43403657】
单词搜索
1.问题描述
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board =
[
[‘A’,‘B’,‘C’,‘E’],
[‘S’,‘F’,‘C’,‘S’],
[‘A’,‘D’,‘E’,‘E’]
]
给定 word = “ABCCED”, 返回 true
给定 word = “SEE”, 返回 true
给定 word = “ABCB”, 返回 false
可使用以下main函数:
int main()
{
vector<vector<char> > board;
string word;
int m,n;
cin>>m;
cin>>n;
char ch;
for(int i=0; i<m; i++)
{
vector<char> aLine;
for(int j=0; j<n; j++)
{
cin>>ch;
aLine.push_back(ch);
}
board.push_back(aLine);
}
cin>>word;
bool res=Solution().exist(board,word);
cout<<(res?"true":"false")<<endl;
return 0;
}
2.输入说明
首先输入board的行数m、列数n,
然后输入m行,每行n个大写或小写英文字母。
最后输入一个单词word,长度在1到100之间,只包含大写和小写英文字母。
1 <= m <= 80
1 <= n <= 80
3.输出说明
输出true或false
4.范例
输入
3 4
ABCE
SFCS
ADEE
ABCCED
输出
true
5.代码
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
//回溯
//board为给定的字符矩阵,map记录当前字符是否访问过,word用来表示目标字符串,index表示当前访问的word下标
bool backtrack(vector<vector<char>>& board, vector<vector<int>>& map, string word, int i, int j, int index) {
//判断下标是否正常,是否已经遍历过,对应的字符是否匹配
if (i < 0 || i == board.size() || j < 0 || j == board[0].size() || map[i][j] || board[i][j] != word[index]) return false;
//遍历到字符串word结尾,并且对应的字符依然匹配
if (index == word.size() - 1 && word[index] == board[i][j]) return true;
map[i][j] = 1;//标记该位置已经访问过
//进行上下左右四个方位相邻点的访问
if (backtrack(board, map, word, i, j - 1, index + 1) ||
backtrack(board, map, word, i - 1, j, index + 1) ||
backtrack(board, map, word, i, j + 1, index + 1) ||
backtrack(board, map, word, i + 1, j, index + 1))
return true;
//回溯到上一种情况
map[i][j] = 0;
return false;
}
bool exist(vector<vector<char> > &board, string word)
{
int row = board.size();//行数
int column = board[0].size();//列数
//注意二维数组的初始化方法
vector<vector<int> >map(row, vector<int>(column,0));//定义访问map初始值均为0
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (backtrack(board, map, word, i, j, 0))
return true;
}
}
return false;
}
int main()
{
vector<vector<char> > board;
string word;
int m, n;
cin >> m;
cin >> n;
char ch;
for (int i = 0; i < m; i++)
{
vector<char> aLine;
for (int j = 0; j < n; j++)
{
cin >> ch;
aLine.push_back(ch);
}
board.push_back(aLine);
}
cin >> word;
bool res = exist(board, word);
cout << (res ? "true" : "false") << endl;
return 0;
}
6.新知识点
vector<vector<int> >map(row, vector<int>(column,0));//定义访问map初始值均为0
//其中row表示行数,column表示列数
边栏推荐
- gergovia's deal tijie
- jetracer_pro_2GB AI Kit system installation instructions
- Scientific research notes (5) SLAC WiFi Fingerprint+ Step counter fusion positioning
- Deep Blue Academy-Visual SLAM Lecture 14-Chapter 6 Homework
- 自定义一个下划线分词器
- 数学建模学习(76):多目标线性规划模型(理想法、线性加权法、最大最小法),模型敏感性分析
- 多主复制下处理写冲突(4)-多主复制拓扑
- falco 【1】入门
- ClickHouse的客户端命令行参数
- 什么是接触电流怎么测?
猜你喜欢
随机推荐
batch_size of deep learning foundation
Excel如何解密工作表保护
Nuscenes数据集总结(下)
Arduino框架下STM32F1/F4系列HID模式程序烧录教程
C语言:结构体总结
2022 Huawei Software Elite Challenge (Preliminary) - Summary
如果有些字段不想进行序列化怎么办?
投资组合分析:portfolio_analysis.Tangenvy_portfolio(切点组合)
STM32 OLED显示屏
Sentinel熔断之非控制台方式总结
【数字IC手撕代码】Verilog固定优先级仲裁器|题目|原理|设计|仿真
[Win11] PowerShell cannot activate Conda virtual environment
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,有什么方案?
吴恩达机器学习系列课程笔记——第七章:正则化(Regularization)
开放原子开源峰会落幕,百度超级链牵头成立XuperCore开源工作组
ScholarOne Manuscripts submits journal LaTeX file and cannot convert PDF successfully!
多主复制下处理写冲突(4)-多主复制拓扑
被大厂强制毕业,两个月空窗期死背八股文,幸好上岸,不然房贷都还不上了
Learn about the sequential storage structure of binary tree - heap
【C语言程序】求直角三角形边长