PHP-based Sudoku solution example and php-based solution example
The example in this article describes how to solve the sudoku problem implemented by PHP. We will share this with you for your reference. The details are as follows:
1. Sudoku Problem description:
For the given two-dimensional array of numbers, the numbers in each column of each row must not be repeated.
II. Implementation Code:
<? Php/* Sudoku solver Program * Created on 2017-4-18 **/class Sudoku {var $ matrix; function _ construct ($ arr = null) {if ($ arr = null) {$ this-> clear ();} else {$ this-> matrix = $ arr;} function clear () {for ($ I = 0; $ I <9; $ I ++) {for ($ j = 0; $ j <9; $ j ++) {$ this-> matrix [$ I] [$ j] = array (); for ($ k = 1; $ k <= 9; $ k ++) {$ this-> matrix [$ I] [$ j] [$ k] = $ k ;}}} function setCell ($ row, $ col, $ value) {$ this-> matrix [$ ro W] [$ col] = array ($ value => $ value); // row for ($ I = 0; $ I <9; $ I ++) {if ($ I! = $ Col) {if (! $ This-> removeValue ($ row, $ I, $ value) {return false ;}}// col for ($ I = 0; $ I <9; $ I ++) {if ($ I! = $ Row) {if (! $ This-> removeValue ($ I, $ col, $ value) {return false ;}}// square $ rs = intval ($ row/3) * 3; $ cs = intval ($ col/3) * 3; for ($ I = $ rs; $ I <$ rs + 3; $ I ++) {for ($ j = $ cs; $ j <$ cs + 3; $ j ++) {if ($ I! = $ Row & $ j! = $ Col) {if (! $ This-> removeValue ($ I, $ j, $ value) return false ;}}return true;} function removeValue ($ row, $ col, $ value) {$ count = count ($ this-> matrix [$ row] [$ col]); if ($ count = 1) {$ ret =! Isset ($ this-> matrix [$ row] [$ col] [$ value]); return $ ret ;} if (isset ($ this-> matrix [$ row] [$ col] [$ value]) {unset ($ this-> matrix [$ row] [$ col] [$ value]); if ($ count-1 = 1) {return $ this-> setCell ($ row, $ col, current ($ this-> matrix [$ row] [$ col]) ;}} return true ;} function set ($ arr) {for ($ I = 0; $ I <9; $ I ++) {for ($ j = 0; $ j <9; $ j ++) {if ($ arr [$ I] [$ j]> 0) {$ this-> setCell ($ I, $ j, $ arr [$ I] [$ j]);} }}} Function dump () {for ($ I = 0; $ I <9; $ I ++) {for ($ j = 0; $ j <9; $ j ++) {$ c = count ($ this-> matrix [$ I] [$ j]); if ($ c = 1) {echo "". current ($ this-> matrix [$ I] [$ j]). "";} else {echo "(". $ c. ")" ;}} echo "\ n";} echo "\ n";} function dumpAll () {for ($ I = 0; $ I <9; $ I ++) {for ($ j = 0; $ j <9; $ j ++) {echo implode ('', $ this-> matrix [$ I] [$ j]), "\ t" ;}echo "\ n" ;}echo "\ n ";} function calc ($ data ){ $ This-> clear (); $ this-> set ($ data); $ this-> _ calc (); $ this-> dump ();} function _ calc () {for ($ I = 0; $ I <9; $ I ++) {for ($ j = 0; $ j <9; $ j ++) {if (count ($ this-> matrix [$ I] [$ j]) = 1) {continue ;} foreach ($ this-> matrix [$ I] [$ j] as $ v) {$ flag = false; $ t = new Sudoku ($ this-> matrix ); if (! $ T-> setCell ($ I, $ j, $ v) {continue;} if (! $ T-> _ calc () {continue;} $ this-> matrix = $ t-> matrix; return true ;}return false ;}} return true ;}} $ sd = new Sudoku; $ sd-> calc (array (, 0), array ), array (, 2), array (, 6), array (, 0 ), array (, 4), array (, 0), array (, 0 ), array (, 0),); $ sd-> calc (array (, 0), Array (, 5), array (, 3), array (, 0 ), array (, 1), array (, 0), array (9, 0, 7 ), array (, 0), array (, 4 ),)); $ sd-> calc (array (, 5), array (, 0), array, 0, 0, 0, 0), array (, 3), array (, 0), array (, 0), array, 0, 0, 0, 0), array (0, 0, 0, 0, 0, 9, 0), array (, 0), array (9, 0, 0, 2); $ sd-> calc (array, 0, 0, 0, 0, 0, 0), array (0, 0, 3, 1, 0, 5, 0), array (0, 0, 6, 0, 0, 0), array (6, 0, 0, 0, 0, 0, 0, 0), array (, 4), array (, 7), array,, 0), array (, 0), array (, 0, 0),);?>
The running result is as follows:
1 5 2 3 4 6 7 9 8 9 4 7 5 8 2 6 1 3 3 8 6 9 1 7 4 5 2 7 9 1 8 3 4 5 2 6 5 3 4 6 2 9 1 8 7 2 6 8 7 5 1 9 3 4 4 7 3 1 9 8 2 6 5 8 2 9 4 6 5 3 7 1 6 1 5 2 7 3 8 4 9 1 3 7 2 5 6 9 4 8 4 6 8 9 7 3 2 1 5 2 5 9 1 8 4 6 7 3 6 8 5 3 1 7 4 2 9 3 9 4 6 2 8 7 5 1 7 1 2 4 9 5 8 3 6 9 4 6 5 3 2 1 8 7 5 7 1 8 4 9 3 6 2 8 2 3 7 6 1 5 9 4 7 3 8 1 9 6 4 2 5 2 9 6 3 4 5 7 8 1 4 5 1 2 7 8 3 9 6 5 6 9 7 8 4 2 1 3 3 8 2 5 1 9 6 7 4 1 7 4 6 2 3 8 5 9 6 2 7 4 5 1 9 3 8 8 4 5 9 3 2 1 6 7 9 1 3 8 6 7 5 4 2 9 5 1 3 6 7 4 2 8 7 8 3 1 4 2 5 6 9 2 4 6 9 5 8 7 3 1 6 2 9 4 7 5 8 1 3 8 7 5 6 1 3 2 9 4 1 3 4 2 8 9 6 5 7 4 6 7 5 9 1 3 8 2 3 1 8 7 2 6 9 4 5 5 9 2 8 3 4 1 7 6