数独求解程序 php版



  1 <?php
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[$row][$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(
array(0,5,0,0,0,6,0,9,0),
array(0,4,7,0,8,2,6,0,0),
array(0,8,0,0,0,7,0,5,2),
array(7,0,1,0,3,4,0,0,6),
array(0,3,0,0,2,0,0,8,0),
array(2,0,0,0,0,1,9,0,4),
array(4,7,0,1,0,0,0,6,0),
array(0,0,9,4,6,0,3,7,0),
array(0,1,0,2,0,0,0,4,0),
)); $sd->calc(array(
array(1,0,0,0,0,6,9,0,0),
array(0,0,0,9,0,0,0,0,5),
array(2,0,0,1,0,0,0,0,3),
array(0,0,5,3,0,7,0,2,0),
array(3,0,0,6,0,0,0,0,1),
array(0,1,0,4,0,0,8,0,0),
array(9,0,0,0,0,2,0,0,7),
array(5,0,0,0,0,9,0,0,0),
array(0,0,3,7,0,0,0,0,4),
)); $sd->calc(array(
array(7,0,0,1,0,0,0,0,5),
array(0,0,6,0,4,0,0,8,0),
array(0,0,1,0,0,0,0,0,0),
array(0,6,0,0,8,0,0,0,3),
array(0,8,0,0,0,9,0,7,0),
array(1,0,0,0,0,0,0,5,0),
array(0,0,0,0,0,0,9,0,0),
array(0,4,0,0,3,0,1,0,0),
array(9,0,0,0,0,7,0,0,2),
)); $sd->calc(array(
array(0,5,0,0,0,0,0,2,0),
array(0,0,3,1,0,0,5,0,0),
array(0,0,6,0,0,8,0,0,0),
array(6,0,0,0,0,0,0,1,0),
array(8,0,0,6,0,0,0,0,4),
array(0,3,0,0,0,9,0,0,7),
array(0,0,0,5,0,0,3,0,0),
array(0,0,8,0,0,6,9,0,0),
array(0,9,0,0,0,0,0,7,0),
));

数独 php的更多相关文章

  1. LintCode389.判断数独是否合法

    LintCode简单题:判断数独是否合法 问题描述: 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 注意事项: 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...

  2. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  4. 数独 JAVA实现

    数独游戏的规则从很久之前就知道,但是一直都没怎么玩过,然后到了大学,大一下学期自己学dfs的时候,刚刚好碰到了一个数独的题目,做出来后,感觉还是挺有成就感的 然后大二学了JAVA,看了下那个一些有关于 ...

  5. 用C++实现的解数独(Sudoku)程序

    我是一个C++初学者,控制台实现了一个解数独的小程序. 代码如下: //"数独游戏"V1.0 //李国良于2016年11月11日编写完成 #include <iostream ...

  6. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  7. ACM: ICPC/CCPC Sudoku DFS - 数独

    Sudoku Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Submis ...

  8. codevs 2924 数独挑战

    2924 数独挑战 http://codevs.cn/problem/2924/ 题目描述 Description "芬兰数学家因卡拉,花费3个月时间设计出了世界上迄今难度最大的数独游戏,而 ...

  9. 用html5 canvas和JS写个数独游戏

    为啥要写这个游戏? 因为我儿子二年级数字下册最后一章讲到了数独.他想玩儿. 因为我也想玩有提示功能的数独. 因为我也正想决定要把HTML5和JS搞搞熟.熟悉一个编程平台,最好的办法,就是了解其原理与思 ...

  10. Sudoku 数独游戏

    #include<iostream> using namespace std; bool heng(int **sudo, int a, int b, int value) { bool ...

随机推荐

  1. How to ignore files and directories in subversion?

    Step 1 Copy the files and directories to other place. Step 2 Delete the files and directories. Step ...

  2. robotframework 常用关键字

    标准库 第三方库 其他库

  3. Bootstrap学习遇到的role属性--- 无障碍网页应用属性

    以前接触过Bootstrap,但也只是仅仅接触,现在重新学习下,今天看到一个例子中的属性有一个role, 查阅资料发现这个是--WAI-ARIA无障碍设计属性: 通俗说是该设计为了一些盲人,失聪,残疾 ...

  4. CRC标准以及简记式

    一.CRC标准 下表中列出了一些见于标准的CRC资料: 名称 生成多项式 简记式* 应用举例 CRC-4 x4+x+1 3 ITU G.704 CRC-8 x8+x5+x4+1 31 DS18B20 ...

  5. rails 查看项目的所有路由

    rails routes

  6. vue2.0 移动端,下拉刷新,上拉加载更多插件 转:

    转自:http://www.cnblogs.com/gmajip/p/7204072.html <template lang="html"> <div class ...

  7. Python图表绘制:matplotlib绘图库入门(转)

    matplotlib 是Python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备,并 ...

  8. 747. Largest Number At Least Twice of Others

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. Python中逗号的妙用

    闲着没事打算用Python刷一遍pat,输出过程中遇到了一个这样的问题: 题目1002题目要求 在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格, 但是P ...

  10. MES制造执行系统

    mes :  Manufacturing Execution System 制造执行系统 起因:ERP系统和底层设备之间出现了断层. 包括资源管理,生产调度,单元分配,生产跟踪,性能分析,文档管理,人 ...