当前位置:网站首页>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;
}
};

边栏推荐
- Encapsulation manuelle d'un foreach et d'une carte
- [official testerhome] MTSC 2021 Shanghai and Shenzhen PPT download addresses
- Differences between in and not in, exists and not exists in SQL and performance analysis
- Tasks in C #
- MFC General dialog color dialog
- Longest palindrome string
- 22-2-28 there are many things to do at work today, ETH analysis
- Work report of epidemic data analysis platform [6] visual drawing
- Kill session? This cross domain authentication solution is really elegant!
- Enterprise Architect v16
猜你喜欢

Operation of simulated examination platform for theoretical question bank of G2 utility boiler stoker in 2022

Memory protection

According to aiqicha, keep went public in Hong Kong and hit the "first share of online fitness"

In the era of smart retail, Weimeng reshapes the value of "shopping guide"

Data processing and data set preparation

Zabbix6.0 new feature GEOMAP map marker can you use it?

Raspberry pie 4B uses Intel movidius NCS 2 for inference acceleration

D1 Nezha development board power on record

Create a new table in the database. There was no problem before. Today

Introduction to distributed locks
随机推荐
L1-065 "nonsense code" (5 points)
L1-067 Roche limit (10 points)
Work report on epidemic data analysis platform [7] Alibaba cloud related
疫情数据分析平台工作报告【42】CodeNet
Day18 creation and restoration of sparse array
Summary of common interview questions in redis
Manually encapsulate a foreacht and map
Things to challenge
What are the black box test case design methods in software testing methods?
Create a new table in the database. There was no problem before. Today
存储器的保护
LabVIEW about TDMS and Binary Storage Speed
Work report of epidemic data analysis platform [4] cross domain correlation
SQL injection upload one sentence Trojan horse (turn)
Data processing and data set preparation
Memory protection
leetcode797. All possible paths (medium)
分布式锁介绍
Day17 array features array boundary array application traversal array multidimensional array creation and traversal arrays operation array bubble sort
Is it safe for Guojin Securities Commission Jinbao to open an account? How should we choose securities companies?