Skip to content

Commit f810d53

Browse files
authored
Update README.md
1 parent b7d31ad commit f810d53

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,58 @@
1010

1111
## How it works
1212

13+
### Sudoku board detection
1314

15+
To identify the sudoku board the input image is processed as follows with [OpenCV](https://github.com/opencv/opencv):
16+
- Using the Canny algorithm to detect all contours in the image.
17+
- The biggest contour corresponds to the whole sudoku puzzle.
18+
- Use the found contour to extract and warp the sudoku grid .
1419

15-
<img src="demo/demo1.gif" width=100%></img>
20+
The numbers are detected using [MLKit Text Recognition](https://developers.google.com/ml-kit/vision/text-recognition/v2).
21+
22+
<br>
23+
24+
<p align="center">
25+
<img src="demo/demo1.gif"></img>
26+
</p>
27+
28+
<br>
29+
30+
### Solving
31+
For solving the puzzle I'm using a fairly simple, brute-force algorithm that relies on backtracking to generate the valid solution.
32+
<br>
33+
It goes through the whole 2D array and for each number that needs to be found it tries all possibilities and continues with the following numbers.
34+
<br>
35+
<br>
36+
For each cell there are 9 possible numbers which means the time complexity of this algorithm is O(9<sup>N</sup>).
37+
38+
```
39+
private fun solve(index: Int = 0): Boolean {
40+
for (i in index until 81) {
41+
val r = i / 9
42+
val c = i % 9
43+
if (sudokuBoard[r][c].number == 0) {
44+
for (n in 1..9) {
45+
if (isValidSolution(r, c, n)) {
46+
sudokuBoard[r][c].number = n
47+
if (solve(i + 1)) {
48+
return true
49+
}
50+
sudokuBoard[r][c].number = 0
51+
}
52+
}
53+
return false
54+
}
55+
}
56+
return true
57+
}
58+
```
59+
<br>
60+
<p align="center">
61+
<img src="demo/demo2.gif"></img>
62+
</p>
63+
<p align="center">
64+
<span>Solve algorithm demo</span>
65+
</p>
1666

1767
## Getting Started

0 commit comments

Comments
 (0)