当前位置:网站首页>Li Kou 79 word search
Li Kou 79 word search
2022-06-26 03:52:00 【Yingtai night snow】
Power button 79 Word search
Topic details
Given a m x n Two dimensional character grid board And a string word word . If word Exists in the grid , return true ; otherwise , return false .
The words must be in alphabetical order , It's made up of letters in adjacent cells , among “ adjacent ” Cells are those adjacent horizontally or vertically . Letters in the same cell are not allowed to be reused .
I/o sample
Input :board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output :true
Input :board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output :true
Input :board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output :false
solution : Backtracking algorithm
bool exist(vector<vector<char>>&board,string word)
{
int rLength=board.size();
int cLength=board[0].size();
// Create a two-dimensional array , Used to indicate whether the location has been accessed , The array size is the same as board The size of needs to be consistent
vector<vector<int>>visitesd(rLength,vector<int>(cLength));
for(int i=0;i<rLength;i++)
{
for(int j=0;j<cLength;j++)
{
// Determine whether it belongs to word search
if(check(board,visitesd,i,j,word,0))
{
return true;
}
}
}
return false;
}
//k Indicates that the current is word The first character of
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;
}
// When word[k] stay board in , But not to the end of the string
//1. First set the flag bit to true
visited[i][j]=true;
// Create an array of directions , Mark the direction in which you can move
vector<pair<int,int>>direction{
{
0,1},{
0,-1},{
1,0},{
-1,0}};
// Whether the same string exists around the tag
bool res=false;
for(auto dir:direction)
{
//newi,newj Represents the new coordinate value
int newi=i+dir.first;
int newj=j+dir.second;
// Judge newi and newj Whether it is beyond the boundary or does not exist
if(newi>=0&&newi<board.size()&&newj>=0&&newj<board[0].size())
{
// When this value has not been accessed
if(!visited[newi][newj])
{
// Circulation from the inside out
bool flag=check(board,visited,newi,newj,word,k+1);
if(flag)
{
// When the next character exists, it will jump out of the loop
res=true;
break;
}
}
}
}
// Reset tag bit
visited[i][j]=false;
return res;
}
边栏推荐
- R语言与机器学习
- Solve the problem that the uniapp plug-in Robin editor reports an error when setting the font color and background color
- Some mobile phones open USB debugging, and the solution to installation failure
- MySQL addition, deletion, query and modification (Advanced)
- I/O 虚拟化技术 — UIO Framework
- . Net core learning journey
- Machine learning notes - trend components of time series
- 2022.6.23-----leetcode.30
- navicat16无线试用
- [paper notes] learning to grasp with primitive shaped object policies
猜你喜欢

Quanergy welcomes Lori sundberg as chief human resources officer

Redux thunk simple case, advantages, disadvantages and thinking

Flask入门

You cannot call Glide. get() in registerComponents(), use the provided Glide instance instead

Analysis of camera memory memory leakage (II)

MySQL高級篇第一章(linux下安裝MySQL)【下】
![MySQL advanced Chapter 1 (installing MySQL under Linux) [2]](/img/24/0795ebd2a0673d48c5e0f9d18842d1.png)
MySQL advanced Chapter 1 (installing MySQL under Linux) [2]

MapReduce执行原理记录

ASP. Net core introduction

面试阿里测开岗失败后,被面试官在朋友圈吐槽了......(心塞)
随机推荐
Nebula Graph学习篇3_多线程完成6000w+关系数据迁移
虚拟化是什么意思?包含哪些技术?与私有云有什么区别?
How to solve the problem that iterative semi supervised training is difficult to implement in ASR training? RTC dev Meetup
开源!ViTAE模型再刷世界第一:COCO人体姿态估计新模型取得最高精度81.1AP
Navicat16 wireless trial
Popupwindow utility class
评价——层次分析
User control custom DependencyProperty
TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor
[paper notes] learning to grasp with primitive shaped object policies
169. most elements
三元损失英文版
Binary search
[LOJ 6718] nine suns' weakened version (cyclic convolution, arbitrary modulus NTT)
WebRTC系列-网络传输之6-Connections裁剪
matplotlib多条折线图,点散图
Prism framework project application - Navigation
navicat16无线试用
动态线段树leetcode.715
MySQL advanced part (IV: locking mechanism and SQL optimization)