/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/backtracking/CrosswordSolver.java
125 строк
4 KB
Piotr Idzik
style: include `OCP_OVERLY_CONCRETE_PARAMETER` (#5833)
14 окт 2024, 23:46
Не верифицирован
14 окт 2024, 23:46
8886e09
Код
Авторство
О чём код?
package com.thealgorithms.backtracking; import java.util.ArrayList; import java.util.Collection; import java.util.List; /** * A class to solve a crossword puzzle using backtracking. * Example: * Input: * puzzle = { * {' ', ' ', ' '}, * {' ', ' ', ' '}, * {' ', ' ', ' '} * } * words = List.of("cat", "dog") * * Output: * { * {'c', 'a', 't'}, * {' ', ' ', ' '}, * {'d', 'o', 'g'} * } */ public final class CrosswordSolver { private CrosswordSolver() { } /** * Checks if a word can be placed at the specified position in the crossword. * * @param puzzle The crossword puzzle represented as a 2D char array. * @param word The word to be placed. * @param row The row index where the word might be placed. * @param col The column index where the word might be placed. * @param vertical If true, the word is placed vertically; otherwise, horizontally. * @return true if the word can be placed, false otherwise. */ public static boolean isValid(char[][] puzzle, String word, int row, int col, boolean vertical) { for (int i = 0; i < word.length(); i++) { if (vertical) { if (row + i >= puzzle.length || puzzle[row + i][col] != ' ') { return false; } } else { if (col + i >= puzzle[0].length || puzzle[row][col + i] != ' ') { return false; } } } return true; } /** * Places a word at the specified position in the crossword. * * @param puzzle The crossword puzzle represented as a 2D char array. * @param word The word to be placed. * @param row The row index where the word will be placed. * @param col The column index where the word will be placed. * @param vertical If true, the word is placed vertically; otherwise, horizontally. */ public static void placeWord(char[][] puzzle, String word, int row, int col, boolean vertical) { for (int i = 0; i < word.length(); i++) { if (vertical) { puzzle[row + i][col] = word.charAt(i); } else { puzzle[row][col + i] = word.charAt(i); } } } /** * Removes a word from the specified position in the crossword. * * @param puzzle The crossword puzzle represented as a 2D char array. * @param word The word to be removed. * @param row The row index where the word is placed. * @param col The column index where the word is placed. * @param vertical If true, the word was placed vertically; otherwise, horizontally. */ public static void removeWord(char[][] puzzle, String word, int row, int col, boolean vertical) { for (int i = 0; i < word.length(); i++) { if (vertical) { puzzle[row + i][col] = ' '; } else { puzzle[row][col + i] = ' '; } } } /** * Solves the crossword puzzle using backtracking. * * @param puzzle The crossword puzzle represented as a 2D char array. * @param words The list of words to be placed. * @return true if the crossword is solved, false otherwise. */ public static boolean solveCrossword(char[][] puzzle, Collection<String> words) { // Create a mutable copy of the words list List<String> remainingWords = new ArrayList<>(words); for (int row = 0; row < puzzle.length; row++) { for (int col = 0; col < puzzle[0].length; col++) { if (puzzle[row][col] == ' ') { for (String word : new ArrayList<>(remainingWords)) { for (boolean vertical : new boolean[] {true, false}) { if (isValid(puzzle, word, row, col, vertical)) { placeWord(puzzle, word, row, col, vertical); remainingWords.remove(word); if (solveCrossword(puzzle, remainingWords)) { return true; } remainingWords.add(word); removeWord(puzzle, word, row, col, vertical); } } } return false; } } } return true; } }