关于九宫格算法的代码解决方法

WBOY
Release: 2016-06-13 12:24:34
Original
1027 people have browsed it

关于九宫格算法的代码

<br /><?php<br /><br />ignore_user_abort(true);<br />set_time_limit(0);<br />$tmp = array();<br /><br />function getArr($arr='') {<br />    for ($i = 0; $i <= 11; $i++) {<br />        $arr[] = $i;  <br />        if (count($arr) < 8) {<br />            getArr($arr);<br />        }<br />        <br />        if ( count($arr) < 8 && count($arr) > 1 ) {<br />                array_pop($arr);<br />                continue;<br />            } elseif ( count($arr) == 1 ) {<br />                unset($arr);<br />                continue;<br />            } elseif ( checkArr($arr) ) {<br />                $GLOBALS['tmp'][] = $arr;<br />            }<br />    }<br />}<br /><br />function checkArr($arr)<br />{<br />    $m = array();<br />    $val = 4;<br />    if ($val == $arr[0] + $arr[1] - 9){<br />        $m[] = $arr[0] + $arr[1] - 9;<br />    }    <br />    if ($val == $arr[2] - $arr[3] * $arr[4]) {<br />        $m[] = $arr[2] - $arr[3] * $arr[4];<br />    }    <br />    if ($val == $arr[5] + $arr[6] - $arr[7]) {<br />        $m[] = $arr[5] + $arr[6] - $arr[7];<br />    }<br />    if ($arr[5] != 0) {<br />        if ($val == $arr[0] + $arr[2] / $arr[5]) {<br />            $m[] = $arr[0] + $arr[2] / $arr[5];<br />        }<br />    }<br />    if ($val == $arr[1] - $arr[3] * $arr[6]) {<br />        $m[] = $arr[1] - $arr[3] * $arr[6];<br />    }<br />    if ($val == 9 - $arr[4] - $arr[7]) {<br />        $m[] = 9 - $arr[4] - $arr[7];<br />    }<br />    $tmp = array_count_values($m);<br />    foreach ($tmp as $v) {<br />        if ( $v == 6 ) {<br />            return true;<br />        } else {<br />            return false;<br />        }<br />    }<br />}<br />$startTime = microtime(true);<br />getArr();<br />$endTime = microtime(true);<br />for( $i=0; $i<count($tmp); $i++ ) {<br />$arr = $tmp[$i];<br />echo $str=<<<fs<br />    <table border="1"   style="max-width:90%"><br />        <tr><br />            <td>{$arr[0]}</td><br />            <td>{$arr[1]}</td><br />            <td>9</td><br />        </tr><br />        <tr><br />            <td>{$arr[2]}</td><br />            <td>{$arr[3]}</td><br />            <td>{$arr[4]}</td><br />        </tr><br />        <tr><br />            <td>{$arr[5]}</td><br />            <td>{$arr[6]}</td><br />            <td>{$arr[7]}</td><br />        </tr><br />    </table><br />fs;<br />}<br />echo '<div style="float:left;width:100%;height:5px;clear:both"></div><h1 style="float:left">共花费时间:';<br />echo round($endTime - $startTime, 3);<br />echo '秒</h1>';<br />die;<br />
Copy after login

仿照网上九宫格穷举代码,结果死循环的,请各位看看。
------解决思路----------------------
<br /><?php<br />set_time_limit(300);<br />print_r(test());<br />function test(){<br />    $ret = array();<br />    for($a=0; $a<12; $a++){<br />        for($b=0; $b<12; $b++){<br />            for($c=0; $c<12; $c++){<br />                for($d=0; $d<12; $d++){<br />                    for($e=0; $e<12; $e++){<br />                        for($f=1; $f<12; $f++){<br />                            for($g=0; $g<12; $g++){<br />                                for($h=0; $h<12; $h++){<br />                                    if($a+$b-9==4 && $c-$d*$e==4 && $f+$g-$h==4 && $a+$c/$f==4 && $b-$d*$g==4 && 9-$e-$h==4){<br />                                        return array(<br />                                                'a' => $a,<br />                                                'b' => $b,<br />                                                'c' => $c,<br />                                                'd' => $d,<br />                                                'e' => $e,<br />                                                'f' => $f,<br />                                                'g' => $g,<br />                                                'h' => $h<br />                                            );<br />                                    }<br />                                }<br />                            }<br />                        }<br />                    }<br />                }<br />            }<br />        }<br />    }<br />    return $ret;<br />}<br />?><br />
Copy after login

Array ( [a] => 2 [b] => 11 [c] => 4 [d] => 1 [e] => 0 [f] => 2 [g] => 7 [h] => 5 )

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!