-
Notifications
You must be signed in to change notification settings - Fork 34
/
sudoku_solver_backtracking.pl
124 lines (102 loc) · 3.01 KB
/
sudoku_solver_backtracking.pl
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/perl
# Solve Sudoku puzzle (recursive solution).
use 5.036;
sub is_valid ($board, $row, $col, $num) {
# Check if the number is not present in the current row and column
foreach my $i (0 .. 8) {
if (($board->[$row][$i] == $num) || ($board->[$i][$col] == $num)) {
return 0;
}
}
# Check if the number is not present in the current 3x3 subgrid
my ($start_row, $start_col) = (3 * int($row / 3), 3 * int($col / 3));
foreach my $i (0 .. 2) {
foreach my $j (0 .. 2) {
if ($board->[$start_row + $i][$start_col + $j] == $num) {
return 0;
}
}
}
return 1;
}
sub find_empty_location ($board) {
# Find an empty position (cell with 0)
foreach my $i (0 .. 8) {
foreach my $j (0 .. 8) {
if ($board->[$i][$j] == 0) {
return ($i, $j);
}
}
}
return (undef, undef); # If the board is filled
}
sub solve_sudoku ($board) {
my ($row, $col) = find_empty_location($board);
if (!defined($row) && !defined($col)) {
return 1; # Puzzle is solved
}
foreach my $num (1 .. 9) {
if (is_valid($board, $row, $col, $num)) {
# Try placing the number
$board->[$row][$col] = $num;
# Recursively try to solve the rest of the puzzle
if (__SUB__->($board)) {
return 1;
}
# If placing the current number doesn't lead to a solution, backtrack
$board->[$row][$col] = 0;
}
}
return 0; # No solution found
}
#<<<
# Example usage:
# Define the Sudoku puzzle as a 9x9 list with 0 representing empty cells
my $sudoku_board = [
[2, 0, 0, 0, 7, 0, 0, 0, 3],
[1, 0, 0, 0, 0, 0, 0, 8, 0],
[0, 0, 4, 2, 0, 9, 0, 0, 5],
[9, 4, 0, 0, 0, 0, 6, 0, 8],
[0, 0, 0, 8, 0, 0, 0, 9, 0],
[0, 0, 0, 0, 0, 0, 0, 7, 0],
[7, 2, 1, 9, 0, 8, 0, 6, 0],
[0, 3, 0, 0, 2, 7, 1, 0, 0],
[4, 0, 0, 0, 0, 3, 0, 0, 0]
];
$sudoku_board = [
[0, 0, 0, 8, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 4, 3],
[5, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 7, 0, 8, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 2, 0, 0, 3, 0, 0, 0, 0],
[6, 0, 0, 0, 0, 0, 0, 7, 5],
[0, 0, 3, 4, 0, 0, 0, 0, 0],
[0, 0, 0, 2, 0, 0, 6, 0, 0]
] if 0;
$sudoku_board = [
[8, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 3, 6, 0, 0, 0, 0, 0],
[0, 7, 0, 0, 9, 0, 2, 0, 0],
[0, 5, 0, 0, 0, 7, 0, 0, 0],
[0, 0, 0, 0, 4, 5, 7, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 3, 0],
[0, 0, 1, 0, 0, 0, 0, 6, 8],
[0, 0, 8, 5, 0, 0, 0, 1, 0],
[0, 9, 0, 0, 0, 0, 4, 0, 0]
] if 0;
#>>>
sub display_grid ($grid) {
foreach my $i (0 .. $#$grid) {
print "$grid->[$i] ";
print " " if ($i + 1) % 3 == 0;
print "\n" if ($i + 1) % 9 == 0;
print "\n" if ($i + 1) % 27 == 0;
}
}
if (solve_sudoku($sudoku_board)) {
display_grid([map { @$_ } @$sudoku_board]);
}
else {
say "No solution exists.";
}