-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.php
148 lines (125 loc) · 3.04 KB
/
solver.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* 9X9 SUDOKU SOLVER
* Created by JetBrains PhpStorm.
* User: pashupatipradhan
* Date: 9/25/13
* Time: 8:49 PM
* To change this template use File | Settings | File Templates.
*/
/*
* this is an array of unsloved sudoku problem
*/
$problem = array(
array(0, 8, 0, 4, 7, 1, 6, 3, 0),
array(0, 5, 0, 0, 9, 0, 0, 0, 8),
array(0, 7, 3, 0, 6, 0, 2, 9, 0),
array(0, 0, 0, 0, 0, 7, 0, 0, 0),
array(0, 2, 0, 3, 5, 8, 0, 4, 0),
array(0, 0, 0, 9, 0, 0, 0, 0, 0),
array(0, 6, 2, 0, 8, 0, 3, 1, 0),
array(8, 0, 0, 0, 1, 0, 0, 5, 0),
array(0, 9, 1, 5, 3, 4, 0, 6, 0)
);
function solve()
{
// start solving from the initial position
sudokuSolver(0, 0);
}
/*
* Function that recurse and backtracks
* to solve the give problem
*/
function sudokuSolver($row, $col)
{
global $problem;
if ($col >= 9) {
$col = 0;
if (++$row >= 9)
return true;
}
/*
* checks the particular position is filled or not
* if already filled ignore the current position
* and move to next position
*/
if ($problem[$row][$col] != 0) {
return sudokuSolver($row, $col + 1);
}
/*
* insert the numbers and validates
* whether the give value is valid for the
* current position or not
*/
for ($value = 1; $value <= 9; $value++) {
if (isValidValue($row, $col, $value)) {
$problem[$row][$col] = $value;
if (sudokuSolver($row, $col + 1)) {
return true;
}
}
}
// Reset the value
$problem[$row][$col] = 0; // reset on backtrack
return false;
}
/*
* validates whether the give no is valid for the given position
*/
function isValidValue($row, $col, $value)
{
global $problem;
// Check validity in row
for ($i = 0; $i < 9; $i++) {
if ($value == $problem[$i][col]) {
return false;
}
}
// Check validity in column
for ($j = 0; $j < 9; $j++) {
if ($value == $problem[$row][$j]) {
return false;
}
}
//Check validity in 3X3 small box
$rowSmall = ($row / 3) * 3;
$colSmall = ($col / 3) * 3;
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
if ($value == $problem[$rowSmall + $i][$colSmall + $j]) {
return false;
}
}
}
// if everything is valid return
return true;
}
function sudoku()
{
echo("Given SUDOKU PROBLEM <br />");
display();
solve();
echo(" <br /> <br /> SOLUTION <br />");
display();
}
function display()
{
global $problem;
for ($i = 0; $i < 9; $i++) {
if ($i % 3 == 0)
echo("+-------+-------+-------+ <br />");
for ($j = 0; $j < 9; $j++) {
if ($j % 3 == 0)
echo("| ");
if ($problem[$i][$j] == 0)
echo("0");
else
echo($problem[$i][$j]);
echo(" ");
}
echo("| <br />");
}
echo("+-------+-------+-------+ <br />");
}
print_r(sudoku());
?>