欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實(shí)現(xiàn)的數(shù)獨(dú)求解問題示例

 更新時(shí)間:2017年04月18日 10:46:04   作者:e路相扶  
這篇文章主要介紹了PHP實(shí)現(xiàn)的數(shù)獨(dú)求解問題,涉及php數(shù)組與字符串的遍歷、比較、判斷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP實(shí)現(xiàn)的數(shù)獨(dú)求解問題。分享給大家供大家參考,具體如下:

一、數(shù)獨(dú)問題描述:

對于給出的數(shù)字二維數(shù)組,要求每行每列的數(shù)字不能重復(fù)。

二、實(shí)現(xiàn)代碼:

<?php
/* 數(shù)獨(dú)求解程序
 * 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[$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),
));
?>

運(yùn)行結(jié)果如下:

 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 

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php程序設(shè)計(jì)算法總結(jié)》、《php排序算法總結(jié)》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php實(shí)現(xiàn)mysql備份恢復(fù)分卷處理的方法

    php實(shí)現(xiàn)mysql備份恢復(fù)分卷處理的方法

    這篇文章主要介紹了php實(shí)現(xiàn)mysql備份恢復(fù)分卷處理的方法,包括完整的MySQL備份恢復(fù)類文件及用法實(shí)例,注釋包含了詳盡的用法說明,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-12-12
  • PHP實(shí)現(xiàn)基于棧的后綴表達(dá)式求值功能

    PHP實(shí)現(xiàn)基于棧的后綴表達(dá)式求值功能

    這篇文章主要介紹了PHP實(shí)現(xiàn)基于棧的后綴表達(dá)式求值功能,簡單描述了后綴表達(dá)式的概念并結(jié)合實(shí)例形式分析了php使用棧實(shí)現(xiàn)后綴表達(dá)式求值的相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • php利用cookies實(shí)現(xiàn)購物車的方法

    php利用cookies實(shí)現(xiàn)購物車的方法

    這篇文章主要介紹了php利用cookies實(shí)現(xiàn)購物車的方法,可通過cookie實(shí)現(xiàn)對商品的增刪改等功能,以及統(tǒng)計(jì)與檢查等技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-12-12
  • 使用PHP會話(Session)實(shí)現(xiàn)用戶登陸功能

    使用PHP會話(Session)實(shí)現(xiàn)用戶登陸功能

    本篇文章是對PHP會話(Session)實(shí)現(xiàn)用戶登陸功能進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • CodeIgniter php mvc框架 中國網(wǎng)站

    CodeIgniter php mvc框架 中國網(wǎng)站

    CodeIgniter 是一個(gè)小巧但功能強(qiáng)大的 PHP 框架,作為一個(gè)簡單而“優(yōu)雅”的工具包,它可以為 PHP 程序員建立功能完善的 Web 應(yīng)用程序。如果你是一個(gè)使用共享主機(jī),并且為客戶所要求的期限而煩惱的開發(fā)人員,如果你已經(jīng)厭倦了那些傻大笨粗的框架
    2008-05-05
  • php array_intersect()函數(shù)使用代碼

    php array_intersect()函數(shù)使用代碼

    array_intersect() 返回一個(gè)數(shù)組,該數(shù)組包含了所有在 array1 中也同時(shí)出現(xiàn)在所有其它參數(shù)數(shù)組中的值。注意鍵名保留不變。
    2009-01-01
  • php沒有文件被上傳的實(shí)例分析及解決辦法

    php沒有文件被上傳的實(shí)例分析及解決辦法

    在本篇文章里小編給大家整理的是一篇關(guān)于php沒有文件被上傳的實(shí)例分析及解決辦法,有興趣的朋友們可以跟著學(xué)習(xí)參考下。
    2021-11-11
  • Yii配置與使用memcached緩存的方法

    Yii配置與使用memcached緩存的方法

    這篇文章主要介紹了Yii配置與使用memcached緩存的方法,簡單分析了mencached的下載、安裝、Yii配置及簡單使用技巧,需要的朋友可以參考下
    2016-07-07
  • 詳解php如何解密json字符串

    詳解php如何解密json字符串

    解密JSON字符串在PHP中并不常見,因?yàn)镴SON通常用于數(shù)據(jù)交換,并不需要加密,本文將給大家詳細(xì)介紹了php如何解密json字符串,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下
    2024-02-02
  • PHP文件讀寫操作之文件讀取方法詳解

    PHP文件讀寫操作之文件讀取方法詳解

    在上一篇PHP教程中介紹了如何利用PHP實(shí)現(xiàn)文件讀寫操作中的文件寫入功能,接下來和大家分享PHP文件讀取功能的應(yīng)用實(shí)例
    2011-01-01

最新評論