Eerste poging voor nr 4:
Verborgen inhoud
[pre]class Coord
{
var $x,$y;
function Coord($x,$y) { $this->x=$x; $this->y=$y; }
function Copy() { return new Coord($this->x,$this->y); }
function Neighbors() { return array(new Coord($this->x-1,$this->y),new Coord($this->x+1,$this->y),new Coord($this->x,$this->y-1),new Coord($this->x,$this->y+1)); }
}
class Polyonimo
{
var $c=array();
function Copy() { $p=new Polyonimo; foreach($this->c as $q) $p->c[]=$q->Copy(); return $p; }
function Fix() { $p=$this->c[0]->Copy(); foreach($this->c as $q) { $p->x=min($p->x,$q->x); $p->y=min($p->y,$q->y); } foreach($this->c as &$q) { $q->x -= $p->x; $q->y -= $p->y; } }
function Rotate() { $b=$this->c[0]->y; foreach($this->c as $q) $b=max($b,$q->y); foreach($this->c as &$q) { $z=$b-$q->y; $q->y=$q->x; $q->x=$z; } $this->Fix(); }
function IsEqual($p) { if (count($this->c)!=count($p->c)) return false; foreach($this->c as $q) if (!in_array($q,$p->c)) return false; return true; }
function IsSimilar($p) { for($i=0; $i<4; $i++) { if ($i) $p->Rotate(); if ($this->IsEqual($p)) return true; } return false; }
function GetSurroundingCoords() { $a=array(); foreach($this->c as $q) { $r=$q->Neighbors(); foreach($r as $s) if (!in_array($s,$a) && !in_array($s,$this->c)) $a[]=$s; } return $a; }
function GetExtensions() { $a=array(); $b=$this->GetSurroundingCoords(); foreach($b as $p) { $q=$this->Copy(); $q->c[]=$p; $q->Fix(); $a[]=$q; } return $a; }
}
function GetAllPolyonimos( $n )
{
if ($n==1) { $p=new Polyonimo; $p->c[]=new Coord(0,0); return array($p); }
$a=array(); $p=GetAllPolyonimos($n-1); foreach($p as $q) { $e=$q->GetExtensions(); foreach($e as $r) { foreach($a as $s) if ($r->IsSimilar($s)) { $r=null; break; } if ($r) $a[]=$r; } }
return $a;
}
function CountPolyonimos( $n ) { return count( GetAllPolyonimos($n)); }[/pre]
Bepaald niet optimaal, bij n=10 duurt hij vervelend lang (uitkomst: 9189
)
In theory, there's no difference between theory and practice. In practice, there is.