|
1 |
| -package com.thealgorithms.puzzlesandgames; |
| 1 | +package sudoku; |
| 2 | + |
| 3 | +import java.util.Iterator; |
| 4 | +import java.util.NoSuchElementException; |
2 | 5 |
|
3 | 6 | /**
|
4 |
| - * A class that provides methods to solve Sudoku puzzles of any n x n size |
5 |
| - * using a backtracking approach, where n must be a perfect square. |
6 |
| - * The algorithm checks for safe number placements in rows, columns, |
7 |
| - * and subgrids (which are sqrt(n) x sqrt(n) in size) and recursively solves the puzzle. |
8 |
| - * Though commonly used for 9x9 grids, it is adaptable to other valid Sudoku dimensions. |
| 7 | + * Represents a Sudoku board with support for iteration using the Iterator pattern. |
9 | 8 | */
|
10 |
| -final class Sudoku { |
| 9 | +public class SudokuBoard implements Iterable<SudokuBoard.Cell> { |
| 10 | + |
| 11 | + private final int[][] board; |
| 12 | + private final int size; |
11 | 13 |
|
12 |
| - private Sudoku() { |
| 14 | + public SudokuBoard(int size) { |
| 15 | + this.size = size; |
| 16 | + this.board = new int[size][size]; |
13 | 17 | }
|
14 | 18 |
|
15 |
| - /** |
16 |
| - * Checks if placing a number in a specific position on the Sudoku board is safe. |
17 |
| - * The number is considered safe if it does not violate any of the Sudoku rules: |
18 |
| - * - It should not be present in the same row. |
19 |
| - * - It should not be present in the same column. |
20 |
| - * - It should not be present in the corresponding 3x3 subgrid. |
21 |
| - * - It should not be present in the corresponding subgrid, which is sqrt(n) x sqrt(n) in size (e.g., for a 9x9 grid, the subgrid will be 3x3). |
22 |
| - * |
23 |
| - * @param board The current state of the Sudoku board. |
24 |
| - * @param row The row index where the number is to be placed. |
25 |
| - * @param col The column index where the number is to be placed. |
26 |
| - * @param num The number to be placed on the board. |
27 |
| - * @return True if the placement is safe, otherwise false. |
28 |
| - */ |
29 |
| - public static boolean isSafe(int[][] board, int row, int col, int num) { |
30 |
| - // Check the row for duplicates |
31 |
| - for (int d = 0; d < board.length; d++) { |
32 |
| - if (board[row][d] == num) { |
33 |
| - return false; |
34 |
| - } |
35 |
| - } |
| 19 | + public int getSize() { |
| 20 | + return size; |
| 21 | + } |
36 | 22 |
|
37 |
| - // Check the column for duplicates |
38 |
| - for (int r = 0; r < board.length; r++) { |
39 |
| - if (board[r][col] == num) { |
40 |
| - return false; |
41 |
| - } |
42 |
| - } |
| 23 | + public int getValue(int row, int col) { |
| 24 | + return board[row][col]; |
| 25 | + } |
43 | 26 |
|
44 |
| - // Check the corresponding 3x3 subgrid for duplicates |
45 |
| - int sqrt = (int) Math.sqrt(board.length); |
46 |
| - int boxRowStart = row - row % sqrt; |
47 |
| - int boxColStart = col - col % sqrt; |
| 27 | + public void setValue(int row, int col, int value) { |
| 28 | + board[row][col] = value; |
| 29 | + } |
48 | 30 |
|
49 |
| - for (int r = boxRowStart; r < boxRowStart + sqrt; r++) { |
50 |
| - for (int d = boxColStart; d < boxColStart + sqrt; d++) { |
51 |
| - if (board[r][d] == num) { |
52 |
| - return false; |
53 |
| - } |
54 |
| - } |
55 |
| - } |
| 31 | + /** Represents a single cell in the Sudoku board */ |
| 32 | + public static class Cell { |
| 33 | + private final int row; |
| 34 | + private final int col; |
| 35 | + private final int value; |
56 | 36 |
|
57 |
| - return true; |
58 |
| - } |
| 37 | + public Cell(int row, int col, int value) { |
| 38 | + this.row = row; |
| 39 | + this.col = col; |
| 40 | + this.value = value; |
| 41 | + } |
59 | 42 |
|
60 |
| - /** |
61 |
| - * Solves the Sudoku puzzle using backtracking. |
62 |
| - * The algorithm finds an empty cell and tries placing numbers |
63 |
| - * from 1 to n, where n is the size of the board |
64 |
| - * (for example, from 1 to 9 in a standard 9x9 Sudoku). |
65 |
| - * The algorithm finds an empty cell and tries placing numbers from 1 to 9. |
66 |
| - * The standard version of Sudoku uses numbers from 1 to 9, so the algorithm can be |
67 |
| - * easily modified for other variations of the game. |
68 |
| - * If a number placement is valid (checked via `isSafe`), the number is |
69 |
| - * placed and the function recursively attempts to solve the rest of the puzzle. |
70 |
| - * If no solution is possible, the number is removed (backtracked), |
71 |
| - * and the process is repeated. |
72 |
| - * |
73 |
| - * @param board The current state of the Sudoku board. |
74 |
| - * @param n The size of the Sudoku board (typically 9 for a standard puzzle). |
75 |
| - * @return True if the Sudoku puzzle is solvable, false otherwise. |
76 |
| - */ |
77 |
| - public static boolean solveSudoku(int[][] board, int n) { |
78 |
| - int row = -1; |
79 |
| - int col = -1; |
80 |
| - boolean isEmpty = true; |
81 |
| - |
82 |
| - // Find the next empty cell |
83 |
| - for (int i = 0; i < n; i++) { |
84 |
| - for (int j = 0; j < n; j++) { |
85 |
| - if (board[i][j] == 0) { |
86 |
| - row = i; |
87 |
| - col = j; |
88 |
| - isEmpty = false; |
89 |
| - break; |
90 |
| - } |
91 |
| - } |
92 |
| - if (!isEmpty) { |
93 |
| - break; |
94 |
| - } |
| 43 | + public int getRow() { |
| 44 | + return row; |
95 | 45 | }
|
96 | 46 |
|
97 |
| - // No empty space left |
98 |
| - if (isEmpty) { |
99 |
| - return true; |
| 47 | + public int getCol() { |
| 48 | + return col; |
100 | 49 | }
|
101 | 50 |
|
102 |
| - // Try placing numbers 1 to n in the empty cell (n should be a perfect square) |
103 |
| - // Eg: n=9 for a standard 9x9 Sudoku puzzle, n=16 for a 16x16 puzzle, etc. |
104 |
| - for (int num = 1; num <= n; num++) { |
105 |
| - if (isSafe(board, row, col, num)) { |
106 |
| - board[row][col] = num; |
107 |
| - if (solveSudoku(board, n)) { |
108 |
| - return true; |
109 |
| - } else { |
110 |
| - // replace it |
111 |
| - board[row][col] = 0; |
112 |
| - } |
113 |
| - } |
| 51 | + public int getValue() { |
| 52 | + return value; |
114 | 53 | }
|
115 |
| - return false; |
116 | 54 | }
|
117 | 55 |
|
118 |
| - /** |
119 |
| - * Prints the current state of the Sudoku board in a readable format. |
120 |
| - * Each row is printed on a new line, with numbers separated by spaces. |
121 |
| - * |
122 |
| - * @param board The current state of the Sudoku board. |
123 |
| - * @param n The size of the Sudoku board (typically 9 for a standard puzzle). |
124 |
| - */ |
125 |
| - public static void print(int[][] board, int n) { |
126 |
| - // Print the board in a nxn grid format |
127 |
| - // if n=9, print the board in a 9x9 grid format |
128 |
| - // if n=16, print the board in a 16x16 grid format |
129 |
| - for (int r = 0; r < n; r++) { |
130 |
| - for (int d = 0; d < n; d++) { |
131 |
| - System.out.print(board[r][d]); |
132 |
| - System.out.print(" "); |
133 |
| - } |
134 |
| - System.out.print("\n"); |
| 56 | + /** Iterator implementation for Sudoku board cells */ |
| 57 | + private class CellIterator implements Iterator<Cell> { |
| 58 | + private int row = 0; |
| 59 | + private int col = 0; |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean hasNext() { |
| 63 | + return row < size && col < size; |
| 64 | + } |
135 | 65 |
|
136 |
| - if ((r + 1) % (int) Math.sqrt(n) == 0) { |
137 |
| - System.out.print(""); |
| 66 | + @Override |
| 67 | + public Cell next() { |
| 68 | + if (!hasNext()) { |
| 69 | + throw new NoSuchElementException(); |
138 | 70 | }
|
| 71 | + Cell cell = new Cell(row, col, board[row][col]); |
| 72 | + col++; |
| 73 | + if (col == size) { |
| 74 | + col = 0; |
| 75 | + row++; |
| 76 | + } |
| 77 | + return cell; |
139 | 78 | }
|
140 | 79 | }
|
141 | 80 |
|
142 |
| - /** |
143 |
| - * The driver method to demonstrate solving a Sudoku puzzle. |
144 |
| - * A sample 9x9 Sudoku puzzle is provided, and the program attempts to solve it |
145 |
| - * using the `solveSudoku` method. If a solution is found, it is printed to the console. |
146 |
| - * |
147 |
| - * @param args Command-line arguments (not used in this program). |
148 |
| - */ |
149 |
| - public static void main(String[] args) { |
150 |
| - int[][] board = new int[][] { |
151 |
| - {3, 0, 6, 5, 0, 8, 4, 0, 0}, |
152 |
| - {5, 2, 0, 0, 0, 0, 0, 0, 0}, |
153 |
| - {0, 8, 7, 0, 0, 0, 0, 3, 1}, |
154 |
| - {0, 0, 3, 0, 1, 0, 0, 8, 0}, |
155 |
| - {9, 0, 0, 8, 6, 3, 0, 0, 5}, |
156 |
| - {0, 5, 0, 0, 9, 0, 6, 0, 0}, |
157 |
| - {1, 3, 0, 0, 0, 0, 2, 5, 0}, |
158 |
| - {0, 0, 0, 0, 0, 0, 0, 7, 4}, |
159 |
| - {0, 0, 5, 2, 0, 6, 3, 0, 0}, |
160 |
| - }; |
161 |
| - int n = board.length; |
162 |
| - |
163 |
| - if (solveSudoku(board, n)) { |
164 |
| - print(board, n); |
165 |
| - } else { |
166 |
| - System.out.println("No solution"); |
167 |
| - } |
| 81 | + @Override |
| 82 | + public Iterator<Cell> iterator() { |
| 83 | + return new CellIterator(); |
168 | 84 | }
|
169 | 85 | }
|
0 commit comments