Coding a Sudoku solver in Python using recursion/backtracking

Publicado em: 26 Outubro 2020
no canal de: Kylie Ying
80,996
1.9k

Learn how to code a Sudoku puzzle solver in Python! In this tutorial, I explain how recursion/backtracking work in order to solve a Sudoku puzzle input.

Code: https://github.com/kying18/sudoku
- sudoku_empty.py: empty template with optional helper functions and comments in the code for the steps
- sudoku.py: my solution from the video!

In this video, I go over the steps necessary to use recursion to solve the input. Basically, the steps we need to follow are: determine where the next guess should go, place a guess between 1-9, then attempt to solve by recursively calling the mutating function. If the function solves the puzzle, then we are done. Otherwise, we reset our guess and guess another number.

At the end, we've literally tried every single combination of numbers on the board, through the backtracking process, so if we haven't reached a solution, then we know that our puzzle is unsolvable.

Of course, this is not a very "intelligent" solution in the sense that we have to try all the various combinations. There is probably a more optimized solution out there that can solve the problem in fewer steps. This is a good exercise to try! If you think your solution is good, you can make a pull request on github or just DM me your solution. I might include it in the github (with credits to you ofc).

Check out this wiki page for some more info about sudoku algorithms that are more intelligent than just backtracking: https://en.wikipedia.org/wiki/Sudoku_...

Feel free to leave any questions.

Please consider subscribing if you liked this video: https://www.youtube.com/c/ycubed?sub_...

Thanks for watching everyone!
~~~~~~~~~~~~~~~~~~~~~~~~
Follow me on Instagram:   / kylieyying  
Follow me on Twitter:   / kylieyying  
Check out my website: https://www.kylieying.com


Nesta página do site você pode assistir ao vídeo on-line Coding a Sudoku solver in Python using recursion/backtracking duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Kylie Ying 26 Outubro 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 80,996 vezes e gostou 1.9 mil espectadores. Boa visualização!