前些天 玩一个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;
}