{"id":727,"date":"2017-05-03T14:15:30","date_gmt":"2017-05-03T06:15:30","guid":{"rendered":"http:\/\/www.rain1024.com\/?p=727"},"modified":"2023-08-07T20:57:27","modified_gmt":"2023-08-07T12:57:27","slug":"php-article79","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/05\/03\/php-article79\/","title":{"rendered":"PHP\u5b9e\u73b0\u6570\u72ec\u7834\u89e3(\u53ef\u4ee5\u76f4\u63a5\u6d4b\u8bd5\u4f7f\u7528)"},"content":{"rendered":"<h2>\u6709\u65f6\u5019\u4f1a\u73a9\u6570\u72ec\u6e38\u620f\uff0c\u73a9\u7684\u591a\u4e86\u4f1a\u53d1\u73b0\u53ef\u4ee5\u4f7f\u7528\u8ba1\u7b97\u673a\u6765\u6c42\u89e3\u6570\u72ec\u91cc\u7684\u6570\u5b57\uff0c\u4f7f\u7528\u9012\u5f52\u8fdb\u884c\u8fdb\u884c\u904d\u5386\u5373\u53ef\uff0c\u6240\u4ee5\u5c31\u5199\u4e86\u8fd9\u4e48\u4e00\u4e2a\u5c0f\u529f\u80fd\uff0c\u653e\u5728\u6211\u7684\u7f51\u7ad9\u91cc\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u7814\u7a76\u4ee3\u7801\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\uff0c\u8981\u4f7f\u7528\u6233\u8fd9\u91cc:<a href=\"http:\/\/www.rain1024.com\/more\/sudoku_web.php\">\u6570\u72ec\u7834\u89e3\u6d4b\u8bd5\u4f7f\u7528<\/a><\/h2>\n<h2>\u672c\u529f\u80fd\u4f7f\u7528\u4e24\u4e2aphp\u9875\u9762\u5b8c\u6210\uff0csudoku_web.php\u662f\u7528\u6765\u8f93\u5165\u6570\u636e\u7684\uff0c\u800csudoku_with.php\u662f\u7528\u6765\u5904\u7406\u6570\u636e\u7684\uff0c\u4e0d\u8981\u968f\u610f\u8bbf\u95eephp\u9875\u9762\uff0c\u5426\u5219\u4f1a\u88ab\u91cd\u5b9a\u5411\u3002\u4f7f\u7528\u65f6\u8981\u6ce8\u610f\u8f93\u5165\u7684\u6570\u636e\u6b63\u786e\uff0c\u6570\u72ec\u53ea\u67091-9,\u51719\u4e2a\u6570\u5b57\uff0c\u8bf7\u4e0d\u8981\u8f93\u5165\u5176\u4ed6\u5b57\u7b26\uff0c\u7834\u89e3\u7ed3\u679c\u4e2d\uff0c\u7070\u8272\u8f93\u5165\u6846\u662f\u4f7f\u7528\u8005\u81ea\u5df1\u8f93\u5165\u7684\u6570\u636e\uff0c\u672c\u529f\u80fd\u4f5c\u4e3a\u6d4b\u8bd5\uff0c\u5982\u6709\u96f7\u540c\uff0c\u7eaf\u5c5e\u5de7\u5408\u3002<\/h2>\n<h2>\u6548\u679c\u56fe<\/h2>\n<h3>\u8f93\u5165\u6570\u636e<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/php15.jpg\" alt=\"\" \/><\/p>\n<h3>\u7834\u89e3\u7ed3\u679c<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/php16.jpg\" alt=\"\" \/><\/p>\n<h2>\u4e0b\u9762\u662f\u4ee3\u7801<\/h2>\n<h3>sudoku_web.php<\/h3>\n<pre><code class=\"PHP\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;title&gt;Title&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;style&gt;\n    .form{\n        width: 395px;\n        height: 200px;\n    }\n    input{\n        width: 30px;\n        height: 30px;\n        font-size: 18px;\n        text-align: center;\n    }\n    td{\n\n    }\n&lt;\/style&gt;\n&lt;div align=\"center\"&gt;\n    &lt;h1&gt;\u6570\u72ec\u7834\u89e3&lt;\/h1&gt;\n    \uff08\u8bf7\u5728\u5bf9\u5e94\u6587\u672c\u6846\u4e2d\u8f93\u5165\u5df2\u7ecf\u63d0\u793a\u7684\u6570\u5b57\uff0c\u8bf7\u4fdd\u8bc1\u8f93\u5165\u7684\u53ea\u6709\u6570\u5b57\uff0c\u7136\u540e\u70b9\u51fb\u63d0\u4ea4\uff09\n    &lt;form action=\"sudoku.php\" method=\"post\"&gt;\n    &lt;table border=\"0\" &gt;\n        &lt;?php\n            $sum = 0;\n            for ($i=0;$i&lt;9;$i++){\n                echo \"&lt;tr&gt;\";\n                for ($j=0;$j&lt;9;$j++){\n                    $sum = $sum + 1;\n                    echo \"&lt;td&gt;&lt;input type=\\\"text\\\" name=\\\"fig\".$sum.\"\\\" max=\\\"9\\\" min=\\\"1\\\" maxlength=\\\"1\\\" size=\\\"1\\\" value=\\\"\\\"&gt;&lt;\/td&gt;\";\n                    if($sum%3==0){\n                        echo \"&lt;td&gt;&lt;\/td&gt;\";\n                    }\n                }\n                echo \"&lt;\/tr&gt;\";\n                if($sum%27==0){\n                    echo \"&lt;tr&gt;&lt;\/tr&gt;&lt;tr&gt;&lt;\/tr&gt;\";\n                }\n            }\n\n        ?&gt;\n    &lt;\/table&gt;\n    &lt;input type=\"submit\" value=\"\u63d0\u4ea4\" style=\"width: 50px;height: 30px;\"&gt;\u00a0\u00a0\u00a0&lt;input type=\"reset\" value=\"\u91cd\u7f6e\" style=\"width: 50px;height: 30px;\"&gt;\n    &lt;\/form&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<h3>sudoku_with.php<\/h3>\n<pre><code class=\"php\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;title&gt;Title&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;style&gt;\n    .form{\n        width: 395px;\n        height: 200px;\n    }\n    input{\n        width: 30px;\n        height: 30px;\n        font-size: 18px;\n        text-align: center;\n    }\n    td{\n\n    }\n&lt;\/style&gt;\n&lt;?php\n\/**\n * Created by PhpStorm.\n * User: Rain\n * Date: 2017\/5\/2\n * Time: 21:29\n *\/\n\/\/$old_host =  $_SERVER['HTTP_REFERER'];\n\/\/if($old_host != 'http:\/\/www.rain1024.com\/more\/sudoku_web.php'){\n\/\/    $url = 'http:\/\/www.rain1024.com\/more\/sudoku_web.php';\n\/\/    header(\"Locatio:$url\");\n\/\/    exit();\n\/\/}\n\/\/$host = $_SERVER['HTTP_HOST'];\n\/\/if($host=='manage.rain1024.com'){\n\/\/    $url = 'manage.rain1024.com\/manage\/index.php';\n\/\/    header(\"Locatio:$url\");\n\/\/    exit();\n\/\/}\n$sudoku_arr = array();\nfor ($i = 0; $i &lt; 20; $i++) {\n    for ($j = 0; $j &lt; 20; $j++) {\n        $sudoku_arr[$i][$j] = 0;\n    }\n}\n$sum = 0;\nfor ($i = 0; $i &lt; 9; $i++) {\n    for ($j = 0; $j &lt; 9; $j++) {\n\n        $sum = $sum + 1;\n        \/**\n         * \u8981\u5148\u5224\u65ad\u8f93\u5165\u7684\u7c7b\u578b\uff0c\u53ea\u80fd\u662f1-9\u7684\u6570\u5b57\n         *\/\n        if (empty($_POST['fig' . $sum])) {\n            $sudoku_arr[$i][$j] = 0;\n        } else {\n            $sudoku_arr[$i][$j] = intval($_POST['fig' . $sum]);\n        }\n    }\n}\n$new_arr = $sudoku_arr;\n\/\/var_dump($sudoku_arr);\n\/* \u6570\u72ec\u6c42\u89e3\u7a0b\u5e8f\n * Created on 2017-4-18\n *\n *\/\nclass Sudoku {\n    var $matrix;\n    function __construct($arr = null) {\n        if ($arr == null) {\n            $this-&gt;clear();\n        } else {\n            $this-&gt;matrix = $arr;\n        }\n    }\n    function clear() {\n        for($i=0; $i&lt;9; $i++) {\n            for($j=0; $j&lt;9; $j++) {\n                $this-&gt;matrix[$i][$j] = array();\n                for ($k = 1; $k &lt;= 9; $k++) {\n                    $this-&gt;matrix[$i][$j][$k] = $k;\n                }\n            }\n        }\n    }\n    function setCell($row, $col, $value){\n        $this-&gt;matrix[$row][$col] = array($value =&gt; $value);\n        \/\/row\n        for($i = 0; $i &lt; 9; $i++){\n            if($i != $col){\n                if(! $this-&gt;removeValue($row, $i, $value)) {\n                    return false;\n                }\n            }\n        }\n        \/\/col\n        for($i = 0; $i &lt; 9; $i++){\n            if($i != $row){\n                if(! $this-&gt;removeValue($i, $col, $value)) {\n                    return false;\n                }\n            }\n        }\n        \/\/square\n        $rs=intval($row \/ 3) * 3;\n        $cs=intval($col \/ 3) * 3;\n        for($i = $rs; $i &lt; $rs + 3; $i++){\n            for($j = $cs; $j &lt; $cs + 3; $j++){\n                if($i != $row &amp;&amp; $j != $col){\n                    if(! $this-&gt;removeValue($i, $j, $value))\n                        return false;\n                }\n            }\n        }\n        return true;\n    }\n    function removeValue($row, $col, $value) {\n        $count = count($this-&gt;matrix[$row][$col]);\n        if($count == 1){\n            $ret = !isset($this-&gt;matrix[$row][$col][$value]);\n            return $ret;\n        }\n        if (isset($this-&gt;matrix[$row][$col][$value])) {\n            unset($this-&gt;matrix[$row][$col][$value]);\n            if($count - 1 == 1) {\n                return $this-&gt;setCell($row, $col, current($this-&gt;matrix[$row][$col]));\n            }\n        }\n        return true;\n    }\n    function set($arr) {\n        for ($i = 0; $i &lt; 9; $i++) {\n            for ($j = 0; $j &lt; 9; $j++) {\n                if ($arr[$i][$j] &gt; 0) {\n                    $this-&gt;setCell($i, $j, $arr[$i][$j]);\n                }\n            }\n        }\n    }\n    function dump() {\n        for($i = 0; $i &lt; 9; $i++){\n            for($j = 0; $j &lt; 9; $j++){\n                $c = count($this-&gt;matrix[$i][$j]);\n                if($c == 1){\n                    echo \" \".current($this-&gt;matrix[$i][$j]).\" \";\n                } else {\n                    echo \"(\".$c.\")\";\n                }\n            }\n            echo \"&lt;br&gt;\";\n        }\n        echo \"&lt;br&gt;\";\n    }\n    function result() {\n        global $new_arr;\n        for($i = 0; $i &lt; 9; $i++){\n            for($j = 0; $j &lt; 9; $j++){\n                $c = count($this-&gt;matrix[$i][$j]);\n                if($c == 1){\n                    $new_arr[$i][$j] = current($this-&gt;matrix[$i][$j]);\n                } else {\n                    $new_arr[$i][$j] = $c;\n                }\n            }\n\/\/            echo \"&lt;br&gt;\";\n        }\n\/\/        echo \"&lt;br&gt;\";\n    }\n    function dumpAll() {\n        for($i = 0; $i &lt; 9; $i++){\n            for($j = 0; $j &lt; 9; $j++){\n                echo implode('', $this-&gt;matrix[$i][$j]), \"\u00a0\u00a0\";\n            }\n            echo \"&lt;br&gt;\";\n        }\n        echo \"&lt;br&gt;\";\n    }\n    function calc($data) {\n        $this-&gt;clear();\n        $this-&gt;set($data);\n        $this-&gt;_calc();\n        $this-&gt;result();\n\/\/        $this-&gt;dump();\n    }\n    function _calc() {\n        for($i = 0; $i &lt; 9; $i++){\n            for($j = 0; $j &lt; 9; $j++){\n                if(count($this-&gt;matrix[$i][$j]) == 1) {\n                    continue;\n                }\n                foreach($this-&gt;matrix[$i][$j] as $v){\n                    $flag = false;\n                    $t = new Sudoku($this-&gt;matrix);\n                    if(!$t-&gt;setCell($i, $j, $v)){\n                        continue;\n                    }\n                    if(!$t-&gt;_calc()){\n                        continue;\n                    }\n                    $this-&gt;matrix = $t-&gt;matrix;\n                    return true;\n                }\n                return false;\n            }\n        }\n        return true;\n    }\n}\n$sudoku = new Sudoku();\n$sudoku-&gt;calc($sudoku_arr);\n\/\/for ($i = 0; $i &lt; 9; $i++) {\n\/\/    for ($j = 0; $j &lt; 9; $j++) {\n\/\/        echo $new_arr[$i][$j].\"  \";\n\/\/    }\n\/\/    echo \"&lt;br&gt;\";\n\/\/}\n?&gt;\n\n&lt;div align=\"center\"&gt;\n    &lt;h1&gt;\u7834\u89e3\u7ed3\u679c&lt;\/h1&gt;\n    \uff08\u7070\u8272\u8f93\u5165\u6846\u4e3a\u81ea\u5df1\u8f93\u5165\u7684\u503c\uff0c\u70b9\u51fb\u8fd4\u56de\u6309\u94ae\u53ef\u91cd\u65b0\u4fee\u6539\u5185\u5bb9\uff09\n        &lt;table border=\"0\" &gt;\n            &lt;?php\n            $sum = 0;\n            for ($i=0;$i&lt;9;$i++){\n                echo \"&lt;tr&gt;\";\n                for ($j=0;$j&lt;9;$j++){\n                    $sum = $sum + 1;\n                    if($sudoku_arr[$i][$j]==0){\n                        echo \"&lt;td&gt;&lt;input  type=\\\"text\\\" value=\\\"\".$new_arr[$i][$j].\"\\\" max=\\\"9\\\" min=\\\"1\\\" maxlength=\\\"1\\\" size=\\\"1\\\" &gt;&lt;\/td&gt;\";\n                    }else{\n                        echo \"&lt;td&gt;&lt;input style='background-color: #cac5c9' type=\\\"text\\\" max=\\\"9\\\" min=\\\"1\\\" maxlength=\\\"1\\\" size=\\\"1\\\" value=\\\"\".$sudoku_arr[$i][$j].\"\\\"&gt;&lt;\/td&gt;\";\n                    }\n                    if($sum%3==0){\n                        echo \"&lt;td&gt;&lt;\/td&gt;\";\n                    }\n                }\n                echo \"&lt;\/tr&gt;\";\n                if($sum%27==0){\n                    echo \"&lt;tr&gt;&lt;\/tr&gt;&lt;tr&gt;&lt;\/tr&gt;\";\n                }\n            }\n\n            ?&gt;\n        &lt;\/table&gt;\n        &lt;input type=\"button\" value=\"\u8fd4\u56de\" style=\"width: 50px;height: 30px;\" onclick=\"goback()\"&gt;\n&lt;\/div&gt;\n&lt;script&gt;\n    function goback(){\n        history.go(-1);\n    }\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u5019\u4f1a\u73a9\u6570\u72ec\u6e38\u620f\uff0c\u73a9\u7684\u591a\u4e86\u4f1a\u53d1\u73b0\u53ef\u4ee5\u4f7f\u7528\u8ba1\u7b97\u673a\u6765\u6c42\u89e3\u6570\u72ec\u91cc\u7684\u6570\u5b57\uff0c\u4f7f\u7528\u9012\u5f52\u8fdb\u884c\u8fdb\u884c\u904d\u5386\u5373\u53ef\uff0c\u6240\u4ee5\u5c31\u5199\u4e86\u8fd9\u4e48\u4e00\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/05\/03\/php-article79\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":728,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,18],"tags":[45,57],"class_list":["post-727","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-technique","tag-html","tag-php"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=727"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/727\/revisions"}],"predecessor-version":[{"id":1454,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/727\/revisions\/1454"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/728"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=727"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}