diff --git a/Backtracking Algorithms/Word Search in a 2D Grid/Program.c b/Backtracking Algorithms/Word Search in a 2D Grid/Program.c new file mode 100644 index 00000000..a664e7a9 --- /dev/null +++ b/Backtracking Algorithms/Word Search in a 2D Grid/Program.c @@ -0,0 +1,69 @@ +#include +#include +#include + +#define ROWS 3 +#define COLS 4 + +// Struct to store board and word details +typedef struct { + char board[ROWS][COLS]; + int rows; + int cols; +} Board; + +// Helper function for backtracking +bool backtrack(Board *b, const char *word, int index, int i, int j) { + if (index == strlen(word)) { + return true; // All characters in the word have been found + } + // Check if the current position is within the board boundaries and if the character matches + if (i < 0 || j < 0 || i >= b->rows || j >= b->cols || b->board[i][j] != word[index]) { + return false; // Out of bounds or mismatch + } + + char temp = b->board[i][j]; + b->board[i][j] = '*'; // Mark the current cell as visited + + // Explore neighboring cells + bool exist = backtrack(b, word, index + 1, i - 1, j) || + backtrack(b, word, index + 1, i, j - 1) || + backtrack(b, word, index + 1, i + 1, j) || + backtrack(b, word, index + 1, i, j + 1); + + b->board[i][j] = temp; // Restore the cell + + return exist; +} + +// Function to check if a word exists in the board +bool exist(Board *b, const char *word) { + for (int i = 0; i < b->rows; i++) { + for (int j = 0; j < b->cols; j++) { + if (backtrack(b, word, 0, i, j)) { + return true; + } + } + } + return false; +} + +int main() { + Board b = { + { + {'A', 'B', 'C', 'E'}, + {'S', 'F', 'C', 'S'}, + {'A', 'D', 'E', 'E'} + }, + ROWS, COLS + }; + const char *word1 = "ABCCED"; + const char *word2 = "SEE"; + const char *word3 = "ABCB"; + + printf("Word \"%s\" exists: %s\n", word1, exist(&b, word1) ? "true" : "false"); + printf("Word \"%s\" exists: %s\n", word2, exist(&b, word2) ? "true" : "false"); + printf("Word \"%s\" exists: %s\n", word3, exist(&b, word3) ? "true" : "false"); + + return 0; +} diff --git a/Backtracking Algorithms/Word Search in a 2D Grid/README.md b/Backtracking Algorithms/Word Search in a 2D Grid/README.md new file mode 100644 index 00000000..3c74edf1 --- /dev/null +++ b/Backtracking Algorithms/Word Search in a 2D Grid/README.md @@ -0,0 +1,46 @@ +### Word Search in 2D Grid + +### Overview + +This program determines if a given word can be found in a 2D grid of letters. The word can be constructed from letters in the grid by moving horizontally or vertically from one letter to another adjacent letter. The same cell cannot be used more than once in the word search. + +### Problem Definition + +Given a 2D board of characters and a word, the task is to determine if the word exists in the grid. The word can be constructed by sequentially adjacent cells (horizontally or vertically). Each cell in the grid can be used only once per word search. + +**Input**: A 2D board of characters and a string `word`. + +**Output**: A boolean value - `true` if the word exists in the grid, otherwise `false`. + +### Algorithm Overview + +1. **Iterate Through the Grid**: The program starts by iterating over each cell in the grid. For each cell, if the character matches the first letter of the word, it initiates a recursive search from that cell. + +2. **Recursive Backtracking (`backtrack`)**: + - The function `backtrack` is a recursive function that checks if the current cell matches the current character in the word. + - If the entire word is found (index reaches the length of the word), the function returns `true`. + - Otherwise, the function marks the cell as visited by temporarily changing its value. + - It then recursively checks the adjacent cells (up, down, left, and right) for the next character in the word. + - If a path is found, it returns `true`; otherwise, it restores the cell's original value (backtracking) and continues the search. + +3. **Boundary and Matching Checks**: During the search, boundary conditions are checked to ensure the search does not go out of bounds, and that each cell matches the corresponding character in the word. + +### Example + +Let the `board` be: +![alt text](image.png) + +Let `word = "ABCCED"`. + +**Steps**: +1. The search starts at cell `[0][0]` ('A') and proceeds with adjacent cells to form the word "ABCCED". +2. Through recursive calls, the word is found starting from cell `[0][0]`, moving right and down to cover the entire word. + +**Output**: `true` + +### Edge Cases + +1. **Empty Grid**: If the board or word is empty, the function returns `false`. +2. **Word Not in Grid**: If no path can form the word, the function returns `false`. +3. **Repeated Letters in Word**: The word can contain repeated letters, but each cell in the grid can be used only once per word search. + diff --git a/Backtracking Algorithms/Word Search in a 2D Grid/image.png b/Backtracking Algorithms/Word Search in a 2D Grid/image.png new file mode 100644 index 00000000..854d55e5 Binary files /dev/null and b/Backtracking Algorithms/Word Search in a 2D Grid/image.png differ