-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.rs
47 lines (46 loc) · 1.27 KB
/
Solution.rs
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
impl Solution {
fn dfs(
i: usize,
j: usize,
c: usize,
word: &[u8],
board: &Vec<Vec<char>>,
vis: &mut Vec<Vec<bool>>,
) -> bool {
if (board[i][j] as u8) != word[c] {
return false;
}
if c == word.len() - 1 {
return true;
}
vis[i][j] = true;
let dirs = [[-1, 0], [0, -1], [1, 0], [0, 1]];
for [x, y] in dirs.into_iter() {
let i = x + (i as i32);
let j = y + (j as i32);
if i < 0 || i == (board.len() as i32) || j < 0 || j == (board[0].len() as i32) {
continue;
}
let (i, j) = (i as usize, j as usize);
if !vis[i][j] && Self::dfs(i, j, c + 1, word, board, vis) {
return true;
}
}
vis[i][j] = false;
false
}
pub fn exist(board: Vec<Vec<char>>, word: String) -> bool {
let m = board.len();
let n = board[0].len();
let word = word.as_bytes();
let mut vis = vec![vec![false; n]; m];
for i in 0..m {
for j in 0..n {
if Self::dfs(i, j, 0, word, &board, &mut vis) {
return true;
}
}
}
false
}
}