Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab Task:

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 define the rules that need to be satisfied in Sudoku.

 The constraints ensure that each row, each column, and each 3x3 sub grid
contains all digits from 1 to 9 without repetition.

 We can define the following constraints for Sudoku:

 Constraint 1: All values in a row must be distinct.

 Constraint 2: All values in a column must be distinct.

 Constraint 3: All values in a 3x3 sub grid must be distinct.

 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 {

private static final int SIZE = 9;

private int[][] grid;

public Sudoku(int[][] initialGrid) {

this.grid = initialGrid;

public boolean solve() {

int row, col;

Cell nextCell = findEmptyCell();

if (nextCell == null) {

return true;

row = nextCell.row;

col = nextCell.col;

for (int value = 1; value <= SIZE; value++) {

if (isValueValid(row, col, value)) {

grid[row][col] = value;

if (solve()) {

return true;

}
grid[row][col] = 0;

return false;

private Cell findEmptyCell() {

for (int row = 0; row < SIZE; row++) {

for (int col = 0; col < SIZE; col++) {

if (grid[row][col] == 0) {

return new Cell(row, col);

return null;

private boolean isValueValid(int row, int col, int value) {

for (int c = 0; c < SIZE; c++) {

if (grid[row][c] == value) {

return false;

for (int r = 0; r < SIZE; r++) {

if (grid[r][col] == value) {

return false;

}
}

int subgridStartRow = row - row % 3;

int subgridStartCol = col - col % 3;

for (int r = subgridStartRow; r < subgridStartRow + 3; r++) {

for (int c = subgridStartCol; c < subgridStartCol + 3; c++) {

if (grid[r][c] == value) {

return false;

return true;

public void printSolution() {

for (int row = 0; row < SIZE; row++) {

for (int col = 0; col < SIZE; col++) {

System.out.print(grid[row][col] + " ");

System.out.println();

private static class Cell {

private int row;


private int col;

public Cell(int row, int col) {

this.row = row;

this.col = col;

public static void main(String[] args) {

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}

};

Sudoku solver = new Sudoku(initialGrid);

if (solver.solve()) {

System.out.println("Solution:");

solver.printSolution();
} else {

System.out.println("No solution exists.");

You might also like