-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_search.rs
84 lines (80 loc) · 2.04 KB
/
word_search.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
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
#![allow(dead_code)]
fn dfs(
board: &[Vec<char>],
word: &[char],
visited: &mut [Vec<bool>],
m: usize,
n: usize,
len: usize,
i: usize,
j: usize,
depth: usize,
) -> bool {
depth == len
|| (i < m && j < n && !visited[i][j] && word[depth] == board[i][j] && {
visited[i][j] = true;
let rez = [0, 1, 0, !0, 0].windows(2).any(|w| {
dfs(
board,
word,
visited,
m,
n,
len,
i.wrapping_add(w[0]),
j.wrapping_add(w[1]),
depth + 1,
)
});
visited[i][j] = false;
rez
})
}
pub fn exist(board: Vec<Vec<char>>, word: String) -> bool {
let m = board.len();
let n = board[0].len();
let word = word.chars().collect::<Vec<_>>();
let len = word.len();
let mut visited = vec![vec![false; n]; m];
(0..m).any(|i| (0..n).any(|j| dfs(&board, &word, &mut visited, m, n, len, i, j, 0)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exist() {
assert_eq!(
exist(
vec![
vec!['A', 'B', 'C', 'E'],
vec!['S', 'F', 'C', 'S'],
vec!['A', 'D', 'E', 'E']
],
"ABCCED".to_string()
),
true
);
assert_eq!(
exist(
vec![
vec!['A', 'B', 'C', 'E'],
vec!['S', 'F', 'C', 'S'],
vec!['A', 'D', 'E', 'E']
],
"SEE".to_string()
),
true
);
assert_eq!(
exist(
vec![
vec!['A', 'B', 'C', 'E'],
vec!['S', 'F', 'C', 'S'],
vec!['A', 'D', 'E', 'E']
],
"ABCB".to_string()
),
false
);
}
}