Home >Backend Development >PHP Tutorial >关于九宫格算法的代码

关于九宫格算法的代码

WBOY
WBOYOriginal
2016-06-23 13:26:391056browse


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

仿照网上九宫格穷举代码,结果死循环的,请各位看看。


回复讨论(解决方案)

ignore_user_abort(true);set_time_limit(0);$tmp = array();function getArr($arr=''){    for ($i = 1; $i <= 9; $i++) {        if ( empty($arr) ) {            $arr[] = $i;        } elseif ( in_array($i, $arr) ) {            continue;        } else {            $arr[] = $i;        }        if ( count($arr) < 9 ) {            getArr($arr);        }        if ( count($arr) < 9 && count($arr) > 1 ) {            array_pop($arr);            continue;        } elseif ( count($arr) == 1 ) {            unset($arr);            continue;        } elseif ( checkArr($arr) ) {            $GLOBALS['tmp'][] = $arr;        }    }}function checkArr($arr){    $m = array();    $m[] = $arr[0] + $arr[1] + $arr[2];    $m[] = $arr[3] + $arr[4] + $arr[5];    $m[] = $arr[6] + $arr[7] + $arr[8];    $m[] = $arr[0] + $arr[3] + $arr[6];    $m[] = $arr[1] + $arr[4] + $arr[7];    $m[] = $arr[2] + $arr[5] + $arr[8];    $m[] = $arr[0] + $arr[4] + $arr[8];    $m[] = $arr[2] + $arr[4] + $arr[6];    $tmp = array_count_values($m);    foreach ($tmp as $v) {        if ( $v == 8 ) {            return true;        } else {            return false;        }    }}$startTime = microtime(true);getArr();$endTime = microtime(true);for( $i=0; $i<count($tmp); $i++ ) {$arr = $tmp[$i];echo $str=<<<fs    <table border="1" style="float:left;margin-left:10px;">        <tr>            <td>{$arr[0]}</td>            <td>{$arr[1]}</td>            <td>{$arr[2]}</td>        </tr>        <tr>            <td>{$arr[3]}</td>            <td>{$arr[4]}</td>            <td>{$arr[5]}</td>        </tr>        <tr>            <td>{$arr[6]}</td>            <td>{$arr[7]}</td>            <td>{$arr[8]}</td>        </tr>    </table>fs;}echo '<div style="float:left;width:100%;height:5px;clear:both"></div><h1 style="float:left">共花费时间:';echo round($endTime - $startTime, 3);echo '秒</h1>';die;//该片段来自于http://outofmemory.cn

这是网上的例子。

<?phpset_time_limit(300);print_r(test());function test(){    $ret = array();    for($a=0; $a<12; $a++){        for($b=0; $b<12; $b++){            for($c=0; $c<12; $c++){                for($d=0; $d<12; $d++){                    for($e=0; $e<12; $e++){                        for($f=1; $f<12; $f++){                            for($g=0; $g<12; $g++){                                for($h=0; $h<12; $h++){                                    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){                                        return array(                                                'a' => $a,                                                'b' => $b,                                                'c' => $c,                                                'd' => $d,                                                'e' => $e,                                                'f' => $f,                                                'g' => $g,                                                'h' => $h                                            );                                    }                                }                            }                        }                    }                }            }        }    }    return $ret;}?>

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

计算量有点大。。

Statement:
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