GithubHelp home page GithubHelp logo

kirilkirkov / sudoku-solver Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 7.0 476 KB

PHP Based Sudoku Puzzle Solving

PHP 100.00%
sudoku-solver sudoku sudoku-puzzle php-sudoku-solver backtracking-algorithm backtracking conflict

sudoku-solver's Introduction

PHP Sudoku Solver

Class for solving sudoku puzzle

Using:

  • Just put 2D Array into solve_it method
  • Call getResult method to see result
  • Example:
 
$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();

How it works ?

  • It works with backtracking algorithm. When you put array into solve_it method array go loop and check all cells. When find cell==0 program call other method that find all possible values that can be put in this cell than save row num and col num and values into other array and repeat this with all cells. If have conflict function return itself and try again. When all zero values are replaced and dont have conflicts method return ready solved array!

What is backtracking algorithm ?

  • Backtracking algorithms are adapted to solve the Sudoku that iterates all the possible solutions for the given sudoku. If the solutions assigned do not lead to the solution of Sudoku, the algorithm discards the solutions and rollbacks to the original solutions and retries again and hence the name backtracking.
  • Example of backtracking:
    alt tag

sudoku-solver's People

Contributors

kirilkirkov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.