-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathju-zhen-zhong-de-lu-jing-lcof.rs
78 lines (67 loc) · 2.11 KB
/
ju-zhen-zhong-de-lu-jing-lcof.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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
pub fn exist(mut board: Vec<Vec<char>>, word: String) -> bool {
let chars: Vec<char> = word.chars().collect();
for i in 0..board.len() {
for j in 0..board[i].len() {
if board[i][j] == chars[0] {
let x = board[i][j];
board[i][j] = '0';
if Self::scan(i, j, &mut board[..], &chars[1..]) {
return true;
}
board[i][j] = x;
}
}
}
false
}
fn scan(i: usize, j: usize, board: &mut [Vec<char>], word: &[char]) -> bool {
if word.is_empty() {
return true;
}
if i > 0 {
if board[i - 1][j] == word[0] {
let x = board[i - 1][j];
board[i - 1][j] = '0';
if Self::scan(i - 1, j, board, &word[1..]) {
return true;
}
board[i - 1][j] = x;
}
}
if j > 0 {
if board[i][j - 1] == word[0] {
let x = board[i][j - 1];
board[i][j - 1] = '0';
if Self::scan(i, j - 1, board, &word[1..]) {
return true;
}
board[i][j - 1] = x;
}
}
if i < board.len() - 1 {
if board[i + 1][j] == word[0] {
let x = board[i + 1][j];
board[i + 1][j] = '0';
if Self::scan(i + 1, j, board, &word[1..]) {
return true;
}
board[i + 1][j] = x;
}
}
if i <= board.len() - 1 && j < board[i].len() - 1 {
if board[i][j + 1] == word[0] {
let x = board[i][j + 1];
board[i][j + 1] = '0';
if Self::scan(i, j + 1, board, &word[1..]) {
return true;
}
board[i][j + 1] = x;
}
}
false
}
}