20240708 Finished
This commit is contained in:
parent
2945497f04
commit
75e3159aac
|
@ -169,4 +169,5 @@ mod p17_letter_combinations_of_a_phone_number;
|
|||
mod p77_combinations;
|
||||
mod p46_permutations;
|
||||
mod p39_combination_sum;
|
||||
mod p22_generate_parentheses;
|
||||
mod p22_generate_parentheses;
|
||||
mod p79_word_search;
|
78
src/problem/p79_word_search.rs
Normal file
78
src/problem/p79_word_search.rs
Normal file
|
@ -0,0 +1,78 @@
|
|||
/**
|
||||
* [79] Word Search
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn exist(board: Vec<Vec<char>>, word: String) -> bool {
|
||||
let word: Vec<char> = word.chars().collect();
|
||||
|
||||
let (m, n) = (board.len(), board[0].len());
|
||||
|
||||
let mut visited = vec![vec![false;n];m];
|
||||
for i in 0..m {
|
||||
for j in 0..n {
|
||||
if board[i][j] == word[0] {
|
||||
if Self::search(&board, &word, &mut visited, i as i32, j as i32, 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn search(board: &Vec<Vec<char>>, word: &Vec<char>, visited: &mut Vec<Vec<bool>>, x: i32, y: i32, pos: usize) -> bool {
|
||||
if pos == word.len() {
|
||||
return true;
|
||||
}
|
||||
|
||||
let (m, n) = (board.len() as i32, board[0].len() as i32);
|
||||
|
||||
if x < 0 || x >= m || y < 0 || y >= n {
|
||||
return false;
|
||||
}
|
||||
|
||||
let (x_pos, y_pos) = (x as usize, y as usize);
|
||||
|
||||
if visited[x_pos][y_pos] {
|
||||
return false;
|
||||
}
|
||||
|
||||
visited[x_pos][y_pos] = true;
|
||||
if board[x_pos][y_pos] == word[pos] {
|
||||
if Self::search(board, word, visited, x + 1, y, pos + 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if Self::search(board, word, visited, x - 1, y, pos + 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if Self::search(board, word, visited, x, y + 1, pos + 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if Self::search(board, word, visited, x, y - 1, pos + 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
visited[x_pos][y_pos] = false;
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_79() {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user