nouvel algoritme plus rapide
This commit is contained in:
parent
2445bfcf54
commit
1416b8bd54
135
Grille.php
135
Grille.php
@ -8,127 +8,106 @@ class Grille {
|
|||||||
public $hauteur;
|
public $hauteur;
|
||||||
public $largeur;
|
public $largeur;
|
||||||
private $grilles;
|
private $grilles;
|
||||||
private $debuts;
|
private $lettres_suivantes;
|
||||||
|
private $positions;
|
||||||
|
private $nb_positions;
|
||||||
private $mots_utilises = [];
|
private $mots_utilises = [];
|
||||||
|
|
||||||
public function __construct($hauteur, $largeur, $id="") {
|
public function __construct($hauteur, $largeur, $id="") {
|
||||||
$this->hauteur = $hauteur;
|
$this->hauteur = $hauteur;
|
||||||
$this->largeur = $largeur;
|
$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 == "") {
|
if ($id == "") {
|
||||||
mt_srand();
|
mt_srand();
|
||||||
} else {
|
} else {
|
||||||
mt_srand(crc32($id));
|
mt_srand(crc32($id));
|
||||||
}
|
}
|
||||||
$this->debuts = [];
|
$this->lettres_suivantes = [];
|
||||||
foreach ($hauteur == $largeur? [$hauteur]: [$hauteur, $largeur] as $longueur) {
|
foreach ($hauteur == $largeur? [$hauteur]: [$hauteur, $largeur] as $longueur) {
|
||||||
$this->debuts[$longueur] = [];
|
$this->lettres_suivantes[$longueur] = [];
|
||||||
$nb_mots = 0;
|
|
||||||
foreach(mots_espaces($longueur) as $mot) {
|
foreach(mots_espaces($longueur) as $mot) {
|
||||||
for ($i = 0; $i <= $longueur; $i++) {
|
for ($i = 0; $i <= $longueur; $i++) {
|
||||||
$debut = substr($mot, 0, $i);
|
$debut = substr($mot, 0, $i);
|
||||||
if (!isset($this->debuts[$longueur][$debut])) {
|
if (!isset($this->lettres_suivantes[$longueur][$debut])) {
|
||||||
$this->debuts[$longueur][$debut] = [];
|
$this->lettres_suivantes[$longueur][$debut] = [];
|
||||||
}
|
}
|
||||||
$this->debuts[$longueur][$debut][] = $mot;
|
$this->lettres_suivantes[$longueur][$debut][substr($mot, $i, 1)] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mt_srand();
|
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();
|
$this->grilles = $this->generateur();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_ligne($l, $largeur) {
|
public function get_ligne($y, $largeur) {
|
||||||
$ligne = "";
|
$ligne = "";
|
||||||
for ($c = 0; $c < $largeur; $c++) {
|
for ($x = 0; $x < $largeur; $x++) {
|
||||||
$ligne .= $this->grille[$l][$c];
|
$ligne .= $this->grille[$y][$x];
|
||||||
}
|
}
|
||||||
return $ligne;
|
return $ligne;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set_ligne($l, $mot) {
|
public function get_colonne($x, $hauteur) {
|
||||||
for ($i = 0; $i < strlen($mot); $i++) {
|
|
||||||
$this->grille[$l][$i] = $mot[$i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_colonne($c, $hauteur) {
|
|
||||||
$colonne = "";
|
$colonne = "";
|
||||||
for ($l = 0; $l < $hauteur; $l++) {
|
for ($y = 0; $y < $hauteur; $y++) {
|
||||||
$colonne .= $this->grille[$l][$c];
|
$colonne .= $this->grille[$y][$x];
|
||||||
}
|
}
|
||||||
return $colonne;
|
return $colonne;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set_colonne($c, $mot) {
|
public function generateur($index=0) {
|
||||||
for ($i = 0; $i < strlen($mot); $i++) {
|
if ($index == $this->nb_positions) {
|
||||||
$this->grille[$i][$c] = $mot[$i];
|
yield $this;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function generateur() {
|
[$x, $y] = $this->positions[$index];
|
||||||
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) {
|
$lettres_possibles = array_intersect_assoc(
|
||||||
global $mots_de_n_lettres;
|
$this->lettres_suivantes[$this->largeur][$this->get_ligne($y, $x)],
|
||||||
$largeur = min($l, $this->largeur);
|
$this->lettres_suivantes[$this->hauteur][$this->get_colonne($x, $y)]
|
||||||
$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);
|
foreach ($lettres_possibles as $lettre => $_) {
|
||||||
$ok = true;
|
$this->grille[$y][$x] = $lettre;
|
||||||
for ($c = $l; $c < $this->largeur; $c++) {
|
|
||||||
if (!isset($this->debuts[$this->hauteur][$this->get_colonne($c, $hauteur)])) {
|
$mot_ligne = NULL;
|
||||||
$ok = false;
|
if ($x == $this->largeur - 1) {
|
||||||
break;
|
$mot_ligne = $this->get_ligne($y, $x);
|
||||||
|
if (isset($this->mots_utilises[$mot_ligne])) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
$this-> mots_utilises[$mot_ligne] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!$ok) {
|
$mot_colonne = NULL;
|
||||||
continue;
|
if ($y == $this->hauteur - 1) {
|
||||||
}
|
$mot_colonne = $this->get_colonne($x, $y);
|
||||||
$this->mots_utilises[$mot_lig] = true;
|
if (isset($this->mots_utilises[$mot_colonne])) {
|
||||||
if ($l < $this->largeur) {
|
continue;
|
||||||
yield from $this->trouve_une_colonne($l);
|
} else {
|
||||||
} else if ($l + 1 < $this->hauteur) {
|
$this-> mots_utilises[$mot_colonne] = true;
|
||||||
yield from $this->trouve_une_ligne($l + 1);
|
|
||||||
} else {
|
|
||||||
yield $this;
|
|
||||||
}
|
|
||||||
unset($this->mots_utilises[$mot_lig]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!$ok) {
|
|
||||||
continue;
|
yield from $this->generateur($index + 1);
|
||||||
|
|
||||||
|
if ($mot_ligne) {
|
||||||
|
unset($this-> mots_utilises[$mot_ligne]);
|
||||||
}
|
}
|
||||||
$this->mots_utilises[$mot_col] = true;
|
if ($mot_colonne) {
|
||||||
if ($c +1 < $this->hauteur) {
|
unset($this-> mots_utilises[$mot_colonne]);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
unset($this->mots_utilises[$mot_col]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
149
Grille0.php
Normal file
149
Grille0.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
2
dico.php
2
dico.php
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
|
|
||||||
const MIN_LETTRES_MOT_1 = 2;
|
const MIN_LETTRES_MOT_1 = 2;
|
||||||
const MIN_LETTRES_MOT_2 = 0;
|
const MIN_LETTRES_MOT_2 = 1;
|
||||||
// const MAX_MOTS = 100000;
|
// const MAX_MOTS = 100000;
|
||||||
|
|
||||||
|
|
||||||
|
26
index.php
26
index.php
@ -84,17 +84,17 @@ $grille->current();
|
|||||||
<table class="grille">
|
<table class="grille">
|
||||||
<tr>
|
<tr>
|
||||||
<th></th>
|
<th></th>
|
||||||
<?php for ($c = 0; $c < $largeur; $c++): ?>
|
<?php for ($x = 0; $x < $largeur; $x++): ?>
|
||||||
<th><?= chr($c + 65) ?></th>
|
<th><?= chr($x + 65) ?></th>
|
||||||
<?php endfor; ?>
|
<?php endfor; ?>
|
||||||
<th></th>
|
<th></th>
|
||||||
</tr>
|
</tr>
|
||||||
<?php for ($l = 0; $l < $hauteur; $l++): ?>
|
<?php for ($y = 0; $y < $hauteur; $y++): ?>
|
||||||
<tr>
|
<tr>
|
||||||
<th><?= $l + 1 ?></th>
|
<th><?= $y + 1 ?></th>
|
||||||
<?php for ($c = 0; $c < $largeur; $c++): ?>
|
<?php for ($x = 0; $x < $largeur; $x++): ?>
|
||||||
<td class="case <?= $grille->grille[$l][$c] == " " ? "noire" : "blanche" ?>">
|
<td class="case <?= $grille->grille[$y][$x] == " " ? "noire" : "blanche" ?>">
|
||||||
<?php if ($grille->grille[$l][$c] == " "): ?>
|
<?php if ($grille->grille[$y][$x] == " "): ?>
|
||||||
<input type="text" maxlength="1" size="1" value=" " disabled />
|
<input type="text" maxlength="1" size="1" value=" " disabled />
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<input type="text" maxlength="1" size="1" pattern="[A-Z]" />
|
<input type="text" maxlength="1" size="1" pattern="[A-Z]" />
|
||||||
@ -108,14 +108,14 @@ $grille->current();
|
|||||||
<div class="horizontales">
|
<div class="horizontales">
|
||||||
<h2>Horizontalement</h2>
|
<h2>Horizontalement</h2>
|
||||||
<ol>
|
<ol>
|
||||||
<?php for ($l = 0; $l < $hauteur; $l++): ?>
|
<?php for ($y = 0; $y < $hauteur; $y++): ?>
|
||||||
<li>
|
<li>
|
||||||
<?php $definitions = $dico[$grille->get_ligne($l, $largeur)] ?>
|
<?php $definitions = $dico[$grille->get_ligne($y, $largeur)] ?>
|
||||||
<?php if (count($definitions) == 1): ?>
|
<?php if (count($definitions) == 1): ?>
|
||||||
<?= $definitions[0] ?>
|
<?= $definitions[0] ?>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<ol>
|
<ol>
|
||||||
<?php foreach ($dico[$grille->get_ligne($l, $largeur)] as $definition) : ?>
|
<?php foreach ($dico[$grille->get_ligne($y, $largeur)] as $definition) : ?>
|
||||||
<li><?= $definition ?></li>
|
<li><?= $definition ?></li>
|
||||||
<?php endforeach ?>
|
<?php endforeach ?>
|
||||||
</ol>
|
</ol>
|
||||||
@ -127,14 +127,14 @@ $grille->current();
|
|||||||
<div class="verticales">
|
<div class="verticales">
|
||||||
<h2>Verticalement</h2>
|
<h2>Verticalement</h2>
|
||||||
<ol type="A">
|
<ol type="A">
|
||||||
<?php for ($c = 0; $c < $largeur; $c++): ?>
|
<?php for ($x = 0; $x < $largeur; $x++): ?>
|
||||||
<li>
|
<li>
|
||||||
<?php $definitions = $dico[$grille->get_colonne($c, $hauteur)] ?>
|
<?php $definitions = $dico[$grille->get_colonne($x, $hauteur)] ?>
|
||||||
<?php if (count($definitions) == 1): ?>
|
<?php if (count($definitions) == 1): ?>
|
||||||
<?= $definitions[0] ?>
|
<?= $definitions[0] ?>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<ol>
|
<ol>
|
||||||
<?php foreach ($dico[$grille->get_colonne($c, $hauteur)] as $definition) : ?>
|
<?php foreach ($dico[$grille->get_colonne($x, $hauteur)] as $definition) : ?>
|
||||||
<li><?= $definition ?></li>
|
<li><?= $definition ?></li>
|
||||||
<?php endforeach ?>
|
<?php endforeach ?>
|
||||||
</ol>
|
</ol>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user