当前位置:网站首页>1009 word search
1009 word search
2022-06-12 04:41:00 【-Linzeyu】
The title is as follows
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 .
Example 1:
Input :board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”
Output :true
Example 2:
Input :board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “SEE”
Output :true
Example 3:
Input :board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCB”
Output :false
How to solve the problem

class Solution
{
public:
bool check(vector<vector<char>>& board, vector<vector<int>>& visited, int i, int j, string& s, int k)
{
if (board[i][j] != s[k])
{
return false;
}
else if (k == s.length() - 1)
{
return true;
}
visited[i][j] = true;
vector<pair<int, int>> directions{
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0} };
bool result = false;
for (const auto& dir : directions)
{
int newi = i + dir.first, newj = j + dir.second;
if (newi >= 0 && newi < board.size() && newj >= 0 && newj < board[0].size())
{
if (!visited[newi][newj])
{
bool flag = check(board, visited, newi, newj, s, k + 1);
if (flag)
{
result = true;
break;
}
}
}
}
visited[i][j] = false;
return result;
}
bool exist(vector<vector<char>>& board, string word)
{
int h = board.size(), w = board[0].size();
vector<vector<int>> visited(h, vector<int>(w));
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
bool flag = check(board, visited, i, j, word, 0);
if (flag)
{
return true;
}
}
}
return false;
}
};

边栏推荐
- Tasks in C #
- SQL injection upload one sentence Trojan horse (turn)
- Work report of epidemic data analysis platform [6.5] epidemic map
- QT compiling security video monitoring system 43- picture playback
- Differences between in and not in, exists and not exists in SQL and performance analysis
- Encapsulation manuelle d'un foreach et d'une carte
- EN in Spacey_ core_ web_ SM installation problems
- Function realization and application of trait
- Labor
- [SC] OpenService FAILED 5: Access is denied.
猜你喜欢

2022 self study materials for Zhejiang computer level III network and security technology examination (1) (updated on 2.28)

Mysql主从搭建与Django实现读写分离

Enterprise Architect v16
![Work report of epidemic data analysis platform [6] visual drawing](/img/cc/9eaff451068d0efb174b58719c700e.png)
Work report of epidemic data analysis platform [6] visual drawing

How to use union all in LINQ- How to use union all in LINQ?

Simple Tetris

Tasks in C #

如何制作数据集并基于yolov5训练成模型并部署

AI and logistics Patent

eBPF系列学习(4)了解libbpf、CO-RE (Compile Once – Run Everywhe) | 使用go开发ebpf程序(云原生利器cilium ebpf )
随机推荐
QT compile 45 graphic report of security video monitoring system
Using datetime in MySQL
kali下安装pycharm并创建快捷访问
Question for the 3D printing lattice?
CCF access control system
MFC General dialog color dialog
Ebpf series learning (4) learn about libbpf, co-re (compile once – run everywhere) | use go to develop ebpf programs (cloud native tool cilium ebpf)
疫情数据分析平台工作报告【4】跨域相关
AI and logistics Patent
Simple Tetris
Memory protection
Epidemic data analysis platform work report [8.5] additional crawlers and drawings
Longest palindrome string
WiFi module scheme of the wireless Internet of things, esp32-s3 chip technology, helps the equipment to be intelligent
Tasks in C #
Work report of epidemic data analysis platform [6.5] epidemic map
L1-067 Roche limit (10 points)
Interview must ask: summary of ten classic sorting algorithms
How to make datasets, train them into models and deploy them based on yolov5
Oracle's instr()