|
| 1 | +package com.thealgorithms.backtracking; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * A class to solve a crossword puzzle using backtracking. |
| 8 | + * Example: |
| 9 | + * Input: |
| 10 | + * puzzle = { |
| 11 | + * {' ', ' ', ' '}, |
| 12 | + * {' ', ' ', ' '}, |
| 13 | + * {' ', ' ', ' '} |
| 14 | + * } |
| 15 | + * words = List.of("cat", "dog") |
| 16 | + * |
| 17 | + * Output: |
| 18 | + * { |
| 19 | + * {'c', 'a', 't'}, |
| 20 | + * {' ', ' ', ' '}, |
| 21 | + * {'d', 'o', 'g'} |
| 22 | + * } |
| 23 | + */ |
| 24 | +public final class CrosswordSolver { |
| 25 | + private CrosswordSolver() { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Checks if a word can be placed at the specified position in the crossword. |
| 30 | + * |
| 31 | + * @param puzzle The crossword puzzle represented as a 2D char array. |
| 32 | + * @param word The word to be placed. |
| 33 | + * @param row The row index where the word might be placed. |
| 34 | + * @param col The column index where the word might be placed. |
| 35 | + * @param vertical If true, the word is placed vertically; otherwise, horizontally. |
| 36 | + * @return true if the word can be placed, false otherwise. |
| 37 | + */ |
| 38 | + public static boolean isValid(char[][] puzzle, String word, int row, int col, boolean vertical) { |
| 39 | + for (int i = 0; i < word.length(); i++) { |
| 40 | + if (vertical) { |
| 41 | + if (row + i >= puzzle.length || puzzle[row + i][col] != ' ') { |
| 42 | + return false; |
| 43 | + } |
| 44 | + } else { |
| 45 | + if (col + i >= puzzle[0].length || puzzle[row][col + i] != ' ') { |
| 46 | + return false; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Places a word at the specified position in the crossword. |
| 55 | + * |
| 56 | + * @param puzzle The crossword puzzle represented as a 2D char array. |
| 57 | + * @param word The word to be placed. |
| 58 | + * @param row The row index where the word will be placed. |
| 59 | + * @param col The column index where the word will be placed. |
| 60 | + * @param vertical If true, the word is placed vertically; otherwise, horizontally. |
| 61 | + */ |
| 62 | + public static void placeWord(char[][] puzzle, String word, int row, int col, boolean vertical) { |
| 63 | + for (int i = 0; i < word.length(); i++) { |
| 64 | + if (vertical) { |
| 65 | + puzzle[row + i][col] = word.charAt(i); |
| 66 | + } else { |
| 67 | + puzzle[row][col + i] = word.charAt(i); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Removes a word from the specified position in the crossword. |
| 74 | + * |
| 75 | + * @param puzzle The crossword puzzle represented as a 2D char array. |
| 76 | + * @param word The word to be removed. |
| 77 | + * @param row The row index where the word is placed. |
| 78 | + * @param col The column index where the word is placed. |
| 79 | + * @param vertical If true, the word was placed vertically; otherwise, horizontally. |
| 80 | + */ |
| 81 | + public static void removeWord(char[][] puzzle, String word, int row, int col, boolean vertical) { |
| 82 | + for (int i = 0; i < word.length(); i++) { |
| 83 | + if (vertical) { |
| 84 | + puzzle[row + i][col] = ' '; |
| 85 | + } else { |
| 86 | + puzzle[row][col + i] = ' '; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Solves the crossword puzzle using backtracking. |
| 93 | + * |
| 94 | + * @param puzzle The crossword puzzle represented as a 2D char array. |
| 95 | + * @param words The list of words to be placed. |
| 96 | + * @return true if the crossword is solved, false otherwise. |
| 97 | + */ |
| 98 | + public static boolean solveCrossword(char[][] puzzle, List<String> words) { |
| 99 | + // Create a mutable copy of the words list |
| 100 | + List<String> remainingWords = new ArrayList<>(words); |
| 101 | + |
| 102 | + for (int row = 0; row < puzzle.length; row++) { |
| 103 | + for (int col = 0; col < puzzle[0].length; col++) { |
| 104 | + if (puzzle[row][col] == ' ') { |
| 105 | + for (String word : new ArrayList<>(remainingWords)) { |
| 106 | + for (boolean vertical : new boolean[] {true, false}) { |
| 107 | + if (isValid(puzzle, word, row, col, vertical)) { |
| 108 | + placeWord(puzzle, word, row, col, vertical); |
| 109 | + remainingWords.remove(word); |
| 110 | + if (solveCrossword(puzzle, remainingWords)) { |
| 111 | + return true; |
| 112 | + } |
| 113 | + remainingWords.add(word); |
| 114 | + removeWord(puzzle, word, row, col, vertical); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return false; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + return true; |
| 123 | + } |
| 124 | +} |
0 commit comments