forked from simranlotey/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuSolver.ts
86 lines (73 loc) · 1.9 KB
/
SudokuSolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function solveSudoku(board: number[][]): boolean {
const emptyCell = findEmptyCell(board);
if (!emptyCell) {
// No empty cells left, the Sudoku puzzle is solved
return true;
}
const [row, col] = emptyCell;
for (let num = 1; num <= 9; num++) {
if (isValidMove(board, row, col, num)) {
board[row][col] = num;
if (solveSudoku(board)) {
return true; // Recursion succeeded
}
board[row][col] = 0; // Backtrack
}
}
return false; // No valid solution found
}
function findEmptyCell(board: number[][]): [number, number] | null {
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
if (board[row][col] === 0) {
return [row, col];
}
}
}
return null; // No empty cell found
}
function isValidMove(board: number[][], row: number, col: number, num: number): boolean {
// Check row
for (let i = 0; i < 9; i++) {
if (board[row][i] === num) {
return false;
}
}
// Check column
for (let i = 0; i < 9; i++) {
if (board[i][col] === num) {
return false;
}
}
// Check 3x3 grid
const gridStartRow = Math.floor(row / 3) * 3;
const gridStartCol = Math.floor(col / 3) * 3;
for (let i = gridStartRow; i < gridStartRow + 3; i++) {
for (let j = gridStartCol; j < gridStartCol + 3; j++) {
if (board[i][j] === num) {
return false;
}
}
}
return true;
}
/* Testcase
const sudokuBoard: number[][] = [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 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 (solveSudoku(sudokuBoard)) {
console.log("Solved Sudoku:");
for (let row of sudokuBoard) {
console.log(row.join(" "));
}
} else {
console.log("No solution exists.");
}*/