0

I am trying to think of the solution for this problem:

Generate all possibilities for triangle between 10 dots which are positioned like this:

.   .   .

.   .   . 

.   .   .

    .

(like a keypad on regular mobile phone)
first line : 1 2 3
second line: 4 5 6
third line : 7 8 9
forth line : 0

I am using PHP. I tried to generate using the oposite logic. (get all combinations and remove all combination that are not triangles, but that is too complicated). Is anyone have any suggestion?

4

2 回答 2

2

我想到了以下几点:

$banned = array(5,8,10);
$array = array();
for($i=1;$i<11;$i++){
    for($j=1;$j<11;$j++){
        for($k=1;$k<11;$k++){
            if($i != $j && $j != $k && $i != $k){
                $tmp = array($i, $j, $k);
                sort($tmp);
                if($tmp[1]-$tmp[0] !== $tmp[2]-$tmp[1] AND $tmp !== $banned){
                    $array[] = $tmp;
                }
            }
        }
    }
}
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));// remove duplicates
print_r($array); // printing
于 2013-06-07T23:46:00.893 回答
2
  1. 选择三个点
  2. 测试它们是否可以形成一个三角形(只需测试它们是否在一条线上)
  3. 记录下来
  4. 重复
于 2013-06-07T23:25:29.997 回答