时间:2021-07-01 10:21:17 帮助过:27人阅读
$dict = array('A', 'B', 'C', 'D', 'E', 'F');
//搜索 F太太成了C太太的儿媳妇的儿媳妇的儿媳妇的儿媳妇
$st = array(
array('F' => 'A'),
array('F' => 'B'),
array('F' => 'C'),
array('F' => 'D'),
array('F' => 'E'),
);
while($rs = array_shift($st)) {
$len = count($rs);
$k = end($rs);
if(isset($rs[$k])) continue;
foreach($dict as $v) {
if($k == $v
------解决方案--------------------
in_array($v, $rs)) continue;
if($len == 4 && $v == 'C') $res[] = array_merge($rs, array($k => $v));
else $st[] = array_merge($rs, array($k => $v));
}
}
//搜索结束,共 24 组
foreach($res as $i=>$r) {
//配齐 6 个
$k = current(array_diff($dict, array_keys($r)));
$v = current(array_diff($dict, $r));
if($k == $v) continue;
$r = array_merge($r, array($k => $v));
//判断,两个条件不能同时满足
if($r[$r['B']] == 'A' && $r[$r[$r['D']]] == 'E') print_r($r);
//但分别可以
// if($r[$r['B']] == 'A') print_r($r); //A成了B的继父的继父
// if($r[$r[$r['D']]] == 'E') print_r($r); //D太太成了E太太的婆婆的婆婆的婆婆
}