-
Notifications
You must be signed in to change notification settings - Fork 0
/
solvdoku-core.php
135 lines (118 loc) · 3.74 KB
/
solvdoku-core.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
<?php declare(strict_types = 1);
define("SIZE", 9);
// $grid = [ [3, 0, 6, 5, 0, 8, 4, 0, 0],
// [5, 2, 0, 0, 0, 0, 0, 0, 0],
// [0, 8, 7, 0, 0, 0, 0, 3, 1],
// [0, 0, 3, 0, 1, 0, 0, 8, 0],
// [9, 0, 0, 8, 6, 3, 0, 0, 5],
// [0, 5, 0, 0, 9, 0, 6, 0, 0],
// [1, 3, 0, 0, 0, 0, 2, 5, 0],
// [0, 0, 0, 0, 0, 0, 0, 7, 4],
// [0, 0, 5, 2, 0, 6, 3, 0, 0] ];
if (isset($grid)) {
$mygrid = &$grid;
checkGrid($grid);
if (isset($GLOBALS["faultyCells"])) {
$result = "🧐 Hmmm... Bad input. Make sure you didn't insert same number twice in a row/column.";
} else {
if (solveSudoku()) {
$result = "We Found multiple solutions. This is one of them 😉 👉 .";
} else if (!isset($grid2)) {
$result = "No solution exists.";
}
}
}
function checkGrid(array &$grid) : void {
for ($y = 0; $y < SIZE; $y++) {
for ($x = 0; $x < SIZE; $x++) {
if ($grid[$y][$x] === 0) {
continue;
}
$num = $grid[$y][$x];
$grid[$y][$x] = 0;
$isSafe = isSafe($y, $x, $num);
if (!$isSafe) {
findFaulty($y, $x, $num);
$GLOBALS["faultyCells"][] = ["x" => $x, "y" => $y];
}
$grid[$y][$x] = $num;
if (!$isSafe) {
return;
}
}
}
}
// marks all faulty cells with the current one in the first rule found
function findFaulty(int $row, int $col, int $num) : void {
for ($i = 0; $i < SIZE; $i++) {
if ($GLOBALS["mygrid"][$row][$i] === $num) {
$GLOBALS["faultyCells"][] = ["x" => $i, "y" => $row];
}
}
if (isset($GLOBALS["faultyCells"])) { return; }
for ($i = 0; $i < SIZE; $i++) {
if ($GLOBALS["mygrid"][$i][$col] === $num) {
$GLOBALS["faultyCells"][] = ["x" => $col, "y" => $i];
}
}
if (isset($GLOBALS["faultyCells"])) { return; }
$startRow = $row - $row % 3;
$startCol = $col - $col % 3;
for ($y = 0; $y < 3; $y++) {
for ($x = 0; $x < 3; $x++) {
if ($GLOBALS["mygrid"][$startRow + $y][$startCol + $x] === $num) {
$GLOBALS["faultyCells"][] = ["x" => $startCol + $x, "y" => $startRow + $y];
}
}
}
}
function isSafe(int $row, int $col, int $num) : bool {
for ($i = 0; $i < SIZE; $i++) {
if ($GLOBALS["mygrid"][$row][$i] === $num) {
return false;
}
}
for ($i = 0; $i < SIZE; $i++) {
if ($GLOBALS["mygrid"][$i][$col] === $num) {
return false;
}
}
$startRow = $row - $row % 3;
$startCol = $col - $col % 3;
for ($y = 0; $y < 3; $y++) {
for ($x = 0; $x < 3; $x++) {
if ($GLOBALS["mygrid"][$startRow + $y][$startCol + $x] === $num) {
return false;
}
}
}
return true;
}
// return value specifies whether to halt execution
function solveSudoku(int $row = 0, int $col = 0) : bool {
if ($col === SIZE) {
$row++;
$col = 0;
}
if ($row === SIZE) {
if (isset($GLOBALS["grid2"])) {
return true;
}
$GLOBALS["grid2"] = $GLOBALS["grid"];
$GLOBALS["mygrid"] = &$GLOBALS["grid2"];
return false;
}
if ($GLOBALS["mygrid"][$row][$col] > 0) {
return (solveSudoku($row, $col + 1));
}
for ($num = 1; $num <= SIZE; $num++) {
if (isSafe($row, $col, $num)) {
$GLOBALS["mygrid"][$row][$col] = $num;
if (solveSudoku($row, $col + 1)) {
return true;
}
}
$GLOBALS["mygrid"][$row][$col] = 0;
}
return false;
}