数独求解程序 php版

  1 数独求解程序 php版
  2 
  3 <?php
  4 class Sudoku {
  5     var $matrix;
  6 
  7     function __construct($arr = null) {
  8         if ($arr == null) {
  9             $this->clear();
 10         } else {
 11             $this->matrix = $arr;
 12         }
 13     }
 14     
 15     function clear() {
 16         for($i=0; $i<9; $i++) {
 17             for($j=0; $j<9; $j++) {
 18                 $this->matrix[$i][$j] = array();
 19                 for ($k = 1; $k <= 9; $k++) {
 20                     $this->matrix[$i][$j][$k] = $k;
 21                 }
 22             }
 23         }
 24     }
 25 
 26     function setCell($row, $col, $value){
 27         $this->matrix[$row][$col] = array($value => $value);
 28         //row
 29         for($i = 0; $i < 9; $i++){
 30             if($i != $col){
 31                 if(! $this->removeValue($row, $i, $value)) {
 32                     return false;
 33                 }
 34             }
 35         }
 36         //col
 37         for($i = 0; $i < 9; $i++){
 38             if($i != $row){
 39                 if(! $this->removeValue($i, $col, $value)) {
 40                     return false;
 41                 }
 42             }
 43         }
 44         //square
 45         $rs=intval($row / 3) * 3;
 46         $cs=intval($col / 3) * 3;
 47 
 48         for($i = $rs; $i < $rs + 3; $i++){
 49             for($j = $cs; $j < $cs + 3; $j++){
 50                 if($i != $row && $j != $col){
 51                     if(! $this->removeValue($i, $j, $value))
 52                         return false;
 53                 }
 54             }
 55         }
 56         return true;
 57     }
 58     
 59     function removeValue($row, $col, $value) {
 60         $count = count($this->matrix[$row][$col]);
 61         if($count == 1){
 62             $ret = !isset($this->matrix[$row][$col][$value]);
 63             return $ret;
 64         }
 65         if (isset($this->matrix[$row][$col][$value])) {
 66             unset($this->matrix[$row][$col][$value]);
 67             if($count - 1 == 1) {
 68                 return $this->setCell($row, $col, current($this->matrix[$row][$col]));
 69             }
 70         }
 71         return true;
 72     }
 73     
 74     function set($arr) {
 75         for ($i = 0; $i < 9; $i++) {
 76             for ($j = 0; $j < 9; $j++) {
 77                 if ($arr[$i][$j] > 0) {
 78                     $this->setCell($i, $j, $arr[$i][$j]);
 79                 }
 80             }
 81         }
 82     }
 83 
 84     function dump() {
 85         for($i = 0; $i < 9; $i++){
 86             for($j = 0; $j < 9; $j++){
 87                 $c = count($this->matrix[$i][$j]);
 88                 if($c == 1){
 89                     echo " ".current($this->matrix[$i][$j])." ";
 90                 } else {
 91                     echo "(".$c.")";
 92                 }
 93             }
 94             echo "\n";
 95         }
 96         echo "\n";
 97     }
 98 
 99     function dumpAll() {
100         for($i = 0; $i < 9; $i++){
101             for($j = 0; $j < 9; $j++){
102                 echo implode('', $this->matrix[$i][$j]), "\t";
103             }
104             echo "\n";
105         }
106         echo "\n";
107     }
108 
109     function calc($data) {
110         $this->clear();
111         $this->set($data);
112         $this->_calc();
113         $this->dump();
114     }
115 
116     function _calc() {
117         for($i = 0; $i < 9; $i++){
118             for($j = 0; $j < 9; $j++){
119                 if(count($this->matrix[$i][$j]) == 1) {
120                     continue;
121                 }
122                 foreach($this->matrix[$i][$j] as $v){
123                     $flag = false;
124                     $t = new Sudoku($this->matrix);
125                     if(!$t->setCell($i, $j, $v)){
126                         continue;
127                     }
128                     if(!$t->_calc()){
129                         continue;
130                     }
131                     $this->matrix = $t->matrix;
132                     return true;
133                 }
134                 return false;
135             }
136         }
137         return true;
138     }
139 }
140 
141 $sd=new Sudoku;
142 $sd->calc(array(
143 array(0,5,0,0,0,6,0,9,0),
144 array(0,4,7,0,8,2,6,0,0),
145 array(0,8,0,0,0,7,0,5,2),
146 array(7,0,1,0,3,4,0,0,6),
147 array(0,3,0,0,2,0,0,8,0),
148 array(2,0,0,0,0,1,9,0,4),
149 array(4,7,0,1,0,0,0,6,0),
150 array(0,0,9,4,6,0,3,7,0),
151 array(0,1,0,2,0,0,0,4,0),
152 ));
153 
154 $sd->calc(array(
155 array(1,0,0,0,0,6,9,0,0),
156 array(0,0,0,9,0,0,0,0,5),
157 array(2,0,0,1,0,0,0,0,3),
158 array(0,0,5,3,0,7,0,2,0),
159 array(3,0,0,6,0,0,0,0,1),
160 array(0,1,0,4,0,0,8,0,0),
161 array(9,0,0,0,0,2,0,0,7),
162 array(5,0,0,0,0,9,0,0,0),
163 array(0,0,3,7,0,0,0,0,4),
164 ));
165 
166 $sd->calc(array(
167 array(7,0,0,1,0,0,0,0,5),
168 array(0,0,6,0,4,0,0,8,0),
169 array(0,0,1,0,0,0,0,0,0),
170 array(0,6,0,0,8,0,0,0,3),
171 array(0,8,0,0,0,9,0,7,0),
172 array(1,0,0,0,0,0,0,5,0),
173 array(0,0,0,0,0,0,9,0,0),
174 array(0,4,0,0,3,0,1,0,0),
175 array(9,0,0,0,0,7,0,0,2),
176 ));
177 
178 $sd->calc(array(
179 array(0,5,0,0,0,0,0,2,0),
180 array(0,0,3,1,0,0,5,0,0),
181 array(0,0,6,0,0,8,0,0,0),
182 array(6,0,0,0,0,0,0,1,0),
183 array(8,0,0,6,0,0,0,0,4),
184 array(0,3,0,0,0,9,0,0,7),
185 array(0,0,0,5,0,0,3,0,0),
186 array(0,0,8,0,0,6,9,0,0),
187 array(0,9,0,0,0,0,0,7,0),
188 ));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值