当前位置:网站首页>力扣79单词搜索
力扣79单词搜索
2022-06-26 03:25:00 【瀛台夜雪】
力扣79单词搜索
题目详情
给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
输入输出样例
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:true
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
输出:true
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
输出:false
解法:回溯算法
bool exist(vector<vector<char>>&board,string word)
{
int rLength=board.size();
int cLength=board[0].size();
//建立二维数组,用来表示位置是否被访问过,数组大小与board的大小需要一致
vector<vector<int>>visitesd(rLength,vector<int>(cLength));
for(int i=0;i<rLength;i++)
{
for(int j=0;j<cLength;j++)
{
//判断是否属于单词搜索
if(check(board,visitesd,i,j,word,0))
{
return true;
}
}
}
return false;
}
//k指代当前是word的第几个字符
bool check(vector<vector<char>>&board,vector<vector<int>>&visited,int i,int j,string word,int k)
{
if(board[i][j]!=word[k])
{
return false;
}
else if(k==word.size()-1)
{
return true;
}
//当word[k]在board中存在,但并未到字符串末尾时
//1.首先将标志位设置为true
visited[i][j]=true;
//建立方向数组,标记可以移动的方向
vector<pair<int,int>>direction{
{
0,1},{
0,-1},{
1,0},{
-1,0}};
//标记周围是否存在相同的字符串
bool res=false;
for(auto dir:direction)
{
//newi,newj 代表新的坐标值
int newi=i+dir.first;
int newj=j+dir.second;
//判断newi和newj是否超出边界或则不存在
if(newi>=0&&newi<board.size()&&newj>=0&&newj<board[0].size())
{
//当该值并没有被访问过
if(!visited[newi][newj])
{
//从里到外的循环
bool flag=check(board,visited,newi,newj,word,k+1);
if(flag)
{
//当下一个字符存在的时候便跳出循环
res=true;
break;
}
}
}
}
//重置标记位
visited[i][j]=false;
return res;
}
边栏推荐
- MySQL advanced part (IV: locking mechanism and SQL optimization)
- 360 second understanding of smartx hyper converged infrastructure
- Uni app custom navigation bar component
- Run multiple main functions in the clion project
- Do you want to add a key to the applet or for sequence?
- Navicat16 wireless trial
- Uni app custom selection date 2 (September 16, 2021)
- Kotlin quick start
- HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct
- js实现文字跑马灯效果
猜你喜欢

Multimedia elements, audio, video

ABP framework

分割、柱子、list

String到底能不能改变?

【LOJ#6718】九个太阳「弱」化版(循环卷积,任意模数NTT)

Cloud Computing Foundation -0

Qixia fire department carries out fire safety training on construction site

Nepal graph learning Chapter 3_ Multithreading completes 6000w+ relational data migration

Camera-memory内存泄漏分析(二)

Open Camera异常分析(一)
随机推荐
MySQL开发环境
Camera-CreateCaptureSession
机器学习笔记 - 时间序列的趋势分量
Gradient
USB peripheral driver - Enumeration
XGBoost, lightGBM, CatBoost——尝试站在巨人的肩膀上
Add an "open search description" to the site to adapt to the browser's "site search"“
mysql存储过程
ASP. Net startup and running mechanism
View of MySQL
Is the compass app regular? Is it safe or not
Various errors in kitti2bag installation
Todolist incomplete, completed
mysql存儲過程
渐变
【哈希表】很简单的拉链法哈希结构,以至于效果太差,冲突太多,链表太长
2020 summary: industrial software development under Internet thinking
After Ali failed to start his job in the interview, he was roast by the interviewer in the circle of friends (plug)
Uni app swiper rotation chart (full screen / card)
栖霞消防开展在建工地消防安全培训