-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
87 lines (78 loc) · 2.61 KB
/
main.cpp
File metadata and controls
87 lines (78 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Source: https://leetcode.com/problems/word-search
// Title: Word Search
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an `m x n` grid of characters `board` and a string `word`, return `true` if `word` exists in the grid.
//
// The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
//
// **Example 1:**
//
// https://assets.leetcode.com/uploads/2020/11/04/word2.jpg
//
// ```
// Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
// Output: true
// ```
//
// **Example 2:**
//
// https://assets.leetcode.com/uploads/2020/11/04/word-1.jp
//
// ```
// Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
// Output: true
// ```
//
// **Example 3:**
//
// https://assets.leetcode.com/uploads/2020/10/15/word3.jpg
//
// ```
// Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
// Output: false
// ```
//
// **Constraints:**
//
// - `m == board.length`
// - `n = board[i].length`
// - `1 <= m, n <= 6`
// - `1 <= word.length <= 15`
// - `board` and `word` consists of only lowercase and uppercase English letters.
//
// **Follow up:** Could you use search pruning to make your solution faster with a larger `board`?
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <cstdint>
#include <vector>
using namespace std;
// DFS + Backtracking
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
int m = board.size(), n = board[0].size(), l = word.size();
// Backtracking state, use uint8_t to avoid vector<bool>
auto visited = vector(m, vector(n, uint8_t(0)));
// DFS, Use Y-combinator
auto dfs = [&](auto&& self, int i, int j, int k) -> bool {
if (k == l) return true;
if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || board[i][j] != word[k]) return false;
visited[i][j] = true;
if (self(self, i - 1, j, k + 1)) return true;
if (self(self, i + 1, j, k + 1)) return true;
if (self(self, i, j - 1, k + 1)) return true;
if (self(self, i, j + 1, k + 1)) return true;
visited[i][j] = false;
return false;
};
// Loop
for (auto i = 0; i < m; ++i) {
for (auto j = 0; j < n; ++j) {
if (dfs(dfs, i, j, 0)) return true;
}
}
return false;
}
};