nouvel algoritme plus rapide

This commit is contained in:
Adrien MALINGREY 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]);
}
}

149
Grille0.php Normal file
View File

@ -0,0 +1,149 @@
<?php
include_once "dico.php";
class Grille {
public $grille;
public $hauteur;
public $largeur;
private $grilles;
private $debuts;
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, '.'));
if ($id == "") {
mt_srand();
} else {
mt_srand(crc32($id));
}
$this->debuts = [];
foreach ($hauteur == $largeur? [$hauteur]: [$hauteur, $largeur] as $longueur) {
$this->debuts[$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] = [];
}
$this->debuts[$longueur][$debut][] = $mot;
}
}
}
mt_srand();
$this->grilles = $this->generateur();
}
public function get_ligne($y, $largeur) {
$ligne = "";
for ($x = 0; $x < $largeur; $x++) {
$ligne .= $this->grille[$y][$x];
}
return $ligne;
}
public function set_ligne($y, $mot) {
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$y][$i] = $mot[$i];
}
}
public function get_colonne($x, $hauteur) {
$colonne = "";
for ($y = 0; $y < $hauteur; $y++) {
$colonne .= $this->grille[$y][$x];
}
return $colonne;
}
public function set_colonne($x, $mot) {
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$i][$x] = $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($y) {
global $mots_de_n_lettres;
$largeur = min($y, $this->largeur);
$hauteur = min($y + 1, $this->hauteur);
foreach ($this->debuts[$this->largeur][$this->get_ligne($y, $largeur)] as $mot_lig) {
$this->set_ligne($y, $mot_lig);
$ok = true;
for ($x = $y; $x < $this->largeur; $x++) {
if (!isset($this->debuts[$this->hauteur][$this->get_colonne($x, $hauteur)])) {
$ok = false;
break;
}
}
if (!$ok) {
continue;
}
$this->mots_utilises[$mot_lig] = true;
if ($y < $this->largeur) {
yield from $this->trouve_une_colonne($y);
} else if ($y + 1 < $this->hauteur) {
yield from $this->trouve_une_ligne($y + 1);
} else {
yield $this;
}
unset($this->mots_utilises[$mot_lig]);
}
}
private function trouve_une_colonne($x) {
global $mots_de_n_lettres;
$hauteur = min($x + 1, $this->hauteur);
$largeur = min($x + 1, $this->largeur);
foreach ($this->debuts[$this->hauteur][$this->get_colonne($x, $hauteur)] as $mot_col) {
if (isset($this->mots_utilises[$mot_col])) {
continue;
}
$this->set_colonne($x, $mot_col);
$ok = true;
for ($y = $x; $y < $this->hauteur; $y++) {
if (!isset($this->debuts[$this->largeur][$this->get_ligne($y, $largeur)])) {
$ok = false;
break;
}
}
if (!$ok) {
continue;
}
$this->mots_utilises[$mot_col] = true;
if ($x +1 < $this->hauteur) {
yield from $this->trouve_une_ligne($x + 1);
} else if ($x + 1 < $this->largeur) {
yield from $this->trouve_une_colonne($x + 1);
} else {
yield $this;
}
unset($this->mots_utilises[$mot_col]);
}
}
public function current() {
return $this->grilles->current();
}
public function valid() {
return $this->grilles->valid();
}
public function hash() {
$string = "";
foreach ($this->grille as $ligne) {
$string .= implode("", $ligne);
}
return hash('sha256', $string);
}
}

View File

@ -2,7 +2,7 @@
const MIN_LETTRES_MOT_1 = 2;
const MIN_LETTRES_MOT_2 = 0;
const MIN_LETTRES_MOT_2 = 1;
// const MAX_MOTS = 100000;

View File

@ -84,17 +84,17 @@ $grille->current();
<table class="grille">
<tr>
<th></th>
<?php for ($c = 0; $c < $largeur; $c++): ?>
<th><?= chr($c + 65) ?></th>
<?php for ($x = 0; $x < $largeur; $x++): ?>
<th><?= chr($x + 65) ?></th>
<?php endfor; ?>
<th></th>
</tr>
<?php for ($l = 0; $l < $hauteur; $l++): ?>
<?php for ($y = 0; $y < $hauteur; $y++): ?>
<tr>
<th><?= $l + 1 ?></th>
<?php for ($c = 0; $c < $largeur; $c++): ?>
<td class="case <?= $grille->grille[$l][$c] == " " ? "noire" : "blanche" ?>">
<?php if ($grille->grille[$l][$c] == " "): ?>
<th><?= $y + 1 ?></th>
<?php for ($x = 0; $x < $largeur; $x++): ?>
<td class="case <?= $grille->grille[$y][$x] == " " ? "noire" : "blanche" ?>">
<?php if ($grille->grille[$y][$x] == " "): ?>
<input type="text" maxlength="1" size="1" value=" " disabled />
<?php else: ?>
<input type="text" maxlength="1" size="1" pattern="[A-Z]" />
@ -108,14 +108,14 @@ $grille->current();
<div class="horizontales">
<h2>Horizontalement</h2>
<ol>
<?php for ($l = 0; $l < $hauteur; $l++): ?>
<?php for ($y = 0; $y < $hauteur; $y++): ?>
<li>
<?php $definitions = $dico[$grille->get_ligne($l, $largeur)] ?>
<?php $definitions = $dico[$grille->get_ligne($y, $largeur)] ?>
<?php if (count($definitions) == 1): ?>
<?= $definitions[0] ?>
<?php else: ?>
<ol>
<?php foreach ($dico[$grille->get_ligne($l, $largeur)] as $definition) : ?>
<?php foreach ($dico[$grille->get_ligne($y, $largeur)] as $definition) : ?>
<li><?= $definition ?></li>
<?php endforeach ?>
</ol>
@ -127,14 +127,14 @@ $grille->current();
<div class="verticales">
<h2>Verticalement</h2>
<ol type="A">
<?php for ($c = 0; $c < $largeur; $c++): ?>
<?php for ($x = 0; $x < $largeur; $x++): ?>
<li>
<?php $definitions = $dico[$grille->get_colonne($c, $hauteur)] ?>
<?php $definitions = $dico[$grille->get_colonne($x, $hauteur)] ?>
<?php if (count($definitions) == 1): ?>
<?= $definitions[0] ?>
<?php else: ?>
<ol>
<?php foreach ($dico[$grille->get_colonne($c, $hauteur)] as $definition) : ?>
<?php foreach ($dico[$grille->get_colonne($x, $hauteur)] as $definition) : ?>
<li><?= $definition ?></li>
<?php endforeach ?>
</ol>