📖 Scenario: You are building a simple word search puzzle solver. The puzzle is a grid of letters, and you want to find if a given word exists in the grid by moving horizontally or vertically to adjacent letters.This is like searching for a hidden word in a crossword puzzle.
🎯 Goal: Build a program that checks if a given word can be found in the letter grid by moving up, down, left, or right without revisiting the same cell.
📋 What You'll Learn
Create a 2D vector of characters representing the letter grid.
Create a string variable for the word to search.
Implement a backtracking function to search the word in the grid.
Print true if the word is found, otherwise false.
💡 Why This Matters
🌍 Real World
Word search puzzles are common in newspapers and apps. This technique helps solve or generate such puzzles automatically.
💼 Career
Backtracking algorithms are used in many fields like game development, AI, and solving constraint problems. Understanding this helps in coding interviews and real-world problem solving.
Progress0 / 4 steps
1
Create the letter grid
Create a 2D vector of characters called board with these exact rows: ['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']
DSA Rust
Hint
Use vec![vec!['A', 'B', 'C', 'E'], ...] to create the 2D vector.
2
Create the word to search
Add a string variable called word and set it to "ABCCED"
DSA Rust
Hint
Use String::from("ABCCED") to create the word string.
3
Implement the backtracking search function
Add a function called exist that takes board: &Vec> and word: &str and returns bool. Implement backtracking inside exist to check if word exists in board. Use helper function dfs with parameters board, word, index, row, col, and a mutable 2D vector visited to track visited cells. The search moves up, down, left, and right. Return true if the word is found, otherwise false. Call exist(&board, &word) inside main and store the result in a variable found.
DSA Rust
Hint
Use a helper function dfs to explore neighbors recursively. Mark cells visited to avoid reuse.
4
Print the search result
Add a println! statement in main to print the value of found which shows if the word was found in the grid.
DSA Rust
Hint
Use println!("{}", found); to print the boolean result.
Practice
(1/5)
1. What are the four directions checked at each cell during word search backtracking?