开灯,熄灯 小游戏 的 程序解

前些天 玩一个Light On的小游戏,由于智商实在不足,后面的关卡玩不过了, 干脆写个程序解决吧.


以下是 3*3的解决方案.状态空间较小,512种状态, 使用了宽度优先搜索算法.


    public function test2(){    

        for($n=0;$n<512;$n++){
            $bin=str_pad( decbin($n),9,'0',STR_PAD_LEFT);
            $path=self::test($bin);
            echo $bin.'=>'.$path."\r\n";
        }
            
    }
    
    public function test($init){
        $end='111111111';
        $operators=array(
            '110100000',
            '111010000',
            '011001000',
            '100110100',
            '010111010',
            '001011001',
            '000100110',
            '000010111',
            '000001011',
        );
        $stack=array($init=>'');
        while(true){
            list($now,$path)=each($stack);
            for($i=0;$i<9;$i++){
                $to=self::tOperator($now, $operators[$i]);
                if($to==$end){
                    return $path.$i;
                }
                if(isset($stack[$to])){
                    continue;
                }
                $stack[$to]=$path.$i;
            }
        }
    }
    private function tOperator($now,$operator){
        $src=$now;
        $now=(string)$now;
        for($i=0;$i<9;$i++){
            $now[$i]=intval($now[$i]) ^ intval($operator[$i]);
        }
        //echo $src.'^'.$operator.'=>'.$now.'<br/>';
        return $now;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值