nouvel algoritme plus rapide

This commit is contained in:
2025-04-29 08:57:08 +02:00
parent 2445bfcf54
commit 1416b8bd54
4 changed files with 223 additions and 95 deletions

View File

@ -8,127 +8,106 @@ class Grille {
public $hauteur;
public $largeur;
private $grilles;
private $debuts;
private $lettres_suivantes;
private $positions;
private $nb_positions;
private $mots_utilises = [];
public function __construct($hauteur, $largeur, $id="") {
$this->hauteur = $hauteur;
$this->largeur = $largeur;
$this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '.'));
$this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, ''));
if ($id == "") {
mt_srand();
} else {
mt_srand(crc32($id));
}
$this->debuts = [];
$this->lettres_suivantes = [];
foreach ($hauteur == $largeur? [$hauteur]: [$hauteur, $largeur] as $longueur) {
$this->debuts[$longueur] = [];
$nb_mots = 0;
$this->lettres_suivantes[$longueur] = [];
foreach(mots_espaces($longueur) as $mot) {
for ($i = 0; $i <= $longueur; $i++) {
$debut = substr($mot, 0, $i);
if (!isset($this->debuts[$longueur][$debut])) {
$this->debuts[$longueur][$debut] = [];
if (!isset($this->lettres_suivantes[$longueur][$debut])) {
$this->lettres_suivantes[$longueur][$debut] = [];
}
$this->debuts[$longueur][$debut][] = $mot;
$this->lettres_suivantes[$longueur][$debut][substr($mot, $i, 1)] = true;
}
}
}
mt_srand();
$this->positions = [];
for ($y = 0; $y < $hauteur; $y++) {
for ($x = 0; $x < $largeur; $x++) {
$this->positions[] = [$x, $y];
}
}
$this->nb_positions = count($this->positions);
$this->grilles = $this->generateur();
}
public function get_ligne($l, $largeur) {
public function get_ligne($y, $largeur) {
$ligne = "";
for ($c = 0; $c < $largeur; $c++) {
$ligne .= $this->grille[$l][$c];
for ($x = 0; $x < $largeur; $x++) {
$ligne .= $this->grille[$y][$x];
}
return $ligne;
}
public function set_ligne($l, $mot) {
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$l][$i] = $mot[$i];
}
}
public function get_colonne($c, $hauteur) {
public function get_colonne($x, $hauteur) {
$colonne = "";
for ($l = 0; $l < $hauteur; $l++) {
$colonne .= $this->grille[$l][$c];
for ($y = 0; $y < $hauteur; $y++) {
$colonne .= $this->grille[$y][$x];
}
return $colonne;
}
public function set_colonne($c, $mot) {
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$i][$c] = $mot[$i];
}
}
public function generateur() {
yield from $this->trouve_une_ligne(0);
$this->grille = array_fill(0, $this->hauteur, array_fill(0, $this->largeur, ' '));
}
private function trouve_une_ligne($l) {
global $mots_de_n_lettres;
$largeur = min($l, $this->largeur);
$hauteur = min($l + 1, $this->hauteur);
foreach ($this->debuts[$this->largeur][$this->get_ligne($l, $largeur)] as $mot_lig) {
$this->set_ligne($l, $mot_lig);
$ok = true;
for ($c = $l; $c < $this->largeur; $c++) {
if (!isset($this->debuts[$this->hauteur][$this->get_colonne($c, $hauteur)])) {
$ok = false;
break;
}
}
if (!$ok) {
continue;
}
$this->mots_utilises[$mot_lig] = true;
if ($l < $this->largeur) {
yield from $this->trouve_une_colonne($l);
} else if ($l + 1 < $this->hauteur) {
yield from $this->trouve_une_ligne($l + 1);
} else {
yield $this;
}
unset($this->mots_utilises[$mot_lig]);
public function generateur($index=0) {
if ($index == $this->nb_positions) {
yield $this;
return;
}
}
private function trouve_une_colonne($c) {
global $mots_de_n_lettres;
$hauteur = min($c + 1, $this->hauteur);
$largeur = min($c + 1, $this->largeur);
foreach ($this->debuts[$this->hauteur][$this->get_colonne($c, $hauteur)] as $mot_col) {
if (isset($this->mots_utilises[$mot_col])) {
continue;
}
$this->set_colonne($c, $mot_col);
$ok = true;
for ($l = $c; $l < $this->hauteur; $l++) {
if (!isset($this->debuts[$this->largeur][$this->get_ligne($l, $largeur)])) {
$ok = false;
break;
[$x, $y] = $this->positions[$index];
$lettres_possibles = array_intersect_assoc(
$this->lettres_suivantes[$this->largeur][$this->get_ligne($y, $x)],
$this->lettres_suivantes[$this->hauteur][$this->get_colonne($x, $y)]
);
foreach ($lettres_possibles as $lettre => $_) {
$this->grille[$y][$x] = $lettre;
$mot_ligne = NULL;
if ($x == $this->largeur - 1) {
$mot_ligne = $this->get_ligne($y, $x);
if (isset($this->mots_utilises[$mot_ligne])) {
continue;
} else {
$this-> mots_utilises[$mot_ligne] = true;
}
}
if (!$ok) {
continue;
$mot_colonne = NULL;
if ($y == $this->hauteur - 1) {
$mot_colonne = $this->get_colonne($x, $y);
if (isset($this->mots_utilises[$mot_colonne])) {
continue;
} else {
$this-> mots_utilises[$mot_colonne] = true;
}
}
$this->mots_utilises[$mot_col] = true;
if ($c +1 < $this->hauteur) {
yield from $this->trouve_une_ligne($c + 1);
} else if ($c + 1 < $this->largeur) {
yield from $this->trouve_une_colonne($c + 1);
} else {
yield $this;
yield from $this->generateur($index + 1);
if ($mot_ligne) {
unset($this-> mots_utilises[$mot_ligne]);
}
if ($mot_colonne) {
unset($this-> mots_utilises[$mot_colonne]);
}
unset($this->mots_utilises[$mot_col]);
}
}