This repository has been archived by the owner on Oct 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsudoku.php
172 lines (150 loc) · 4.62 KB
/
sudoku.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
class Sudoku {
private $comming_arr = array();
private $grids = array();
private $columns_begining = array();
private $time_tracking = array();
public function __construct() {
$this->time_tracking['start'] = microtime(true);
}
private function set_grids() { //MAKE GRIDS
$grids = array();
foreach ($this->comming_arr as $k => $row) {
if ($k <= 2) {
$row_num = 1;
}
if ($k > 2 && $k <= 5) {
$row_num = 2;
}
if ($k > 5 && $k <= 8) {
$row_num = 3;
}
foreach ($row as $kk => $r) {
if ($kk <= 2) {
$col_num = 1;
}
if ($kk > 2 && $kk <= 5) {
$col_num = 2;
}
if ($kk > 5 && $kk <= 8) {
$col_num = 3;
}
$grids[$row_num][$col_num][] = $r;
}
}
$this->grids = $grids;
}
private function set_columns() { //ORDER BY COLUMNS
$columns_begining = array();
$i = 1;
foreach ($this->comming_arr as $k => $row) {
$e = 1;
foreach ($row as $kk => $r) {
$columns_begining[$e][$i] = $r;
$e++;
}
$i++;
}
$this->columns_begining = $columns_begining;
}
private function get_possibilities($k, $kk) { //GET POSSIBILITIES FOR GIVEN CELL
$values = array();
if ($k <= 2) {
$row_num = 1;
}
if ($k > 2 && $k <= 5) {
$row_num = 2;
}
if ($k > 5 && $k <= 8) {
$row_num = 3;
}
if ($kk <= 2) {
$col_num = 1;
}
if ($kk > 2 && $kk <= 5) {
$col_num = 2;
}
if ($kk > 5 && $kk <= 8) {
$col_num = 3;
}
for ($n = 1; $n <= 9; $n++) {
if (!in_array($n, $this->comming_arr[$k]) && !in_array($n, $this->columns_begining[$kk + 1]) && !in_array($n, $this->grids[$row_num][$col_num])) {
$values[] = $n;
}
}
shuffle($values);
return $values;
}
public function solve_it($arr) {
while (true) {
$this->comming_arr = $arr;
$this->set_columns();
$this->set_grids();
$ops = array();
foreach ($arr as $k => $row) {
foreach ($row as $kk => $r) {
if ($r == 0) {
$pos_vals = $this->get_possibilities($k, $kk);
$ops[] = array(
'rowIndex' => $k,
'columnIndex' => $kk,
'permissible' => $pos_vals
);
}
}
}
if (empty($ops)) {
return $arr;
}
usort($ops, array($this, 'sortOps'));
if (count($ops[0]['permissible']) == 1) {
$arr[$ops[0]['rowIndex']][$ops[0]['columnIndex']] = current($ops[0]['permissible']);
continue;
}
foreach ($ops[0]['permissible'] as $value) {
$tmp = $arr;
$tmp[$ops[0]['rowIndex']][$ops[0]['columnIndex']] = $value;
if ($result = $this->solve_it($tmp)) {
return $this->solve_it($tmp);
}
}
return false;
}
}
private function sortOps($a, $b) {
$a = count($a['permissible']);
$b = count($b['permissible']);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
public function getResult() {
echo "\n";
foreach ($this->comming_arr as $k => $row) {
foreach ($row as $kk => $r) {
echo $r . ' ';
}
echo "\n";
}
}
public function __destruct() {
$this->time_tracking['end'] = microtime(true);
$time = $this->time_tracking['end'] - $this->time_tracking['start'];
echo "\nExecution time : " . number_format($time, 3) . " sec\n\n";
}
}
$arr = array(
array(0, 6, 0, 0, 0, 0, 0, 3, 0),
array(0, 0, 0, 5, 0, 0, 0, 0, 4),
array(4, 1, 0, 0, 0, 9, 0, 2, 0),
array(0, 0, 4, 0, 0, 0, 0, 0, 0),
array(0, 3, 6, 0, 0, 0, 4, 8, 9),
array(0, 0, 2, 4, 8, 6, 0, 0, 0),
array(0, 2, 7, 0, 0, 0, 0, 9, 0),
array(6, 0, 1, 9, 0, 0, 3, 0, 7),
array(9, 4, 0, 0, 0, 5, 0, 1, 0),
);
$game = new Sudoku();
$game->solve_it($arr);
$game->getResult();