Professional Documents
Culture Documents
Ai Lab 10
Ai Lab 10
Let’s define the variables (V), domains (D), and constraints (C) as follows:
1. Variables (V):
Each variable represents a cell in the Sudoku grid. In this case, we have 81
variables (9 rows x 9 columns = 81 cells).
Each variable is identified by its row and column indices, ranging from (0, 0) to
(8, 8).
So, the set of variables V = {(0, 0), (0, 1), ..., (8, 8)}.
2. Domains (D):
The domain of each variable represents the possible values that can be assigned
to that variable.
For the Sudoku game, the domain of each variable is {1, 2, 3, 4, 5, 6, 7, 8, 9}.
Initially, the domains of the variables are filled with all possible values and as the
game progresses, values are eliminated based on the constraints and the
numbers already assigned in the same row, column, and sub grid.
3. Constraints (C):
The constraints ensure that each row, each column, and each 3x3 sub grid
contains all digits from 1 to 9 without repetition.
These constraints are checked during the solving process to ensure that the
assigned values satisfy the rules of Sudoku.
By defining the variables, domains, and constraints as described above, we can formulate the
Sudoku game as a Constraint Satisfaction Problem and apply CSP algorithms or techniques to
solve it.
SOLUTION:
public class Sudoku {
this.grid = initialGrid;
if (nextCell == null) {
return true;
row = nextCell.row;
col = nextCell.col;
grid[row][col] = value;
if (solve()) {
return true;
}
grid[row][col] = 0;
return false;
if (grid[row][col] == 0) {
return null;
if (grid[row][c] == value) {
return false;
if (grid[r][col] == value) {
return false;
}
}
if (grid[r][c] == value) {
return false;
return true;
System.out.println();
this.row = row;
this.col = col;
int[][] initialGrid = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0,
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};
if (solver.solve()) {
System.out.println("Solution:");
solver.printSolution();
} else {