-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlib.rs
138 lines (132 loc) · 3.19 KB
/
lib.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
struct Solution {}
impl Solution {
pub fn exist(board: Vec<Vec<char>>, word: String) -> bool {
let word_vec: Vec<&str> = word.split("").collect(); // string转vector
let start_char = word_vec[1]; // 记录开头的字母
let mut path: Vec<Vec<bool>> = vec![vec![false; board[0].len()]; board.len()]; // 记录当前路径有没有被访问
for i in 0..board.len() {
let board_item = board[i].clone();
for j in 0..board_item.len() {
if board[i][j].to_string() == start_char {
if check(&word_vec, &board, i as isize, j as isize, 1, &mut path) {
// 找到第一个字母开头的坐标,可能有多个第一个字母出现
return true;
}
path = vec![vec![false; board[0].len()]; board.len()]; // 清空之前的path记录
}
}
}
return false;
}
}
fn check(
word_vec: &Vec<&str>,
board: &Vec<Vec<char>>,
start_row: isize,
start_column: isize,
index: usize, // 当前正在找第几个字母
path: &mut Vec<Vec<bool>>,
) -> bool {
// ["",A,B,C,C,E,D,""]
if word_vec[index] == "" {
// 已经匹配完所有的字母了
return true;
}
if start_row < 0
|| start_column < 0
|| start_row as usize == board.len()
|| start_column as usize == board[0].len()
|| path[start_row as usize][start_column as usize]
{
// 越界或者当前路径已经访问的情况直接返回false
return false;
}
if word_vec[index] != board[start_row as usize][start_column as usize].to_string() {
return false;
}
path[start_row as usize][start_column as usize] = true;
if check(
&word_vec,
board,
start_row + 1,
start_column,
index + 1,
&mut path.clone(),
) {
// 往下面找
if word_vec[index + 1] == "" {
// 已经匹配完所有的字母了
return true;
}
path[start_row as usize + 1][start_column as usize] = true;
return true;
}
if check(
// 往左边找
&word_vec,
board,
start_row,
start_column - 1,
index + 1,
&mut path.clone(),
) {
if word_vec[index + 1] == "" {
// 已经匹配完所有的字母了
return true;
}
path[start_row as usize][start_column as usize - 1] = true;
return true;
}
if check(
// 往右边找
&word_vec,
board,
start_row,
start_column + 1,
index + 1,
&mut path.clone(),
) {
if word_vec[index + 1] == "" {
// 已经匹配完所有的字母了
return true;
}
path[start_row as usize][start_column as usize + 1] = true;
return true;
}
if check(
// 往上找
&word_vec,
board,
start_row - 1,
start_column,
index + 1,
&mut path.clone(),
) {
if word_vec[index + 1] == "" {
// 已经匹配完所有的字母了
return true;
}
// 往上面找
path[start_row as usize - 1][start_column as usize] = true;
return true;
}
return false;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tests() {
println!(
"{:?}",
Solution::exist(
vec![
vec!['A', 'B', 'C', 'E'],
vec!['S', 'F', 'E', 'S'],
vec!['A', 'D', 'E', 'E']
],
"ABCESEEE".to_string(),
)
)
}
}