web-dev-qa-db-fra.com

Comment trouver la liste des mots possibles d'une matrice de lettres [Solveur Boggle]

Dernièrement, je joue à un jeu sur mon iPhone appelé Scramble. Certains d’entre vous connaissent peut-être ce jeu sous le nom de Boggle. Au début du jeu, vous obtenez une matrice de lettres comme celle-ci:

F X I E
A M L O
E W B X
A S T U

Le but du jeu est de trouver autant de mots que possible pouvant être formés en enchaînant des lettres. Vous pouvez commencer par n’importe quelle lettre et toutes les lettres qui l’entourent sont acceptables, puis une fois que vous passez à la lettre suivante, toutes les lettres l’entourant sont acceptables, sauf les lettres déjà utilisées . Ainsi, dans la grille ci-dessus, par exemple, je pourrais trouver les mots LOB, Tux, SEA, FAME, etc. Les mots doivent comporter au moins 3 caractères, et pas plus de NxN, qui seraient 16 dans ce jeu varient dans certaines mises en œuvre. Bien que ce jeu soit amusant et addictif, je ne suis apparemment pas très bon et je voulais tricher un peu en créant un programme qui me donnerait les meilleurs mots possibles (plus le mot est long, plus vous gagnez de points).

_ { Sample Boggle http://www.boggled.org/sample.gif } _

Malheureusement, je ne suis pas très bon avec les algorithmes ou leur efficacité, etc. Ma première tentative utilise un dictionnaire comme celui-ci _ (~ 2,3 Mo) et effectue une recherche linéaire en essayant de faire correspondre les combinaisons avec les entrées du dictionnaire. Cela prend très beaucoup de temps pour trouver les mots possibles et, comme vous ne disposez que de 2 minutes par tour, ce n'est tout simplement pas adéquat.

Je suis intéressé de voir si Stackoverflowers peut proposer des solutions plus efficaces. Je recherche principalement des solutions utilisant les Big 3 P: Python, PHP et Perl, même si tout ce qui concerne Java ou C++ est cool aussi, car la rapidité est essentielle.

SOLUTIONS ACTUELLES:

  • Adam Rosenfield, Python, ~ 20 ans 
  • John Fouhy, Python, ~ 3s 
  • Kent Fredric, Perl, ~ 1s 
  • Darius Bacon, Python, ~ 1s 
  • rvarcher, VB.NET (lien direct) }, ~ 1s
  • Paolo Bergantino, PHP (lien direct) }, ~ 5s (~ 2s localement)

BOUNTY:

J'ajoute une prime à cette question pour remercier toutes les personnes qui ont participé à leurs programmes. Malheureusement, je ne peux que donner la réponse acceptée à l'un d'entre vous. Je vais donc déterminer qui a le plus rapide des solutions de boggle dans 7 jours et attribuer la prime au gagnant.

Bounty attribué. Merci à tous ceux qui ont participé.

372
Paolo Bergantino

Ma réponse fonctionne comme les autres ici, mais je vais la publier car elle paraît un peu plus rapide que les autres solutions Python, car la configuration du dictionnaire est plus rapide. (J'ai vérifié cela par rapport à la solution de John Fouhy.) Après la configuration, le temps de résolution est écoulé.

grid = "fxie amlo ewbx astu".split()
nrows, ncols = len(grid), len(grid[0])

# A dictionary Word that could be a solution must use only the grid's
# letters and have length >= 3. (With a case-insensitive match.)
import re
alphabet = ''.join(set(''.join(grid)))
bogglable = re.compile('[' + alphabet + ']{3,}$', re.I).match

words = set(Word.rstrip('\n') for Word in open('words') if bogglable(Word))
prefixes = set(Word[:i] for Word in words
               for i in range(2, len(Word)+1))

def solve():
    for y, row in enumerate(grid):
        for x, letter in enumerate(row):
            for result in extending(letter, ((x, y),)):
                yield result

def extending(prefix, path):
    if prefix in words:
        yield (prefix, path)
    for (nx, ny) in neighbors(path[-1]):
        if (nx, ny) not in path:
            prefix1 = prefix + grid[ny][nx]
            if prefix1 in prefixes:
                for result in extending(prefix1, path + ((nx, ny),)):
                    yield result

def neighbors((x, y)):
    for nx in range(max(0, x-1), min(x+2, ncols)):
        for ny in range(max(0, y-1), min(y+2, nrows)):
            yield (nx, ny)

Exemple d'utilisation:

# Print a maximal-length Word and its path:
print max(solve(), key=lambda (Word, path): len(Word))

Modifier: Filtrer les mots de moins de 3 lettres.

Edit 2: J'étais curieux de savoir pourquoi la solution Perl de Kent Fredric était plus rapide; il s'avère que vous utilisez une correspondance d'expression régulière au lieu d'un ensemble de caractères. Faire la même chose en Python double la vitesse.

140
Darius Bacon

La solution la plus rapide que vous obtiendrez impliquera probablement de stocker votre dictionnaire dans un répertoire trie . Ensuite, créez une file d'attente de triplets (x, y, s), où chaque élément de la file d'attente correspond à un préfixe s d'un mot qui peut être orthographié dans la grille, se terminant à l'emplacement (x, y). Initialisez la file d’attente avec les éléments N x N (où N est la taille de votre grille), un élément pour chaque carré du la grille. Ensuite, l'algorithme procède comme suit:

 Tant que la file n’est pas vide: 
 Retirer un triple (x, y, s) 
 Pour chaque carré (x ', y') avec la lettre c adjacente à (x, y): 
 Si s + c est un mot, affichez s + c 
 Si s + c est le préfixe d'un mot, insérez (x ', y', s + c) dans la file d'attente 

Si vous stockez votre dictionnaire dans un fichier, testez si s + c _ est un mot ou si le préfixe d'un mot peut être effectué à temps constant (à condition de conserver des métadonnées supplémentaires dans chaque donnée de la file d'attente, telle qu'un pointeur sur le noeud actuel du trie), le temps d'exécution de cet algorithme est donc O (nombre de mots pouvant être épelés).

[Edit] Voici une implémentation en Python que je viens de coder:

#!/usr/bin/python

class TrieNode:
    def __init__(self, parent, value):
        self.parent = parent
        self.children = [None] * 26
        self.isWord = False
        if parent is not None:
            parent.children[ord(value) - 97] = self

def MakeTrie(dictfile):
    dict = open(dictfile)
    root = TrieNode(None, '')
    for Word in dict:
        curNode = root
        for letter in Word.lower():
            if 97 <= ord(letter) < 123:
                nextNode = curNode.children[ord(letter) - 97]
                if nextNode is None:
                    nextNode = TrieNode(curNode, letter)
                curNode = nextNode
        curNode.isWord = True
    return root

def BoggleWords(grid, dict):
    rows = len(grid)
    cols = len(grid[0])
    queue = []
    words = []
    for y in range(cols):
        for x in range(rows):
            c = grid[y][x]
            node = dict.children[ord(c) - 97]
            if node is not None:
                queue.append((x, y, c, node))
    while queue:
        x, y, s, node = queue[0]
        del queue[0]
        for dx, dy in ((1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1)):
            x2, y2 = x + dx, y + dy
            if 0 <= x2 < cols and 0 <= y2 < rows:
                s2 = s + grid[y2][x2]
                node2 = node.children[ord(grid[y2][x2]) - 97]
                if node2 is not None:
                    if node2.isWord:
                        words.append(s2)
                    queue.append((x2, y2, s2, node2))

    return words

Exemple d'utilisation:

d = MakeTrie('/usr/share/dict/words')
print(BoggleWords(['fxie','amlo','ewbx','astu'], d))

Sortie:

['fa', 'xi', 'ie', 'io', 'el', 'am', 'ax', 'ae', 'aw', 'mi', 'ma', 'moi', ' lo ',' li ',' oe ',' boeuf ',' em ',' ea ',' ea ',' es ',' wa ',' nous ',' wa ',' bo ',' bu ' , 'comme', 'aw', 'ae', 'st', 'se', 'sa', 'tu', 'ut', 'fam', 'fae', 'imi', 'eli', ' Elm ',' elb ',' AMI ',' AMA ',' ame ',' aes ',' awl ',' awa ',' awe ',' awa ',' mix ',' mim ',' mil ' , 'mam', 'max', 'mae', 'maw', 'mew', 'mem', 'mes', 'lob', 'lox', 'lei', 'leo', 'lie', ' lim ',' huile ',' olm ',' ewe ',' eme ',' cire ',' waf ',' wae ',' waw ',' wem ',' wea ',' wea ',' était ' , 'waw', 'wae', 'bob', 'blo', 'bub', 'mais', 'ast', 'ase', 'asa', 'awl', 'awa', 'effrayé', ' awa ',' aes ',' swa ',' swa ',' coudre ',' mer ',' mer ',' saw ',' tux ',' tub ',' tut ',' twa ',' twa ' , 'tst', 'utu', 'fama', 'fame', 'ixil', 'imam', 'amli', 'amil', 'ambo', 'axil', 'axe', 'mimi', ' mima ',' mime ',' milo ',' mile ',' mewl ',' mese ',' mesa ',' lolo ',' lobo ',' lima ',' lime ',' membre ',' lile ' , 'oime', 'oleo', 'olio', 'hautbois', 'obol', 'emim', 'emil', 'est', 'aisance', 'wame', 'wawa', 'wawa', ' Weam ',' ouest ',' wese ',' wast ',' wase ' , 'wawa', 'wawa', 'bouillir', 'bolo', 'bole', 'bobo', 'blob', 'bleo', 'bubo', 'asem', 'stub', 'stut', ' nagé ',' semi ',' seme ',' couture ',' seax ',' sasa ',' sawt ',' tutu ',' tuts ',' twae ',' twas ',' twae ',' ilima ' , 'amble', 'axile', 'awest', 'mamie', 'mambo', 'maxim', 'mease', 'mesem', 'limax', 'limes', 'limbo', 'limbu', ' obole ',' emesa ',' embox ',' awest ',' swami ',' famble ',' mimble ',' maxima ',' embolo ',' embole ',' wamble ',' semese ',' semble ' , 'sawbwa', 'sawbwa']

Remarques: Ce programme ne génère pas de mots d'une lettre, ni ne filtre par longueur de mot. C'est facile à ajouter mais pas vraiment pertinent au problème. Il génère également plusieurs mots à plusieurs reprises s’ils peuvent être épelés de différentes manières. Si un mot donné peut être orthographié de différentes manières (dans le pire des cas: chaque lettre de la grille est identique (par exemple, «A») et qu'un mot du type «aaaaaaaaaa» figure dans votre dictionnaire), le temps d'exécution sera terriblement exponentiel. . Le filtrage des doublons et le tri sont faciles à effectuer une fois l'algorithme terminé.

116
Adam Rosenfield

Pour accélérer le traitement des dictionnaires, vous pouvez effectuer une transformation/un processus général pour réduire considérablement les comparaisons de dictionnaires à l’avance.

Étant donné que la grille ci-dessus ne contient que 16 caractères, dont certains en double, vous pouvez réduire considérablement le nombre total de clés dans votre dictionnaire en filtrant simplement les entrées contenant des caractères inaccessibles.

Je pensais que c'était l'optimisation évidente mais vu que personne ne l'a fait, je le mentionne.

Cela m'a réduit d'un dictionnaire de 200 000 clés à seulement 2 000 clés simplement pendant la passe de saisie. Cela réduit au minimum les frais de mémoire, et il est certain que la vitesse augmentera quelque part, car la mémoire n’est pas infiniment rapide.

Mise en oeuvre de Perl

Mon implémentation est un peu lourde en raison de mon importance pour pouvoir connaître le chemin exact de chaque chaîne extraite, et pas seulement sa validité.

J'ai également quelques adaptations qui permettraient théoriquement de faire fonctionner une grille avec des trous et des lignes de tailles différentes (en supposant que vous obteniez une entrée correcte et que celle-ci s'aligne d'une manière ou d'une autre).

Le premier filtre est de loin le goulot d'étranglement le plus important de mon application, comme on le soupçonnait plus tôt, en commentant que la ligne le gonfle de 1,5 à 7,5 s.

Lors de l'exécution, il semble penser que tous les chiffres simples sont sur leurs propres mots valides, mais je suis à peu près sûr que cela est dû au fonctionnement du fichier de dictionnaire.

C'est un peu bouffi, mais au moins je réutilise Tree :: Trie de cpan

Une partie de celle-ci a été inspirée en partie par les implémentations existantes, une partie que j'avais déjà à l'esprit.

La critique constructive et les moyens de l'améliorer sont les bienvenues (je note qu'il n'a jamais recherché sur CPAN pour trouver un solutionneur de trouble , mais c'était plus amusant de travailler)

mis à jour pour les nouveaux critères

#!/usr/bin/Perl 

use strict;
use warnings;

{

  # this package manages a given path through the grid.
  # Its an array of matrix-nodes in-order with
  # Convenience functions for pretty-printing the paths
  # and for extending paths as new paths.

  # Usage:
  # my $p = Prefix->new(path=>[ $startnode ]);
  # my $c = $p->child( $extensionNode );
  # print $c->current_Word ;

  package Prefix;
  use Moose;

  has path => (
      isa     => 'ArrayRef[MatrixNode]',
      is      => 'rw',
      default => sub { [] },
  );
  has current_Word => (
      isa        => 'Str',
      is         => 'rw',
      lazy_build => 1,
  );

  # Create a clone of this object
  # with a longer path

  # $o->child( $successive-node-on-graph );

  sub child {
      my $self    = shift;
      my $newNode = shift;
      my $f       = Prefix->new();

      # Have to do this manually or other recorded paths get modified
      Push @{ $f->{path} }, @{ $self->{path} }, $newNode;
      return $f;
  }

  # Traverses $o->path left-to-right to get the string it represents.

  sub _build_current_Word {
      my $self = shift;
      return join q{}, map { $_->{value} } @{ $self->{path} };
  }

  # Returns  the rightmost node on this path

  sub tail {
      my $self = shift;
      return $self->{path}->[-1];
  }

  # pretty-format $o->path

  sub pp_path {
      my $self = shift;
      my @path =
        map { '[' . $_->{x_position} . ',' . $_->{y_position} . ']' }
        @{ $self->{path} };
      return "[" . join( ",", @path ) . "]";
  }

  # pretty-format $o
  sub pp {
      my $self = shift;
      return $self->current_Word . ' => ' . $self->pp_path;
  }

  __PACKAGE__->meta->make_immutable;
}

{

  # Basic package for tracking node data
  # without having to look on the grid.
  # I could have just used an array or a hash, but that got ugly.

# Once the matrix is up and running it doesn't really care so much about rows/columns,
# Its just a sea of points and each point has adjacent points.
# Relative positioning is only really useful to map it back to userspace

  package MatrixNode;
  use Moose;

  has x_position => ( isa => 'Int', is => 'rw', required => 1 );
  has y_position => ( isa => 'Int', is => 'rw', required => 1 );
  has value      => ( isa => 'Str', is => 'rw', required => 1 );
  has siblings   => (
      isa     => 'ArrayRef[MatrixNode]',
      is      => 'rw',
      default => sub { [] }
  );

# Its not implicitly uni-directional joins. It would be more effient in therory
# to make the link go both ways at the same time, but thats too hard to program around.
# and besides, this isn't slow enough to bother caring about.

  sub add_sibling {
      my $self    = shift;
      my $sibling = shift;
      Push @{ $self->siblings }, $sibling;
  }

  # Convenience method to derive a path starting at this node

  sub to_path {
      my $self = shift;
      return Prefix->new( path => [$self] );
  }
  __PACKAGE__->meta->make_immutable;

}

{

  package Matrix;
  use Moose;

  has rows => (
      isa     => 'ArrayRef',
      is      => 'rw',
      default => sub { [] },
  );

  has regex => (
      isa        => 'Regexp',
      is         => 'rw',
      lazy_build => 1,
  );

  has cells => (
      isa        => 'ArrayRef',
      is         => 'rw',
      lazy_build => 1,
  );

  sub add_row {
      my $self = shift;
      Push @{ $self->rows }, [@_];
  }

  # Most of these functions from here down are just builder functions,
  # or utilities to help build things.
  # Some just broken out to make it easier for me to process.
  # All thats really useful is add_row
  # The rest will generally be computed, stored, and ready to go
  # from ->cells by the time either ->cells or ->regex are called.

  # traverse all cells and make a regex that covers them.
  sub _build_regex {
      my $self  = shift;
      my $chars = q{};
      for my $cell ( @{ $self->cells } ) {
          $chars .= $cell->value();
      }
      $chars = "[^$chars]";
      return qr/$chars/i;
  }

  # convert a plain cell ( ie: [x][y] = 0 )
  # to an intelligent cell ie: [x][y] = object( x, y )
  # we only really keep them in this format temporarily
  # so we can go through and tie in neighbouring information.
  # after the neigbouring is done, the grid should be considered inoperative.

  sub _convert {
      my $self = shift;
      my $x    = shift;
      my $y    = shift;
      my $v    = $self->_read( $x, $y );
      my $n    = MatrixNode->new(
          x_position => $x,
          y_position => $y,
          value      => $v,
      );
      $self->_write( $x, $y, $n );
      return $n;
  }

# go through the rows/collums presently available and freeze them into objects.

  sub _build_cells {
      my $self = shift;
      my @out  = ();
      my @rows = @{ $self->{rows} };
      for my $x ( 0 .. $#rows ) {
          next unless defined $self->{rows}->[$x];
          my @col = @{ $self->{rows}->[$x] };
          for my $y ( 0 .. $#col ) {
              next unless defined $self->{rows}->[$x]->[$y];
              Push @out, $self->_convert( $x, $y );
          }
      }
      for my $c (@out) {
          for my $n ( $self->_neighbours( $c->x_position, $c->y_position ) ) {
              $c->add_sibling( $self->{rows}->[ $n->[0] ]->[ $n->[1] ] );
          }
      }
      return \@out;
  }

  # given x,y , return array of points that refer to valid neighbours.
  sub _neighbours {
      my $self = shift;
      my $x    = shift;
      my $y    = shift;
      my @out  = ();
      for my $sx ( -1, 0, 1 ) {
          next if $sx + $x < 0;
          next if not defined $self->{rows}->[ $sx + $x ];
          for my $sy ( -1, 0, 1 ) {
              next if $sx == 0 && $sy == 0;
              next if $sy + $y < 0;
              next if not defined $self->{rows}->[ $sx + $x ]->[ $sy + $y ];
              Push @out, [ $sx + $x, $sy + $y ];
          }
      }
      return @out;
  }

  sub _has_row {
      my $self = shift;
      my $x    = shift;
      return defined $self->{rows}->[$x];
  }

  sub _has_cell {
      my $self = shift;
      my $x    = shift;
      my $y    = shift;
      return defined $self->{rows}->[$x]->[$y];
  }

  sub _read {
      my $self = shift;
      my $x    = shift;
      my $y    = shift;
      return $self->{rows}->[$x]->[$y];
  }

  sub _write {
      my $self = shift;
      my $x    = shift;
      my $y    = shift;
      my $v    = shift;
      $self->{rows}->[$x]->[$y] = $v;
      return $v;
  }

  __PACKAGE__->meta->make_immutable;
}

use Tree::Trie;

sub readDict {
  my $fn = shift;
  my $re = shift;
  my $d  = Tree::Trie->new();

  # Dictionary Loading
  open my $fh, '<', $fn;
  while ( my $line = <$fh> ) {
      chomp($line);

 # Commenting the next line makes it go from 1.5 seconds to 7.5 seconds. EPIC.
      next if $line =~ $re;    # Early Filter
      $d->add( uc($line) );
  }
  return $d;
}

sub traverseGraph {
  my $d     = shift;
  my $m     = shift;
  my $min   = shift;
  my $max   = shift;
  my @words = ();

  # Inject all grid nodes into the processing queue.

  my @queue =
    grep { $d->lookup( $_->current_Word ) }
    map  { $_->to_path } @{ $m->cells };

  while (@queue) {
      my $item = shift @queue;

      # put the dictionary into "exact match" mode.

      $d->deepsearch('exact');

      my $cword = $item->current_Word;
      my $l     = length($cword);

      if ( $l >= $min && $d->lookup($cword) ) {
          Push @words,
            $item;    # Push current path into "words" if it exactly matches.
      }
      next if $l > $max;

      # put the dictionary into "is-a-prefix" mode.
      $d->deepsearch('boolean');

    siblingloop: foreach my $sibling ( @{ $item->tail->siblings } ) {
          foreach my $visited ( @{ $item->{path} } ) {
              next siblingloop if $sibling == $visited;
          }

          # given path y , iterate for all its end points
          my $subpath = $item->child($sibling);

          # create a new path for each end-point
          if ( $d->lookup( $subpath->current_Word ) ) {

             # if the new path is a prefix, add it to the bottom of the queue.
              Push @queue, $subpath;
          }
      }
  }
  return \@words;
}

sub setup_predetermined { 
  my $m = shift; 
  my $gameNo = shift;
  if( $gameNo == 0 ){
      $m->add_row(qw( F X I E ));
      $m->add_row(qw( A M L O ));
      $m->add_row(qw( E W B X ));
      $m->add_row(qw( A S T U ));
      return $m;
  }
  if( $gameNo == 1 ){
      $m->add_row(qw( D G H I ));
      $m->add_row(qw( K L P S ));
      $m->add_row(qw( Y E U T ));
      $m->add_row(qw( E O R N ));
      return $m;
  }
}
sub setup_random { 
  my $m = shift; 
  my $seed = shift;
  srand $seed;
  my @letters = 'A' .. 'Z' ; 
  for( 1 .. 4 ){ 
      my @r = ();
      for( 1 .. 4 ){
          Push @r , $letters[int(Rand(25))];
      }
      $m->add_row( @r );
  }
}

# Here is where the real work starts.

my $m = Matrix->new();
setup_predetermined( $m, 0 );
#setup_random( $m, 5 );

my $d = readDict( 'dict.txt', $m->regex );
my $c = scalar @{ $m->cells };    # get the max, as per spec

print join ",\n", map { $_->pp } @{
  traverseGraph( $d, $m, 3, $c ) ;
};

Informations Arch/exécution pour comparaison:

model name      : Intel(R) Core(TM)2 Duo CPU     T9300  @ 2.50GHz
cache size      : 6144 KB
Memory usage summary: heap total: 77057577, heap peak: 11446200, stack peak: 26448
       total calls   total memory   failed calls
 malloc|     947212       68763684              0
realloc|      11191        1045641              0  (nomove:9063, dec:4731, free:0)
 calloc|     121001        7248252              0
   free|     973159       65854762

Histogram for block sizes:
  0-15         392633  36% ==================================================
 16-31          43530   4% =====
 32-47          50048   4% ======
 48-63          70701   6% =========
 64-79          18831   1% ==
 80-95          19271   1% ==
 96-111        238398  22% ==============================
112-127          3007  <1% 
128-143        236727  21% ==============================

Plus de rumeurs sur l'optimisation de Regex

L’optimisation des expressions rationnelles que j’utilise est inutile pour les dictionnaires à résolutions multiples, et vous aurez besoin d’un dictionnaire complet, et non d’un dictionnaire pré-coupé.

Cependant, cela dit, pour des solutions ponctuelles, c'est vraiment rapide. (Les expressions rationnelles Perl sont en C! :))

Voici quelques ajouts de code variables:

sub readDict_nofilter {
  my $fn = shift;
  my $re = shift;
  my $d  = Tree::Trie->new();

  # Dictionary Loading
  open my $fh, '<', $fn;
  while ( my $line = <$fh> ) {
      chomp($line);
      $d->add( uc($line) );
  }
  return $d;
}

sub benchmark_io { 
  use Benchmark qw( cmpthese :hireswallclock );
   # generate a random 16 character string 
   # to simulate there being an input grid. 
  my $regexen = sub { 
      my @letters = 'A' .. 'Z' ; 
      my @lo = ();
      for( 1..16 ){ 
          Push @lo , $_ ; 
      }
      my $c  = join '', @lo;
      $c = "[^$c]";
      return qr/$c/i;
  };
  cmpthese( 200 , { 
      filtered => sub { 
          readDict('dict.txt', $regexen->() );
      }, 
      unfiltered => sub {
          readDict_nofilter('dict.txt');
      }
  });
}
 filtré non filtré 
 non filtré 8.16 - -94% 
 filtré 0.464 1658% - 

ps: 8,16 * 200 = 27 minutes.

39
Kent Fredric

Vous pouvez diviser le problème en deux parties:

  1. Un type d'algorithme de recherche qui énumère les chaînes possibles dans la grille.
  2. Une façon de tester si une chaîne est un mot valide.

Idéalement, (2) devrait également inclure un moyen de tester si une chaîne est le préfixe d'un mot valide - cela vous permettra d'élaguer votre recherche et de gagner beaucoup de temps.

Trie d'Adam Rosenfield est une solution à (2). Il est élégant et probablement ce que votre spécialiste en algorithmes préférerait, mais avec les langages modernes et les ordinateurs modernes, nous pouvons être un peu plus paresseux. En outre, comme le suggère Kent, nous pouvons réduire la taille de notre dictionnaire en supprimant les mots dont les lettres ne sont pas présentes dans la grille. Voici du python:

def make_lookups(grid, fn='dict.txt'):
    # Make set of valid characters.
    chars = set()
    for Word in grid:
        chars.update(Word)

    words = set(x.strip() for x in open(fn) if set(x.strip()) <= chars)
    prefixes = set()
    for w in words:
        for i in range(len(w)+1):
            prefixes.add(w[:i])

    return words, prefixes

Sensationnel; test de préfixe à temps constant. Il faut quelques secondes pour charger le dictionnaire que vous avez lié, mais seulement deux :-) (remarquez que words <= prefixes)

Maintenant, pour la partie (1), je suis enclin à penser en termes de graphiques. Je vais donc construire un dictionnaire qui ressemble à ceci:

graph = { (x, y):set([(x0,y0), (x1,y1), (x2,y2)]), }

c'est-à-dire graph[(x, y)] est l'ensemble des coordonnées que vous pouvez atteindre à partir de la position (x, y). J'ajouterai également un noeud factice None qui se connectera à tout.

Construire c'est un peu maladroit, car il y a 8 positions possibles et vous devez vérifier les limites. Voici un code python assez maladroit:

def make_graph(grid):
    root = None
    graph = { root:set() }
    chardict = { root:'' }

    for i, row in enumerate(grid):
        for j, char in enumerate(row):
            chardict[(i, j)] = char
            node = (i, j)
            children = set()
            graph[node] = children
            graph[root].add(node)
            add_children(node, children, grid)

    return graph, chardict

def add_children(node, children, grid):
    x0, y0 = node
    for i in [-1,0,1]:
        x = x0 + i
        if not (0 <= x < len(grid)):
            continue
        for j in [-1,0,1]:
            y = y0 + j
            if not (0 <= y < len(grid[0])) or (i == j == 0):
                continue

            children.add((x,y))

Ce code construit également un dictionnaire mappant (x,y) au caractère correspondant. Cela me permet de transformer une liste de positions en un mot:

def to_Word(chardict, pos_list):
    return ''.join(chardict[x] for x in pos_list)

Enfin, nous effectuons une recherche en profondeur. La procédure de base est la suivante:

  1. La recherche arrive à un noeud particulier.
  2. Vérifiez si le chemin jusqu’à présent pourrait faire partie d’un mot. Sinon, ne explorez plus cette branche.
  3. Vérifiez si le chemin jusqu'à présent est un mot. Si tel est le cas, ajoutez-le à la liste des résultats.
  4. Explorez tous les enfants ne faisant pas partie du chemin jusqu'à présent.

Python:

def find_words(graph, chardict, position, prefix, results, words, prefixes):
    """ Arguments:
      graph :: mapping (x,y) to set of reachable positions
      chardict :: mapping (x,y) to character
      position :: current position (x,y) -- equals prefix[-1]
      prefix :: list of positions in current string
      results :: set of words found
      words :: set of valid words in the dictionary
      prefixes :: set of valid words or prefixes thereof
    """
    Word = to_Word(chardict, prefix)

    if Word not in prefixes:
        return

    if Word in words:
        results.add(Word)

    for child in graph[position]:
        if child not in prefix:
            find_words(graph, chardict, child, prefix+[child], results, words, prefixes)

Exécutez le code en tant que:

grid = ['fxie', 'amlo', 'ewbx', 'astu']
g, c = make_graph(grid)
w, p = make_lookups(grid)
res = set()
find_words(g, c, None, [], res, w, p)

et inspectez res pour voir les réponses. Voici une liste de mots trouvés pour votre exemple, triés par taille:

 ['a', 'b', 'e', 'f', 'i', 'l', 'm', 'o', 's', 't',
 'u', 'w', 'x', 'ae', 'am', 'as', 'aw', 'ax', 'bo',
 'bu', 'ea', 'el', 'em', 'es', 'fa', 'ie', 'io', 'li',
 'lo', 'ma', 'me', 'mi', 'oe', 'ox', 'sa', 'se', 'st',
 'tu', 'ut', 'wa', 'we', 'xi', 'aes', 'ame', 'AMI',
 'ase', 'ast', 'awa', 'awe', 'awl', 'blo', 'but', 'elb',
 'Elm', 'fae', 'fam', 'lei', 'lie', 'lim', 'lob', 'lox',
 'mae', 'maw', 'mew', 'mil', 'mix', 'oil', 'olm', 'saw',
 'sea', 'sew', 'swa', 'tub', 'Tux', 'twa', 'wae', 'was',
 'wax', 'wem', 'ambo', 'amil', 'amli', 'asem', 'axil',
 'axle', 'bleo', 'boil', 'bole', 'east', 'fame', 'limb',
 'Lime', 'mesa', 'mewl', 'mile', 'milo', 'oime', 'sawt',
 'seam', 'seax', 'semi', 'stub', 'swam', 'twae', 'twas',
 'wame', 'wase', 'wast', 'weam', 'west', 'amble', 'awest',
 'axile', 'embox', 'limbo', 'limes', 'swami', 'embole',
 'famble', 'semble', 'wamble']

Le code prend (littéralement) quelques secondes pour charger le dictionnaire, mais le reste est instantané sur ma machine.

33
John Fouhy

Je pense que vous passerez probablement le plus clair de votre temps à essayer de faire correspondre des mots qui ne peuvent pas être construits avec votre grille de lettres. Donc, la première chose que je ferais serait d’essayer d’accélérer cette étape et cela devrait vous permettre de vous rendre la majeure partie du chemin.

Pour cela, je reformulerais la grille sous forme de tableau des "mouvements" possibles que vous indexez en fonction de la transition de lettre que vous examinez.

Commencez par attribuer à chaque lettre un numéro de votre alphabet entier (A = 0, B = 1, C = 2, etc.).

Prenons cet exemple:

h b c d
e e g h
l l k l
m o f p

Et pour l'instant, utilisons l'alphabet des lettres que nous avons (en général, vous voudrez probablement utiliser le même alphabet entier à chaque fois):

 b | c | d | e | f | g | h | k | l | m |  o |  p
---+---+---+---+---+---+---+---+---+---+----+----
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11

Ensuite, vous créez un tableau booléen 2D qui indique si vous avez une certaine transition de lettre disponible:

     |  0  1  2  3  4  5  6  7  8  9 10 11  <- from letter
     |  b  c  d  e  f  g  h  k  l  m  o  p
-----+--------------------------------------
 0 b |     T     T     T  T     
 1 c |  T     T  T     T  T
 2 d |     T           T  T
 3 e |  T  T     T     T  T  T  T
 4 f |                       T  T     T  T
 5 g |  T  T  T  T        T  T  T
 6 h |  T  T  T  T     T     T  T
 7 k |           T  T  T  T     T     T  T
 8 l |           T  T  T  T  T  T  T  T  T
 9 m |                          T     T
10 o |              T        T  T  T
11 p |              T        T  T
 ^
 to letter

Maintenant, parcourez votre liste de mots et convertissez les mots en transitions:

hello (6, 3, 8, 8, 10):
6 -> 3, 3 -> 8, 8 -> 8, 8 -> 10

Ensuite, vérifiez si ces transitions sont autorisées en les recherchant dans votre tableau:

[6][ 3] : T
[3][ 8] : T
[8][ 8] : T
[8][10] : T

S'ils sont tous autorisés, il y a une chance que cette Parole soit trouvée.

Par exemple, le mot "casque" peut être exclu lors de la 4ème transition (m à e: helMEt), car cette entrée dans votre table est fausse.

Et le mot hamster peut être exclu, car la première transition (h à a) n’est pas autorisée (n’existe même pas dans votre table).

Maintenant, pour les très peu de mots restants probablement que vous n’avez pas éliminés, essayez de les trouver dans la grille de la façon dont vous le faites maintenant ou comme suggéré dans certaines des autres réponses ici. Ceci permet d'éviter les faux positifs résultant de sauts entre des lettres identiques dans votre grille. Par exemple, le mot "aide" est autorisé par la table, mais pas par la grille.

Quelques conseils supplémentaires pour améliorer les performances de cette idée:

  1. Au lieu d'utiliser un tableau 2D, utilisez un tableau 1D et calculez vous-même l'indice de la deuxième lettre. Donc, au lieu d'un tableau 12x12 comme ci-dessus, créez un tableau 1D de longueur 144. Si vous utilisez ensuite toujours le même alphabet (c'est-à-dire un tableau 26x26 = 676x1 pour l'alphabet anglais standard), même si toutes les lettres ne s'affichent pas dans votre grille , vous pouvez pré-calculer les indices dans ce tableau 1D que vous devez tester pour faire correspondre les mots de votre dictionnaire. Par exemple, les indices pour "Bonjour" dans l'exemple ci-dessus seraient

    hello (6, 3, 8, 8, 10):
    42 (from 6 + 3x12), 99, 104, 128
    -> "hello" will be stored as 42, 99, 104, 128 in the dictionary
    
  2. Étendez l’idée sur une table 3D (exprimée sous forme de tableau 1D), c’est-à-dire toutes les combinaisons autorisées de 3 lettres. De cette façon, vous pouvez éliminer encore plus de mots immédiatement et réduire le nombre de recherches de tableaux pour chaque mot de 1: pour "hello", vous avez uniquement besoin de 3 recherches de tableaux: hel, ell, llo. Soit dit en passant, la construction de cette table sera très rapide car il n’ya que 400 déplacements possibles de 3 lettres dans votre grille.

  3. Pré-calculez les indices des déplacements dans votre grille que vous devez inclure dans votre tableau. Pour l'exemple ci-dessus, vous devez définir les entrées suivantes sur 'True':

    (0,0) (0,1) -> here: h, b : [6][0]
    (0,0) (1,0) -> here: h, e : [6][3]
    (0,0) (1,1) -> here: h, e : [6][3]
    (0,1) (0,0) -> here: b, h : [0][6]
    (0,1) (0,2) -> here: b, c : [0][1]
    .
    :
    
  4. Représentez également votre grille de jeu dans un tableau 1-D avec 16 entrées et faites pré-calculer le tableau en 3. contient les indices dans ce tableau.

Je suis sûr que si vous utilisez cette approche, vous pouvez obtenir un code incroyablement rapide si le dictionnaire est pré-calculé et déjà chargé en mémoire.

BTW: Une autre bonne chose à faire, si vous construisez un jeu, est d'exécuter ce genre de choses immédiatement en arrière-plan. Commencez à générer et à résoudre le premier jeu pendant que l'utilisateur regarde toujours l'écran de titre de votre application et qu'il met son doigt en position d'appuyer sur "Play". Ensuite, générez et résolvez le prochain jeu pendant que l'utilisateur joue le précédent. Cela devrait vous donner beaucoup de temps pour exécuter votre code.

(J'aime ce problème, alors je serai probablement tenté d'implémenter ma proposition dans Java dans les prochains jours pour voir comment cela fonctionnerait réellement ... Je posterai le code ici dès que je le ferai. .)

UPDATE:

Ok, j'ai eu un peu de temps aujourd'hui et j'ai implémenté cette idée en Java:

class DictionaryEntry {
  public int[] letters;
  public int[] triplets;
}

class BoggleSolver {

  // Constants
  final int ALPHABET_SIZE = 5;  // up to 2^5 = 32 letters
  final int BOARD_SIZE    = 4;  // 4x4 board
  final int[] moves = {-BOARD_SIZE-1, -BOARD_SIZE, -BOARD_SIZE+1, 
                                  -1,                         +1,
                       +BOARD_SIZE-1, +BOARD_SIZE, +BOARD_SIZE+1};


  // Technically constant (calculated here for flexibility, but should be fixed)
  DictionaryEntry[] dictionary; // Processed Word list
  int maxWordLength = 0;
  int[] boardTripletIndices; // List of all 3-letter moves in board coordinates

  DictionaryEntry[] buildDictionary(String fileName) throws IOException {
    BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
    String Word = fileReader.readLine();
    ArrayList<DictionaryEntry> result = new ArrayList<DictionaryEntry>();
    while (Word!=null) {
      if (Word.length()>=3) {
        Word = Word.toUpperCase();
        if (Word.length()>maxWordLength) maxWordLength = Word.length();
        DictionaryEntry entry = new DictionaryEntry();
        entry.letters  = new int[Word.length()  ];
        entry.triplets = new int[Word.length()-2];
        int i=0;
        for (char letter: Word.toCharArray()) {
          entry.letters[i] = (byte) letter - 65; // Convert ASCII to 0..25
          if (i>=2)
            entry.triplets[i-2] = (((entry.letters[i-2]  << ALPHABET_SIZE) +
                                     entry.letters[i-1]) << ALPHABET_SIZE) +
                                     entry.letters[i];
          i++;
        }
        result.add(entry);
      }
      Word = fileReader.readLine();
    }
    return result.toArray(new DictionaryEntry[result.size()]);
  }

  boolean isWrap(int a, int b) { // Checks if move a->b wraps board Edge (like 3->4)
    return Math.abs(a%BOARD_SIZE-b%BOARD_SIZE)>1;
  }

  int[] buildTripletIndices() {
    ArrayList<Integer> result = new ArrayList<Integer>();
    for (int a=0; a<BOARD_SIZE*BOARD_SIZE; a++)
      for (int bm: moves) {
        int b=a+bm;
        if ((b>=0) && (b<board.length) && !isWrap(a, b))
          for (int cm: moves) {
            int c=b+cm;
            if ((c>=0) && (c<board.length) && (c!=a) && !isWrap(b, c)) {
              result.add(a);
              result.add(b);
              result.add(c);
            }
          }
      }
    int[] result2 = new int[result.size()];
    int i=0;
    for (Integer r: result) result2[i++] = r;
    return result2;
  }


  // Variables that depend on the actual game layout
  int[] board = new int[BOARD_SIZE*BOARD_SIZE]; // Letters in board
  boolean[] possibleTriplets = new boolean[1 << (ALPHABET_SIZE*3)];

  DictionaryEntry[] candidateWords;
  int candidateCount;

  int[] usedBoardPositions;

  DictionaryEntry[] foundWords;
  int foundCount;

  void initializeBoard(String[] letters) {
    for (int row=0; row<BOARD_SIZE; row++)
      for (int col=0; col<BOARD_SIZE; col++)
        board[row*BOARD_SIZE + col] = (byte) letters[row].charAt(col) - 65;
  }

  void setPossibleTriplets() {
    Arrays.fill(possibleTriplets, false); // Reset list
    int i=0;
    while (i<boardTripletIndices.length) {
      int triplet = (((board[boardTripletIndices[i++]]  << ALPHABET_SIZE) +
                       board[boardTripletIndices[i++]]) << ALPHABET_SIZE) +
                       board[boardTripletIndices[i++]];
      possibleTriplets[triplet] = true; 
    }
  }

  void checkWordTriplets() {
    candidateCount = 0;
    for (DictionaryEntry entry: dictionary) {
      boolean ok = true;
      int len = entry.triplets.length;
      for (int t=0; (t<len) && ok; t++)
        ok = possibleTriplets[entry.triplets[t]];
      if (ok) candidateWords[candidateCount++] = entry;
    }
  }

  void checkWords() { // Can probably be optimized a lot
    foundCount = 0;
    for (int i=0; i<candidateCount; i++) {
      DictionaryEntry candidate = candidateWords[i];
      for (int j=0; j<board.length; j++)
        if (board[j]==candidate.letters[0]) { 
          usedBoardPositions[0] = j;
          if (checkNextLetters(candidate, 1, j)) {
            foundWords[foundCount++] = candidate;
            break;
          }
        }
    }
  }

  boolean checkNextLetters(DictionaryEntry candidate, int letter, int pos) {
    if (letter==candidate.letters.length) return true;
    int match = candidate.letters[letter];
    for (int move: moves) {
      int next=pos+move;
      if ((next>=0) && (next<board.length) && (board[next]==match) && !isWrap(pos, next)) {
        boolean ok = true;
        for (int i=0; (i<letter) && ok; i++)
          ok = usedBoardPositions[i]!=next;
        if (ok) {
          usedBoardPositions[letter] = next;
          if (checkNextLetters(candidate, letter+1, next)) return true;
        }
      }
    }   
    return false;
  }


  // Just some helper functions
  String formatTime(long start, long end, long repetitions) {
    long time = (end-start)/repetitions;
    return time/1000000 + "." + (time/100000) % 10 + "" + (time/10000) % 10 + "ms";
  }

  String getWord(DictionaryEntry entry) {
    char[] result = new char[entry.letters.length];
    int i=0;
    for (int letter: entry.letters)
      result[i++] = (char) (letter+97);
    return new String(result);
  }

  void run() throws IOException {
    long start = System.nanoTime();

    // The following can be pre-computed and should be replaced by constants
    dictionary = buildDictionary("C:/TWL06.txt");
    boardTripletIndices = buildTripletIndices();
    long precomputed = System.nanoTime();


    // The following only needs to run once at the beginning of the program
    candidateWords     = new DictionaryEntry[dictionary.length]; // WAAAY too generous
    foundWords         = new DictionaryEntry[dictionary.length]; // WAAAY too generous
    usedBoardPositions = new int[maxWordLength];
    long initialized = System.nanoTime(); 

    for (int n=1; n<=100; n++) {
      // The following needs to run again for every new board
      initializeBoard(new String[] {"DGHI",
                                    "KLPS",
                                    "YEUT",
                                    "EORN"});
      setPossibleTriplets();
      checkWordTriplets();
      checkWords();
    }
    long solved = System.nanoTime();


    // Print out result and statistics
    System.out.println("Precomputation finished in " + formatTime(start, precomputed, 1)+":");
    System.out.println("  Words in the dictionary: "+dictionary.length);
    System.out.println("  Longest Word:            "+maxWordLength+" letters");
    System.out.println("  Number of triplet-moves: "+boardTripletIndices.length/3);
    System.out.println();

    System.out.println("Initialization finished in " + formatTime(precomputed, initialized, 1));
    System.out.println();

    System.out.println("Board solved in "+formatTime(initialized, solved, 100)+":");
    System.out.println("  Number of candidates: "+candidateCount);
    System.out.println("  Number of actual words: "+foundCount);
    System.out.println();

    System.out.println("Words found:");
    int w=0;
    System.out.print("  ");
    for (int i=0; i<foundCount; i++) {
      System.out.print(getWord(foundWords[i]));
      w++;
      if (w==10) {
        w=0;
        System.out.println(); System.out.print("  ");
      } else
        if (i<foundCount-1) System.out.print(", ");
    }
    System.out.println();
  }

  public static void main(String[] args) throws IOException {
    new BoggleSolver().run();
  }
}

Voici quelques résultats:

Pour la grille de la photo publiée dans la question initiale (DGHI ...):

Precomputation finished in 239.59ms:
  Words in the dictionary: 178590
  Longest Word:            15 letters
  Number of triplet-moves: 408

Initialization finished in 0.22ms

Board solved in 3.70ms:
  Number of candidates: 230
  Number of actual words: 163 

Words found:
  eek, eel, eely, eld, elhi, elk, ern, erupt, erupts, euro
  eye, eyer, ghi, ghis, glee, gley, glue, gluer, gluey, glut
  gluts, hip, hiply, hips, his, hist, kelp, kelps, kep, kepi
  kepis, keps, kept, kern, key, kye, lee, lek, lept, leu
  ley, lunt, lunts, lure, lush, lust, lustre, lye, nus, nut
  nuts, ore, ort, orts, ouph, ouphs, our, oust, out, outre
  outs, oyer, pee, per, pert, phi, phis, pis, pish, plus
  plush, ply, plyer, psi, pst, pul, pule, puler, pun, punt
  punts, pur, pure, puree, purely, pus, Push, put, puts, ree
  rely, rep, reply, reps, roe, roue, roup, roups, roust, rout
  routs, rue, rule, ruly, run, runt, runts, rupee, Rush, Rust
  rut, ruts, ship, shlep, sip, sipe, spue, spun, spur, spurn
  spurt, strep, stroy, stun, stupe, sue, suer, sulk, sulker, sulky
  Sun, sup, supe, super, sure, surely, tree, trek, trey, troupe
  troy, true, truly, tule, tun, tup, tups, turn, tush, ups
  urn, uts, yeld, yelk, Yelp, yelps, yep, yeps, yore, you
  your, yourn, yous

Pour les lettres affichées à titre d'exemple dans la question d'origine (FXIE ...)

Precomputation finished in 239.68ms:
  Words in the dictionary: 178590
  Longest Word:            15 letters
  Number of triplet-moves: 408

Initialization finished in 0.21ms

Board solved in 3.69ms:
  Number of candidates: 87
  Number of actual words: 76

Words found:
  amble, ambo, AMI, amie, asea, awa, awe, awes, awl, axil
  axile, axle, boil, bole, box, but, buts, east, Elm, emboli
  fame, fames, fax, lei, lie, lima, limb, limbo, limbs, Lime
  limes, lob, lobs, lox, mae, maes, maw, maws, max, maxi
  mesa, mew, mewl, mews, mil, mile, milo, mix, oil, ole
  sae, saw, sea, seam, semi, sew, stub, swam, swami, tub
  tubs, Tux, twa, twae, twaes, twas, uts, wae, waes, wamble
  wame, wames, was, wast, wax, west

Pour la grille 5x5 suivante:

R P R I T
A H H L N
I E T E P
Z R Y S G
O G W E Y

cela donne ceci:

Precomputation finished in 240.39ms:
  Words in the dictionary: 178590
  Longest Word:            15 letters
  Number of triplet-moves: 768

Initialization finished in 0.23ms

Board solved in 3.85ms:
  Number of candidates: 331
  Number of actual words: 240

Words found:
  aero, aery, ahi, air, airt, airth, airts, airy, ear, egest
  elhi, elint, erg, ergo, ester, eth, ether, eye, eyen, eyer
  eyes, eyre, eyrie, gel, gelt, gelts, gen, gent, gentil, gest
  geste, get, gets, gey, gor, gore, gory, grey, greyest, greys
  gyre, gyri, gyro, hae, haet, haets, hair, hairy, hap, harp
  heap, hear, heh, heir, help, helps, hen, hent, hep, her
  hero, hes, hest, het, hetero, heth, hets, hey, hie, hilt
  hilts, hin, hint, hire, hit, inlet, inlets, ire, leg, leges
  legs, lehr, lent, les, lest, let, lethe, lets, ley, leys
  lin, line, lines, liney, lint, lit, neg, negs, nest, nester
  net, nether, nets, nil, nit, ogre, ore, orgy, ort, orts
  pah, pair, par, peg, pegs, peh, pelt, pelter, peltry, pelts
  pen, pent, pes, pest, pester, pesty, pet, peter, pets, phi
  philter, philtre, phiz, pht, print, pst, rah, rai, rap, raphe
  raphes, reap, rear, rei, ret, rete, rets, rhaphe, rhaphes, rhea
  ria, rile, riles, riley, rin, rye, ryes, seg, sel, sen
  sent, senti, set, sew, spelt, spelter, spent, splent, spline, splint
  split, stent, step, stey, stria, striae, sty, stye, tea, tear
  teg, tegs, tel, ten, tent, thae, the, their, then, these
  thesp, they, thin, thine, thir, thirl, til, tile, tiles, tilt
  tilter, tilth, tilts, tin, tine, tines, tirl, trey, treys, trog
  try, tye, tyer, tyes, tyre, tyro, west, wester, wry, wryest
  wye, wyes, wyte, wytes, yea, yeah, year, yeh, Yelp, yelps
  yen, yep, yeps, yes, yester, yet, yew, yews, zero, zori

Pour cela, j'ai utilisé le TWL06 Tournament Scrabble Liste de Mots , car le lien dans la question initiale ne fonctionne plus. Ce fichier mesure 1,85 Mo, il est donc un peu plus court. Et la fonction buildDictionary jette tous les mots de moins de 3 lettres.

Voici quelques observations sur la performance de ceci:

  • Il est environ 10 fois plus lent que la performance rapportée de la mise en œuvre OCaml de Victor Nicollet. Que cela soit causé par l'algorithme différent, le dictionnaire plus court qu'il a utilisé, le fait que son code est compilé et que le mien s'exécute sur une machine virtuelle Java, ou les performances de nos ordinateurs (le mien est un Intel Q6600 @ 2,4 MHz sous WinXP), je ne sais pas. Mais c'est beaucoup plus rapide que les résultats des autres implémentations citées à la fin de la question initiale. Donc, si cet algorithme est supérieur au dictionnaire de trie ou non, je ne le sais pas pour le moment.

  • La méthode de table utilisée dans checkWordTriplets() donne une très bonne approximation des réponses réelles. Le test de la fonction checkWords() échoue uniquement dans 1 à 3 mots (Voir nombre de candidats vs. nombre de mots] ci-dessus).

  • Quelque chose que vous ne pouvez pas voir ci-dessus: La fonction checkWordTriplets() prend environ 3,65 ms et est donc totalement dominante dans le processus de recherche. La fonction checkWords() occupe à peu près les 0,05 à 0,20 ms restants.

  • Le temps d'exécution de la fonction checkWordTriplets() dépend linéairement de la taille du dictionnaire et est pratiquement indépendant de la taille du tableau!

  • Le temps d'exécution de checkWords() dépend de la taille du tableau et du nombre de mots non exclus par checkWordTriplets().

  • L'implémentation checkWords() ci-dessus est la première version la plus stupide à laquelle j'ai pensé. Ce n'est fondamentalement pas optimisé du tout. Mais comparé à checkWordTriplets(), il n’est pas pertinent pour les performances globales de l’application, je ne me suis donc pas inquiété pour cela. Mais , si la taille du tableau devient plus grande, cette fonction deviendra de plus en plus lente et finira par commencer à devenir importante. Ensuite, il faudrait également l'optimiser.

  • Une bonne chose à propos de ce code est sa flexibilité:

    • Vous pouvez facilement changer la taille du tableau: mettez à jour la ligne 10 et le tableau de chaînes transmis à initializeBoard().
    • Il peut prendre en charge des alphabets plus grands/différents et traiter des choses telles que le traitement de "Qu" en une seule lettre sans aucune surcharge de performances. Pour ce faire, il faudrait mettre à jour la ligne 9 et les deux endroits où les caractères sont convertis en nombres (pour l’instant, il suffit de soustraire 65 de la valeur ASCII).

Ok, mais je pense que ce billet est maintenant assez long. Je peux certainement répondre à vos questions, mais passons aux commentaires.

23
Markus A.

Ma tentative en Java. Il faut environ 2 secondes pour lire le fichier et le créer, et environ 50 ms pour résoudre le casse-tête. J'ai utilisé le dictionnaire lié à la question (il contient quelques mots que je ne connaissais pas existent en anglais tels que fae, ima)

0 [main] INFO gineer.bogglesolver.util.Util  - Reading the dictionary
2234 [main] INFO gineer.bogglesolver.util.Util  - Finish reading the dictionary
2234 [main] INFO gineer.bogglesolver.Solver  - Found: FAM
2234 [main] INFO gineer.bogglesolver.Solver  - Found: FAME
2234 [main] INFO gineer.bogglesolver.Solver  - Found: FAMBLE
2234 [main] INFO gineer.bogglesolver.Solver  - Found: FAE
2234 [main] INFO gineer.bogglesolver.Solver  - Found: IMA
2234 [main] INFO gineer.bogglesolver.Solver  - Found: ELI
2234 [main] INFO gineer.bogglesolver.Solver  - Found: Elm
2234 [main] INFO gineer.bogglesolver.Solver  - Found: ELB
2234 [main] INFO gineer.bogglesolver.Solver  - Found: AXIL
2234 [main] INFO gineer.bogglesolver.Solver  - Found: AXILE
2234 [main] INFO gineer.bogglesolver.Solver  - Found: AXLE
2234 [main] INFO gineer.bogglesolver.Solver  - Found: AMI
2234 [main] INFO gineer.bogglesolver.Solver  - Found: AMIL
2234 [main] INFO gineer.bogglesolver.Solver  - Found: AMLI
2234 [main] INFO gineer.bogglesolver.Solver  - Found: AME
2234 [main] INFO gineer.bogglesolver.Solver  - Found: AMBLE
2234 [main] INFO gineer.bogglesolver.Solver  - Found: AMBO
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AES
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AWL
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AWE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AWEST
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AWA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MIX
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MIL
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MILE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MILO
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MAX
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MAE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MAW
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MEW
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MEWL
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MES
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MESA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MWA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: MWA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LIE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LIM
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LIMA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LIMAX
2250 [main] INFO gineer.bogglesolver.Solver  - Found: Lime
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LIMES
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LIMB
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LIMBO
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LIMBU
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LEI
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LEO
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LOB
2250 [main] INFO gineer.bogglesolver.Solver  - Found: LOX
2250 [main] INFO gineer.bogglesolver.Solver  - Found: OIME
2250 [main] INFO gineer.bogglesolver.Solver  - Found: OIL
2250 [main] INFO gineer.bogglesolver.Solver  - Found: OLE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: OLM
2250 [main] INFO gineer.bogglesolver.Solver  - Found: EMIL
2250 [main] INFO gineer.bogglesolver.Solver  - Found: EMBOLE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: EMBOX
2250 [main] INFO gineer.bogglesolver.Solver  - Found: EAST
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WAF
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WAX
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WAME
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WAMBLE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WAE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WEA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WEAM
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WEM
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WEA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WES
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WEST
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WAE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WAS
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WASE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: WAST
2250 [main] INFO gineer.bogglesolver.Solver  - Found: BLEO
2250 [main] INFO gineer.bogglesolver.Solver  - Found: BLO
2250 [main] INFO gineer.bogglesolver.Solver  - Found: BOIL
2250 [main] INFO gineer.bogglesolver.Solver  - Found: BOLE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: BUT
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AES
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AWA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AWL
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AWE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AWEST
2250 [main] INFO gineer.bogglesolver.Solver  - Found: ASE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: ASEM
2250 [main] INFO gineer.bogglesolver.Solver  - Found: AST
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SEA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SEAX
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SEAM
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SEMI
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SEMBLE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SEW
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SEA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SWA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SWAM
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SWAMI
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SWA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SAW
2250 [main] INFO gineer.bogglesolver.Solver  - Found: SAWT
2250 [main] INFO gineer.bogglesolver.Solver  - Found: STU
2250 [main] INFO gineer.bogglesolver.Solver  - Found: STUB
2250 [main] INFO gineer.bogglesolver.Solver  - Found: TWA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: TWAE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: TWA
2250 [main] INFO gineer.bogglesolver.Solver  - Found: TWAE
2250 [main] INFO gineer.bogglesolver.Solver  - Found: TWAS
2250 [main] INFO gineer.bogglesolver.Solver  - Found: TUB
2250 [main] INFO gineer.bogglesolver.Solver  - Found: Tux

Le code source comprend 6 classes. Je les posterai ci-dessous (si ce n'est pas la bonne pratique sur StackOverflow, dites-le moi).

gineer.bogglesolver.Main

package gineer.bogglesolver;

import org.Apache.log4j.BasicConfigurator;
import org.Apache.log4j.Logger;

public class Main
{
    private final static Logger logger = Logger.getLogger(Main.class);

    public static void main(String[] args)
    {
        BasicConfigurator.configure();

        Solver solver = new Solver(4,
                        "FXIE" +
                        "AMLO" +
                        "EWBX" +
                        "ASTU");
        solver.solve();

    }
}

gineer.bogglesolver.Solver

package gineer.bogglesolver;

import gineer.bogglesolver.trie.Trie;
import gineer.bogglesolver.util.Constants;
import gineer.bogglesolver.util.Util;
import org.Apache.log4j.Logger;

public class Solver
{
    private char[] puzzle;
    private int maxSize;

    private boolean[] used;
    private StringBuilder stringSoFar;

    private boolean[][] matrix;
    private Trie trie;

    private final static Logger logger = Logger.getLogger(Solver.class);

    public Solver(int size, String puzzle)
    {
        trie = Util.getTrie(size);
        matrix = Util.connectivityMatrix(size);

        maxSize = size * size;
        stringSoFar = new StringBuilder(maxSize);
        used = new boolean[maxSize];

        if (puzzle.length() == maxSize)
        {
            this.puzzle = puzzle.toCharArray();
        }
        else
        {
            logger.error("The puzzle size does not match the size specified: " + puzzle.length());
            this.puzzle = puzzle.substring(0, maxSize).toCharArray();
        }
    }

    public void solve()
    {
        for (int i = 0; i < maxSize; i++)
        {
            traverseAt(i);
        }
    }

    private void traverseAt(int Origin)
    {
        stringSoFar.append(puzzle[Origin]);
        used[Origin] = true;

        //Check if we have a valid Word
        if ((stringSoFar.length() >= Constants.MINIMUM_Word_LENGTH) && (trie.containKey(stringSoFar.toString())))
        {
            logger.info("Found: " + stringSoFar.toString());
        }

        //Find where to go next
        for (int destination = 0; destination < maxSize; destination++)
        {
            if (matrix[Origin][destination] && !used[destination] && trie.containPrefix(stringSoFar.toString() + puzzle[destination]))
            {
                traverseAt(destination);
            }
        }

        used[Origin] = false;
        stringSoFar.deleteCharAt(stringSoFar.length() - 1);
    }

}

gineer.bogglesolver.trie.Node

package gineer.bogglesolver.trie;

import gineer.bogglesolver.util.Constants;

class Node
{
    Node[] children;
    boolean isKey;

    public Node()
    {
        isKey = false;
        children = new Node[Constants.NUMBER_LETTERS_IN_ALPHABET];
    }

    public Node(boolean key)
    {
        isKey = key;
        children = new Node[Constants.NUMBER_LETTERS_IN_ALPHABET];
    }

    /**
     Method to insert a string to Node and its children

     @param key the string to insert (the string is assumed to be uppercase)
     @return true if the node or one of its children is changed, false otherwise
     */
    public boolean insert(String key)
    {
        //If the key is empty, this node is a key
        if (key.length() == 0)
        {
            if (isKey)
                return false;
            else
            {
                isKey = true;
                return true;
            }
        }
        else
        {//otherwise, insert in one of its child

            int childNodePosition = key.charAt(0) - Constants.LETTER_A;
            if (children[childNodePosition] == null)
            {
                children[childNodePosition] = new Node();
                children[childNodePosition].insert(key.substring(1));
                return true;
            }
            else
            {
                return children[childNodePosition].insert(key.substring(1));
            }
        }
    }

    /**
     Returns whether key is a valid prefix for certain key in this trie.
     For example: if key "hello" is in this trie, tests with all prefixes "hel", "hell", "hello" return true

     @param prefix the prefix to check
     @return true if the prefix is valid, false otherwise
     */
    public boolean containPrefix(String prefix)
    {
        //If the prefix is empty, return true
        if (prefix.length() == 0)
        {
            return true;
        }
        else
        {//otherwise, check in one of its child
            int childNodePosition = prefix.charAt(0) - Constants.LETTER_A;
            return children[childNodePosition] != null && children[childNodePosition].containPrefix(prefix.substring(1));
        }
    }

    /**
     Returns whether key is a valid key in this trie.
     For example: if key "hello" is in this trie, tests with all prefixes "hel", "hell" return false

     @param key the key to check
     @return true if the key is valid, false otherwise
     */
    public boolean containKey(String key)
    {
        //If the prefix is empty, return true
        if (key.length() == 0)
        {
            return isKey;
        }
        else
        {//otherwise, check in one of its child
            int childNodePosition = key.charAt(0) - Constants.LETTER_A;
            return children[childNodePosition] != null && children[childNodePosition].containKey(key.substring(1));
        }
    }

    public boolean isKey()
    {
        return isKey;
    }

    public void setKey(boolean key)
    {
        isKey = key;
    }
}

gineer.bogglesolver.trie.Trie

package gineer.bogglesolver.trie;

public class Trie
{
    Node root;

    public Trie()
    {
        this.root = new Node();
    }

    /**
     Method to insert a string to Node and its children

     @param key the string to insert (the string is assumed to be uppercase)
     @return true if the node or one of its children is changed, false otherwise
     */
    public boolean insert(String key)
    {
        return root.insert(key.toUpperCase());
    }

    /**
     Returns whether key is a valid prefix for certain key in this trie.
     For example: if key "hello" is in this trie, tests with all prefixes "hel", "hell", "hello" return true

     @param prefix the prefix to check
     @return true if the prefix is valid, false otherwise
     */
    public boolean containPrefix(String prefix)
    {
        return root.containPrefix(prefix.toUpperCase());
    }

    /**
     Returns whether key is a valid key in this trie.
     For example: if key "hello" is in this trie, tests with all prefixes "hel", "hell" return false

     @param key the key to check
     @return true if the key is valid, false otherwise
     */
    public boolean containKey(String key)
    {
        return root.containKey(key.toUpperCase());
    }


}

gineer.bogglesolver.util.Constants

package gineer.bogglesolver.util;

public class Constants
{

    public static final int NUMBER_LETTERS_IN_ALPHABET = 26;
    public static final char LETTER_A = 'A';
    public static final int MINIMUM_Word_LENGTH = 3;
    public static final int DEFAULT_PUZZLE_SIZE = 4;
}

gineer.bogglesolver.util.Util

package gineer.bogglesolver.util;

import gineer.bogglesolver.trie.Trie;
import org.Apache.log4j.Logger;

import Java.io.File;
import Java.io.FileNotFoundException;
import Java.util.Scanner;

public class Util
{
    private final static Logger logger = Logger.getLogger(Util.class);
    private static Trie trie;
    private static int size = Constants.DEFAULT_PUZZLE_SIZE;

    /**
     Returns the trie built from the dictionary.  The size is used to eliminate words that are too long.

     @param size the size of puzzle.  The maximum lenght of words in the returned trie is (size * size)
     @return the trie that can be used for puzzle of that size
     */
    public static Trie getTrie(int size)
    {
        if ((trie != null) && size == Util.size)
            return trie;

        trie = new Trie();
        Util.size = size;

        logger.info("Reading the dictionary");
        final File file = new File("dictionary.txt");
        try
        {
            Scanner scanner = new Scanner(file);
            final int maxSize = size * size;
            while (scanner.hasNext())
            {
                String line = scanner.nextLine().replaceAll("[^\\p{Alpha}]", "");

                if (line.length() <= maxSize)
                    trie.insert(line);
            }
        }
        catch (FileNotFoundException e)
        {
            logger.error("Cannot open file", e);
        }

        logger.info("Finish reading the dictionary");
        return trie;
    }

    static boolean[] connectivityRow(int x, int y, int size)
    {
        boolean[] squares = new boolean[size * size];
        for (int offsetX = -1; offsetX <= 1; offsetX++)
        {
            for (int offsetY = -1; offsetY <= 1; offsetY++)
            {
                final int calX = x + offsetX;
                final int calY = y + offsetY;
                if ((calX >= 0) && (calX < size) && (calY >= 0) && (calY < size))
                    squares[calY * size + calX] = true;
            }
        }

        squares[y * size + x] = false;//the current x, y is false

        return squares;
    }

    /**
     Returns the matrix of connectivity between two points.  Point i can go to point j iff matrix[i][j] is true
     Square (x, y) is equivalent to point (size * y + x).  For example, square (1,1) is point 5 in a puzzle of size 4

     @param size the size of the puzzle
     @return the connectivity matrix
     */
    public static boolean[][] connectivityMatrix(int size)
    {
        boolean[][] matrix = new boolean[size * size][];
        for (int x = 0; x < size; x++)
        {
            for (int y = 0; y < size; y++)
            {
                matrix[y * size + x] = connectivityRow(x, y, size);
            }
        }
        return matrix;
    }
}
23
gineer

De manière surprenante, personne n’a tenté une version PHP de cela.

Ceci est une version de travail PHP de la solution Python de John Fouhy.

Bien que je me sois inspiré des réponses de tout le monde, ceci est principalement copié de John. 

$boggle = "fxie
           amlo
           ewbx
           astu";

$alphabet = str_split(str_replace(array("\n", " ", "\r"), "", strtolower($boggle)));
$rows = array_map('trim', explode("\n", $boggle));
$dictionary = file("C:/dict.txt");
$prefixes = array(''=>'');
$words = array();
$regex = '/[' . implode('', $alphabet) . ']{3,}$/S';
foreach($dictionary as $k=>$value) {
    $value = trim(strtolower($value));
    $length = strlen($value);
    if(preg_match($regex, $value)) {
        for($x = 0; $x < $length; $x++) {
            $letter = substr($value, 0, $x+1);
            if($letter == $value) {
                $words[$value] = 1;
            } else {
                $prefixes[$letter] = 1;
            }
        }
    }
}

$graph = array();
$chardict = array();
$positions = array();
$c = count($rows);
for($i = 0; $i < $c; $i++) {
    $l = strlen($rows[$i]);
    for($j = 0; $j < $l; $j++) {
        $chardict[$i.','.$j] = $rows[$i][$j];
        $children = array();
        $pos = array(-1,0,1);
        foreach($pos as $z) {
            $xCoord = $z + $i;
            if($xCoord < 0 || $xCoord >= count($rows)) {
                continue;
            }
            $len = strlen($rows[0]);
            foreach($pos as $w) {
                $yCoord = $j + $w;
                if(($yCoord < 0 || $yCoord >= $len) || ($z == 0 && $w == 0)) {
                    continue;
                }
                $children[] = array($xCoord, $yCoord);
            }
        }
        $graph['None'][] = array($i, $j);
        $graph[$i.','.$j] = $children;
    }
}

function to_Word($chardict, $prefix) {
    $Word = array();
    foreach($prefix as $v) {
        $Word[] = $chardict[$v[0].','.$v[1]];
    }
    return implode("", $Word);
}

function find_words($graph, $chardict, $position, $prefix, $prefixes, &$results, $words) {
    $Word = to_Word($chardict, $prefix);
    if(!isset($prefixes[$Word])) return false;

    if(isset($words[$Word])) {
        $results[] = $Word;
    }

    foreach($graph[$position] as $child) {
        if(!in_array($child, $prefix)) {
            $newprefix = $prefix;
            $newprefix[] = $child;
            find_words($graph, $chardict, $child[0].','.$child[1], $newprefix, $prefixes, $results, $words);
        }
    }
}

$solution = array();
find_words($graph, $chardict, 'None', array(), $prefixes, $solution);
print_r($solution);

Voici un lien live si vous voulez l'essayer. Bien que cela prenne environ 2 secondes sur mon ordinateur local, il faut environ 5 secondes sur mon serveur Web. Dans les deux cas, ce n'est pas très rapide. Mais c’est quand même assez hideux, alors j’imagine que le temps peut être considérablement réduit. Toute indication sur la façon de réaliser cela serait appréciée. Le manque de tuples de PHP rendait les coordonnées difficiles à utiliser et mon incapacité à comprendre ce qui se passait ne m'aidait pas du tout.

EDIT: quelques corrections font qu'il faut moins de 1 en local.

19
Paolo Bergantino

Pas intéressé par VB? :) Je n'ai pas pu résister. J'ai résolu ce problème différemment de nombreuses solutions présentées ici.

Mes temps sont:

  • Chargement du dictionnaire et des préfixes Word dans une table de hachage: 0,5 à 1 seconde.
  • Trouver les mots: moyenne inférieure à 10 millisecondes.

EDIT: Les temps de chargement du dictionnaire sur le serveur hôte Web durent environ 1 à 1,5 secondes de plus que mon ordinateur personnel.

Je ne sais pas à quel point les temps vont se détériorer avec une charge sur le serveur.

J'ai écrit ma solution sous forme de page Web en .Net. myvrad.com/boggle

J'utilise le dictionnaire référencé dans la question initiale.

Les lettres ne sont pas réutilisées dans un mot. Seuls les mots de 3 caractères ou plus sont trouvés.

J'utilise une table de hachage de tous les préfixes et mots Word uniques au lieu d'un trie. Je ne savais pas à propos de Trie alors j'ai appris quelque chose là-bas. L'idée de créer une liste de préfixes de mots en plus des mots complets est ce qui a finalement permis à mon temps d'être réduit à un nombre respectable.

Lisez les commentaires de code pour plus de détails.

Voici le code:

Imports System.Collections.Generic
Imports System.IO

Partial Class boggle_Default

    'Bob Archer, 4/15/2009

    'To avoid using a 2 dimensional array in VB I'm not using typical X,Y
    'coordinate iteration to find paths.
    '
    'I have locked the code into a 4 by 4 grid laid out like so:
    ' abcd
    ' efgh
    ' ijkl
    ' mnop
    ' 
    'To find paths the code starts with a letter from a to p then
    'explores the paths available around it. If a neighboring letter
    'already exists in the path then we don't go there.
    '
    'Neighboring letters (grid points) are hard coded into
    'a Generic.Dictionary below.



    'Paths is a list of only valid Paths found. 
    'If a Word prefix or Word is not found the path is not
    'added and extending that path is terminated.
    Dim Paths As New Generic.List(Of String)

    'NeighborsOf. The keys are the letters a to p.
    'The value is a string of letters representing neighboring letters.
    'The string of neighboring letters is split and iterated later.
    Dim NeigborsOf As New Generic.Dictionary(Of String, String)

    'BoggleLetters. The keys are mapped to the lettered grid of a to p.
    'The values are what the user inputs on the page.
    Dim BoggleLetters As New Generic.Dictionary(Of String, String)

    'Used to store last postition of path. This will be a letter
    'from a to p.
    Dim LastPositionOfPath As String = ""

    'I found a HashTable was by far faster than a Generic.Dictionary 
    ' - about 10 times faster. This stores prefixes of words and words.
    'I determined 792773 was the number of words and unique prefixes that
    'will be generated from the dictionary file. This is a max number and
    'the final hashtable will not have that many.
    Dim HashTableOfPrefixesAndWords As New Hashtable(792773)

    'Stores words that are found.
    Dim FoundWords As New Generic.List(Of String)

    'Just to validate what the user enters in the grid.
    Dim ErrorFoundWithSubmittedLetters As Boolean = False

    Public Sub BuildAndTestPathsAndFindWords(ByVal ThisPath As String)
        'Word is the Word correlating to the ThisPath parameter.
        'This path would be a series of letters from a to p.
        Dim Word As String = ""

        'The path is iterated through and a Word based on the actual
        'letters in the Boggle grid is assembled.
        For i As Integer = 0 To ThisPath.Length - 1
            Word += Me.BoggleLetters(ThisPath.Substring(i, 1))
        Next

        'If my hashtable of Word prefixes and words doesn't contain this Word
        'Then this isn't a Word and any further extension of ThisPath will not
        'yield any words either. So exit sub to terminate exploring this path.
        If Not HashTableOfPrefixesAndWords.ContainsKey(Word) Then Exit Sub

        'The value of my hashtable is a boolean representing if the key if a Word (true) or
        'just a prefix (false). If true and at least 3 letters long then yay! Word found.
        If HashTableOfPrefixesAndWords(Word) AndAlso Word.Length > 2 Then Me.FoundWords.Add(Word)

        'If my List of Paths doesn't contain ThisPath then add it.
        'Remember only valid paths will make it this far. Paths not found
        'in the HashTableOfPrefixesAndWords cause this sub to exit above.
        If Not Paths.Contains(ThisPath) Then Paths.Add(ThisPath)

        'Examine the last letter of ThisPath. We are looking to extend the path
        'to our neighboring letters if any are still available.
        LastPositionOfPath = ThisPath.Substring(ThisPath.Length - 1, 1)

        'Loop through my list of neighboring letters (representing grid points).
        For Each Neighbor As String In Me.NeigborsOf(LastPositionOfPath).ToCharArray()
            'If I find a neighboring grid point that I haven't already used
            'in ThisPath then extend ThisPath and feed the new path into
            'this recursive function. (see recursive.)
            If Not ThisPath.Contains(Neighbor) Then Me.BuildAndTestPathsAndFindWords(ThisPath & Neighbor)
        Next
    End Sub

    Protected Sub ButtonBoggle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonBoggle.Click

        'User has entered the 16 letters and clicked the go button.

        'Set up my Generic.Dictionary of grid points, I'm using letters a to p -
        'not an x,y grid system.  The values are neighboring points.
        NeigborsOf.Add("a", "bfe")
        NeigborsOf.Add("b", "cgfea")
        NeigborsOf.Add("c", "dhgfb")
        NeigborsOf.Add("d", "hgc")
        NeigborsOf.Add("e", "abfji")
        NeigborsOf.Add("f", "abcgkjie")
        NeigborsOf.Add("g", "bcdhlkjf")
        NeigborsOf.Add("h", "cdlkg")
        NeigborsOf.Add("i", "efjnm")
        NeigborsOf.Add("j", "efgkonmi")
        NeigborsOf.Add("k", "fghlponj")
        NeigborsOf.Add("l", "ghpok")
        NeigborsOf.Add("m", "ijn")
        NeigborsOf.Add("n", "ijkom")
        NeigborsOf.Add("o", "jklpn")
        NeigborsOf.Add("p", "klo")

        'Retrieve letters the user entered.
        BoggleLetters.Add("a", Me.TextBox1.Text.ToLower.Trim())
        BoggleLetters.Add("b", Me.TextBox2.Text.ToLower.Trim())
        BoggleLetters.Add("c", Me.TextBox3.Text.ToLower.Trim())
        BoggleLetters.Add("d", Me.TextBox4.Text.ToLower.Trim())
        BoggleLetters.Add("e", Me.TextBox5.Text.ToLower.Trim())
        BoggleLetters.Add("f", Me.TextBox6.Text.ToLower.Trim())
        BoggleLetters.Add("g", Me.TextBox7.Text.ToLower.Trim())
        BoggleLetters.Add("h", Me.TextBox8.Text.ToLower.Trim())
        BoggleLetters.Add("i", Me.TextBox9.Text.ToLower.Trim())
        BoggleLetters.Add("j", Me.TextBox10.Text.ToLower.Trim())
        BoggleLetters.Add("k", Me.TextBox11.Text.ToLower.Trim())
        BoggleLetters.Add("l", Me.TextBox12.Text.ToLower.Trim())
        BoggleLetters.Add("m", Me.TextBox13.Text.ToLower.Trim())
        BoggleLetters.Add("n", Me.TextBox14.Text.ToLower.Trim())
        BoggleLetters.Add("o", Me.TextBox15.Text.ToLower.Trim())
        BoggleLetters.Add("p", Me.TextBox16.Text.ToLower.Trim())

        'Validate user entered something with a length of 1 for all 16 textboxes.
        For Each S As String In BoggleLetters.Keys
            If BoggleLetters(S).Length <> 1 Then
                ErrorFoundWithSubmittedLetters = True
                Exit For
            End If
        Next

        'If input is not valid then...
        If ErrorFoundWithSubmittedLetters Then
            'Present error message.
        Else
            'Else assume we have 16 letters to work with and start finding words.
            Dim SB As New StringBuilder

            Dim Time As String = String.Format("{0}:{1}:{2}:{3}", Date.Now.Hour.ToString(), Date.Now.Minute.ToString(), Date.Now.Second.ToString(), Date.Now.Millisecond.ToString())

            Dim NumOfLetters As Integer = 0
            Dim Word As String = ""
            Dim TempWord As String = ""
            Dim Letter As String = ""
            Dim fr As StreamReader = Nothing
            fr = New System.IO.StreamReader(HttpContext.Current.Request.MapPath("~/boggle/dic.txt"))

            'First fill my hashtable with Word prefixes and words.
            'HashTable(PrefixOrWordString, BooleanTrueIfWordFalseIfPrefix)
            While fr.Peek <> -1
                Word = fr.ReadLine.Trim()
                TempWord = ""
                For i As Integer = 0 To Word.Length - 1
                    Letter = Word.Substring(i, 1)
                    'This optimization helped quite a bit. Words in the dictionary that begin
                    'with letters that the user did not enter in the grid shouldn't go in my hashtable.
                    '
                    'I realize most of the solutions went with a Trie. I'd never heard of that before,
                    'which is one of the neat things about SO, seeing how others approach challenges
                    'and learning some best practices.
                    '
                    'However, I didn't code a Trie in my solution. I just have a hashtable with 
                    'all words in the dicitonary file and all possible prefixes for those words.
                    'A Trie might be faster but I'm not coding it now. I'm getting good times with this.
                    If i = 0 AndAlso Not BoggleLetters.ContainsValue(Letter) Then Continue While
                    TempWord += Letter
                    If Not HashTableOfPrefixesAndWords.ContainsKey(TempWord) Then
                        HashTableOfPrefixesAndWords.Add(TempWord, TempWord = Word)
                    End If
                Next
            End While

            SB.Append("Number of Word Prefixes and Words in Hashtable: " & HashTableOfPrefixesAndWords.Count.ToString())
            SB.Append("<br />")

            SB.Append("Loading Dictionary: " & Time & " - " & String.Format("{0}:{1}:{2}:{3}", Date.Now.Hour.ToString(), Date.Now.Minute.ToString(), Date.Now.Second.ToString(), Date.Now.Millisecond.ToString()))
            SB.Append("<br />")

            Time = String.Format("{0}:{1}:{2}:{3}", Date.Now.Hour.ToString(), Date.Now.Minute.ToString(), Date.Now.Second.ToString(), Date.Now.Millisecond.ToString())

            'This starts a path at each point on the grid an builds a path until 
            'the string of letters correlating to the path is not found in the hashtable
            'of Word prefixes and words.
            Me.BuildAndTestPathsAndFindWords("a")
            Me.BuildAndTestPathsAndFindWords("b")
            Me.BuildAndTestPathsAndFindWords("c")
            Me.BuildAndTestPathsAndFindWords("d")
            Me.BuildAndTestPathsAndFindWords("e")
            Me.BuildAndTestPathsAndFindWords("f")
            Me.BuildAndTestPathsAndFindWords("g")
            Me.BuildAndTestPathsAndFindWords("h")
            Me.BuildAndTestPathsAndFindWords("i")
            Me.BuildAndTestPathsAndFindWords("j")
            Me.BuildAndTestPathsAndFindWords("k")
            Me.BuildAndTestPathsAndFindWords("l")
            Me.BuildAndTestPathsAndFindWords("m")
            Me.BuildAndTestPathsAndFindWords("n")
            Me.BuildAndTestPathsAndFindWords("o")
            Me.BuildAndTestPathsAndFindWords("p")

            SB.Append("Finding Words: " & Time & " - " & String.Format("{0}:{1}:{2}:{3}", Date.Now.Hour.ToString(), Date.Now.Minute.ToString(), Date.Now.Second.ToString(), Date.Now.Millisecond.ToString()))
            SB.Append("<br />")

            SB.Append("Num of words found: " & FoundWords.Count.ToString())
            SB.Append("<br />")
            SB.Append("<br />")

            FoundWords.Sort()
            SB.Append(String.Join("<br />", FoundWords.ToArray()))

            'Output results.
            Me.LiteralBoggleResults.Text = SB.ToString()
            Me.PanelBoggleResults.Visible = True

        End If

    End Sub

End Class
16
rvarcher

Dès que j'ai vu l'énoncé du problème, j'ai pensé à "Trie". Mais vu que plusieurs autres afficheurs utilisaient cette approche, j'ai cherché une autre approche juste pour être différente. Hélas, l'approche de Trie fonctionne mieux. J'ai exécuté la solution Perl de Kent sur ma machine et il m'a fallu 0,31 secondes pour l'adapter à mon dictionnaire. Ma propre implémentation Perl a nécessité 0.54 secondes pour fonctionner. 

C'était mon approche:

  1. Créez un hachage de transition pour modéliser les transitions légales.

  2. Parcourez les 16 ^ 3 combinaisons possibles de trois lettres.

    • Dans la boucle, excluez les transitions illégales et répétez les visites sur la même place Formez toutes les séquences légales de 3 lettres et stockez-les dans un hachage.
  3. Parcourez ensuite tous les mots du dictionnaire.

    • Exclure les mots trop longs ou trop courts
    • Faites glisser une fenêtre de 3 lettres sur chaque mot et voyez s'il fait partie des combinaisons de 3 lettres de l'étape 2. Excluez les mots qui échouent. Cela élimine la plupart des non-correspondances.
    • S'il n'est toujours pas éliminé, utilisez un algorithme récursif pour voir si le mot peut être formé en créant des chemins dans le puzzle. (Cette partie est lente, mais appelée rarement.)
  4. Imprimez les mots que j'ai trouvés.

    J'ai essayé des séquences de 3 lettres et 4 lettres, mais des séquences de 4 lettres ont ralenti le programme.

Dans mon code, j'utilise/usr/share/dict/words pour mon dictionnaire. Il est livré en standard sur MAC OS X et de nombreux systèmes Unix. Vous pouvez utiliser un autre fichier si vous le souhaitez. Pour résoudre un casse-tête différent, il suffit de changer la variable @puzzle. Cela serait facile à adapter pour de plus grandes matrices. Il vous suffira de changer les hachages% transitions et% legalTransitions. 

La force de cette solution est que le code est court et les structures de données simples.

Voici le code Perl (qui utilise trop de variables globales, je sais):

#!/usr/bin/Perl
use Time::HiRes  qw{ time };

sub readFile($);
sub findAllPrefixes($);
sub isWordTraceable($);
sub findWordsInPuzzle(@);

my $startTime = time;

# Puzzle to solve

my @puzzle = ( 
    F, X, I, E,
    A, M, L, O,
    E, W, B, X,
    A, S, T, U
);

my $minimumWordLength = 3;
my $maximumPrefixLength = 3; # I tried four and it slowed down.

# Slurp the Word list.
my $wordlistFile = "/usr/share/dict/words";

my @words = split(/\n/, uc(readFile($wordlistFile)));
print "Words loaded from Word list: " . scalar @words . "\n";

print "Word file load time: " . (time - $startTime) . "\n";
my $postLoad = time;

# Define the legal transitions from one letter position to another. 
# Positions are numbered 0-15.
#     0  1  2  3
#     4  5  6  7
#     8  9 10 11
#    12 13 14 15
my %transitions = ( 
   -1 => [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
    0 => [1,4,5], 
    1 => [0,2,4,5,6],
    2 => [1,3,5,6,7],
    3 => [2,6,7],
    4 => [0,1,5,8,9],
    5 => [0,1,2,4,6,8,9,10],
    6 => [1,2,3,5,7,9,10,11],
    7 => [2,3,6,10,11],
    8 => [4,5,9,12,13],
    9 => [4,5,6,8,10,12,13,14],
    10 => [5,6,7,9,11,13,14,15],
    11 => [6,7,10,14,15],
    12 => [8,9,13],
    13 => [8,9,10,12,14],
    14 => [9,10,11,13,15],
    15 => [10,11,14]
);

# Convert the transition matrix into a hash for easy access.
my %legalTransitions = ();
foreach my $start (keys %transitions) {
    my $legalRef = $transitions{$start};
    foreach my $stop (@$legalRef) {
        my $index = ($start + 1) * (scalar @puzzle) + ($stop + 1);
        $legalTransitions{$index} = 1;
    }
}

my %prefixesInPuzzle = findAllPrefixes($maximumPrefixLength);

print "Find prefixes time: " . (time - $postLoad) . "\n";
my $postPrefix = time;

my @wordsFoundInPuzzle = findWordsInPuzzle(@words);

print "Find words in puzzle time: " . (time - $postPrefix) . "\n";

print "Unique prefixes found: " . (scalar keys %prefixesInPuzzle) . "\n";
print "Words found (" . (scalar @wordsFoundInPuzzle) . ") :\n    " . join("\n    ", @wordsFoundInPuzzle) . "\n";

print "Total Elapsed time: " . (time - $startTime) . "\n";

###########################################

sub readFile($) {
    my ($filename) = @_;
    my $contents;
    if (-e $filename) {
        # This is magic: it opens and reads a file into a scalar in one line of code. 
        # See http://www.Perl.com/pub/a/2003/11/21/Slurp.html
        $contents = do { local( @ARGV, $/ ) = $filename ; <> } ; 
    }
    else {
        $contents = '';
    }
    return $contents;
}

# Is it legal to move from the first position to the second? They must be adjacent.
sub isLegalTransition($$) {
    my ($pos1,$pos2) = @_;
    my $index = ($pos1 + 1) * (scalar @puzzle) + ($pos2 + 1);
    return $legalTransitions{$index};
}

# Find all prefixes where $minimumWordLength <= length <= $maxPrefixLength
#
#   $maxPrefixLength ... Maximum length of prefix we will store. Three gives best performance. 
sub findAllPrefixes($) {
    my ($maxPrefixLength) = @_;
    my %prefixes = ();
    my $puzzleSize = scalar @puzzle;

    # Every possible N-letter combination of the letters in the puzzle 
    # can be represented as an integer, though many of those combinations
    # involve illegal transitions, duplicated letters, etc.
    # Iterate through all those possibilities and eliminate the illegal ones.
    my $maxIndex = $puzzleSize ** $maxPrefixLength;

    for (my $i = 0; $i < $maxIndex; $i++) {
        my @path;
        my $remainder = $i;
        my $prevPosition = -1;
        my $prefix = '';
        my %usedPositions = ();
        for (my $prefixLength = 1; $prefixLength <= $maxPrefixLength; $prefixLength++) {
            my $position = $remainder % $puzzleSize;

            # Is this a valid step?
            #  a. Is the transition legal (to an adjacent square)?
            if (! isLegalTransition($prevPosition, $position)) {
                last;
            }

            #  b. Have we repeated a square?
            if ($usedPositions{$position}) {
                last;
            }
            else {
                $usedPositions{$position} = 1;
            }

            # Record this prefix if length >= $minimumWordLength.
            $prefix .= $puzzle[$position];
            if ($prefixLength >= $minimumWordLength) {
                $prefixes{$prefix} = 1;
            }

            Push @path, $position;
            $remainder -= $position;
            $remainder /= $puzzleSize;
            $prevPosition = $position;
        } # end inner for
    } # end outer for
    return %prefixes;
}

# Loop through all words in dictionary, looking for ones that are in the puzzle.
sub findWordsInPuzzle(@) {
    my @allWords = @_;
    my @wordsFound = ();
    my $puzzleSize = scalar @puzzle;
Word: foreach my $Word (@allWords) {
        my $wordLength = length($Word);
        if ($wordLength > $puzzleSize || $wordLength < $minimumWordLength) {
            # Reject Word as too short or too long.
        }
        elsif ($wordLength <= $maximumPrefixLength ) {
            # Word should be in the prefix hash.
            if ($prefixesInPuzzle{$Word}) {
                Push @wordsFound, $Word;
            }
        }
        else {
            # Scan through the Word using a window of length $maximumPrefixLength, looking for any strings not in our prefix list.
            # If any are found that are not in the list, this Word is not possible.
            # If no non-matches are found, we have more work to do.
            my $limit = $wordLength - $maximumPrefixLength + 1;
            for (my $startIndex = 0; $startIndex < $limit; $startIndex ++) {
                if (! $prefixesInPuzzle{substr($Word, $startIndex, $maximumPrefixLength)}) {
                    next Word;
                }
            }
            if (isWordTraceable($Word)) {
                # Additional test necessary: see if we can form this Word by following legal transitions
                Push @wordsFound, $Word;
            }
        }

    }
    return @wordsFound;
}

# Is it possible to trace out the Word using only legal transitions?
sub isWordTraceable($) {
    my $Word = shift;
    return traverse([split(//, $Word)], [-1]); # Start at special square -1, which may transition to any square in the puzzle.
}

# Recursively look for a path through the puzzle that matches the Word.
sub traverse($$) {
    my ($lettersRef, $pathRef) = @_;
    my $index = scalar @$pathRef - 1;
    my $position = $pathRef->[$index];
    my $letter = $lettersRef->[$index];
    my $branchesRef =  $transitions{$position};
BRANCH: foreach my $branch (@$branchesRef) {
            if ($puzzle[$branch] eq $letter) {
                # Have we used this position yet?
                foreach my $usedBranch (@$pathRef) {
                    if ($usedBranch == $branch) {
                        next BRANCH;
                    }
                }
                if (scalar @$lettersRef == $index + 1) {
                    return 1; # End of Word and success.
                }
                Push @$pathRef, $branch;
                if (traverse($lettersRef, $pathRef)) {
                    return 1; # Recursive success.
                }
                else {
                    pop @$pathRef;
                }
            }
        }
    return 0; # No path found. Failed.
}
11
Paul Chernoch

Je sais que je suis en retard, mais j’en ai fabriqué un il ya quelque temps en PHP - juste pour le plaisir aussi ...

http://www.lostsockdesign.com.au/sandbox/boggle/index.php?letters=fxieamloewbxastu Trouvé 75 mots (133 pts) en 0.90108 secondes

F.........X..I..............E............... A......................................M..............................L............................O............................... E....................W............................B..........................X A..................S..................................................T.................U....

Donne une indication de ce que le programme fait réellement - chaque lettre est l'endroit où il commence à regarder à travers les modèles tandis que chacun "." montre un chemin qu'il a essayé de prendre. Le plus '.' il y a plus il a cherché.

Faites-moi savoir si vous voulez le code ... c'est un mélange horrible de PHP et de HTML qui n'a jamais été censé voir le jour, alors je n'ose pas le poster ici: P

9
Danny

J'ai passé 3 mois à chercher une solution au problème des 10 denses planches 5x5 Boggle denses.

Le problème est maintenant résolu et présenté avec une divulgation complète sur 5 pages Web. S'il vous plaît contactez-moi avec des questions.

L'algorithme d'analyse de carte utilise une pile explicite pour parcourir de manière pseudo-récursive les carrés de la carte via un graphe de mots acyclique dirigé avec des informations enfant directes et un mécanisme de suivi d'horodatage. Il s’agit peut-être de la structure de données Lexicon la plus avancée au monde.

Le système évalue environ 10 000 très bonnes cartes par seconde sur un quad core. (9500+ points)

Page Web parent:

DeepSearch.c - http://www.pathcom.com/~vadco/deep.html

Pages Web de composant:

Tableau de bord optimal - http://www.pathcom.com/~vadco/binary.html

Structure avancée du lexique - http://www.pathcom.com/~vadco/adtdawg.html

Algorithme d'analyse de carte - http://www.pathcom.com/~vadco/guns.html

Traitement par lots en parallèle - http://www.pathcom.com/~vadco/parallel.html

- Ce corpus complet n'intéressera que ceux qui exigent le meilleur.

9
JohnPaul Adamovsky

Commencez par lire comment un des concepteurs de langage C # a résolu un problème connexe: http://blogs.msdn.com/ericlippert/archive/2009/02/04/a-nasality-talisman-for-the- sultana-analyst.aspx .

Comme lui, vous pouvez commencer avec un dictionnaire et les mots canonisés en créant un dictionnaire à partir d’un tableau de lettres triées par ordre alphabétique en une liste de mots pouvant être épelés à partir de ces lettres. 

Ensuite, commencez à créer les mots possibles à partir du tableau et à les rechercher. Je suppose que cela vous mènera assez loin, mais il y a certainement plus de trucs qui pourraient accélérer les choses. 

4
RossFabricant

Votre algorithme de recherche diminue-t-il continuellement la liste de mots au fur et à mesure que votre recherche se poursuit?

Par exemple, dans la recherche ci-dessus, vos mots ne peuvent commencer que par 13 lettres (ce qui réduit le nombre de lettres de départ à la moitié).

Au fur et à mesure que vous ajoutez des permutations de lettre, les ensembles de mots disponibles diminuent davantage, ce qui diminue la recherche nécessaire.

Je commencerais par là.

4
jerebear

Il faudrait que je réfléchisse davantage à une solution complète, mais en guise d’optimisation pratique, je me demande s’il serait peut-être intéressant de pré-calculer un tableau de fréquences de digrammes et de trigrammes (combinaisons de 2 et 3 lettres) basés sur tous les paramètres. mots de votre dictionnaire, et utilisez-le pour donner la priorité à votre recherche. J'irais avec les lettres de départ des mots. Donc, si votre dictionnaire contient les mots "Inde", "Eau", "Extrême" et "Extraordinaire", votre table pré-calculée peut être:

'IN': 1
'WA': 1
'EX': 2

Puis recherchez ces digrammes dans l'ordre des points communs (d'abord EX, puis WA/IN)

4
Smashery

Je suggère de faire un arbre de lettres basé sur des mots. L'arbre serait composé d'une structure de lettre, comme ceci:

letter: char
isWord: boolean

Ensuite, vous construisez l’arbre, chaque profondeur ajoutant une nouvelle lettre. En d'autres termes, au premier niveau, il y aurait l'alphabet; puis, à partir de chacun de ces arbres, il y aurait encore 26 entrées, et ainsi de suite, jusqu'à ce que vous ayez épelé tous les mots. Accrochez-vous à cet arbre analysé et toutes les réponses possibles seront trouvées plus rapidement.

Avec cet arbre analysé, vous pouvez très rapidement trouver des solutions. Voici le pseudo-code:

BEGIN: 
    For each letter:
        if the struct representing it on the current depth has isWord == true, enter it as an answer.
        Cycle through all its neighbors; if there is a child of the current node corresponding to the letter, recursively call BEGIN on it.

Cela pourrait être accéléré avec un peu de programmation dynamique. Par exemple, dans votre exemple, les deux «A» sont tous deux adjacents à un «E» et à un «W», ce qui (du point où ils les ont indiqués) serait identique. Je n'ai pas assez de temps pour bien préciser le code, mais je pense que vous pouvez rassembler l'idée.

De plus, je suis sûr que vous trouverez d'autres solutions si vous recherchez Google pour "solutionneur de Boggle".

4
Daniel Lew

Juste pour le plaisir, j'en ai mis en place un à bash. Ce n'est pas super rapide, mais raisonnable. 

http://dev.xkyle.com/bashboggle/

4
Kyle

J'ai écrit mon solveur en C++. J'ai mis en place une arborescence personnalisée. Je ne suis pas sûr que cela puisse être considéré comme un essai, mais c'est similaire. Chaque nœud a 26 branches, 1 pour chaque lettre de l'alphabet. Je traverse les branches du boggle board parallèlement aux branches de mon dictionnaire. Si la branche n’existe pas dans le dictionnaire, j’arrête de la chercher sur le tableau Boggle. Je convertis toutes les lettres du tableau en entiers. Donc 'A' = 0. Comme il ne s'agit que de tableaux, la recherche est toujours O (1). Chaque nœud stocke s'il complète un mot et combien de mots existent dans ses enfants. L’arbre est taillé au fur et à mesure que l’on constate que les mots réduisent la recherche répétée des mêmes mots. Je crois que la taille est aussi O (1).

CPU: Pentium SU2700 1,3 GHz
RAM: 3Go 

Charge le dictionnaire de 178 590 mots en <1 seconde.
Résout 100x100 Boggle (boggle.txt) en 4 secondes. ~ 44 000 mots trouvés.
La résolution d'un Boggle 4x4 est trop rapide pour fournir une référence significative. :)

Rapide Boggle Solver GitHub Repo

3
Will

Hilarant. J'ai presque posté la même question il y a quelques jours à cause du même jeu! Cependant, je n’ai pas cherché parce que je viens de chercher sur Google pour boggle solveur python et obtenu toutes les réponses que je pouvais souhaiter.

3
physicsmichael

Je me rends compte que l'heure de cette question est révolue, mais depuis que je travaillais moi-même sur un solveur, et que je suis tombé sur cela pendant que je cherchais sur Google, j'ai pensé que je devrais poster une référence à la mienne car elle semble un peu différente de certaines autres.

J'ai choisi d'aller avec un tableau plat pour le plateau de jeu et de faire des chasses récursives de chaque lettre sur le tableau, en passant de voisin valide à voisin valide, en étendant la recherche si la liste actuelle de lettres était un préfixe valide dans un index. En parcourant la notion de mot en cours, une liste d’index en tableau, pas de lettres constituant un mot. Lors de la vérification de l'index, les index sont traduits en lettres et la vérification est effectuée.

L'index est un dictionnaire de force brute qui ressemble un peu à un dictionnaire, mais permet les requêtes Pythonic de l'index. Si les mots 'cat' et 'cater' sont dans la liste, vous obtiendrez ceci dans le dictionnaire:

   d = { 'c': ['cat','cater'],
     'ca': ['cat','cater'],
     'cat': ['cat','cater'],
     'cate': ['cater'],
     'cater': ['cater'],
   }

Donc, si le current_Word est 'ca', vous savez que c'est un préfixe valide car 'ca' in d renvoie True (donc continuez la traversée du tableau). Et si le current_Word est 'cat', alors vous savez que c'est un mot valide, car c'est un préfixe valide et que 'cat' in d['cat'] renvoie également la valeur True.

Si cela est perçu, cela permet un code lisible qui ne semble pas trop lent. Comme tout le monde, la dépense dans ce système lit/construit l'index. Résoudre le tableau est à peu près du bruit.

Le code est à http://Gist.github.com/268079 . Il est intentionnellement vertical et naïf avec de nombreuses vérifications de validité explicites car je voulais comprendre le problème sans le casser avec un tas de magie ou d’obscurité.

3
cdent

Étant donné un tableau Boggle avec N lignes et M colonnes, supposons ce qui suit:

  • N * M est substantiellement supérieur au nombre de mots possibles
  • N * M est sensiblement plus grand que le mot le plus long possible

Sous ces hypothèses, la complexité de cette solution est O (N * M).

Je pense que la comparaison des durées d’exécution de cet exemple de carte manque à bien des égards, mais, par souci d’exhaustivité, cette solution s’achève en <0,2 s sur mon MacBook Pro moderne.

Cette solution trouvera tous les chemins possibles pour chaque mot du corpus.

#!/usr/bin/env Ruby
# Example usage: ./boggle-solver --board "fxie amlo ewbx astu"

autoload :Matrix, 'matrix'
autoload :OptionParser, 'optparse'

DEFAULT_CORPUS_PATH = '/usr/share/dict/words'.freeze

# Functions

def filter_corpus(matrix, corpus, min_Word_length)
  board_char_counts = Hash.new(0)
  matrix.each { |c| board_char_counts[c] += 1 }

  max_Word_length = matrix.row_count * matrix.column_count
  boggleable_regex = /^[#{board_char_counts.keys.reduce(:+)}]{#{min_Word_length},#{max_Word_length}}$/
  corpus.select{ |w| w.match boggleable_regex }.select do |w|
    Word_char_counts = Hash.new(0)
    w.each_char { |c| Word_char_counts[c] += 1 }
    Word_char_counts.all? { |c, count| board_char_counts[c] >= count }
  end
end

def neighbors(point, matrix)
  i, j = point
  ([i-1, 0].max .. [i+1, matrix.row_count-1].min).inject([]) do |r, new_i|
    ([j-1, 0].max .. [j+1, matrix.column_count-1].min).inject(r) do |r, new_j|
      neighbor = [new_i, new_j]
      neighbor.eql?(point) ? r : r << neighbor
    end
  end
end

def expand_path(path, Word, matrix)
  return [path] if path.length == Word.length

  next_char = Word[path.length]
  viable_neighbors = neighbors(path[-1], matrix).select do |point|
    !path.include?(point) && matrix.element(*point).eql?(next_char)
  end

  viable_neighbors.inject([]) do |result, point|
    result + expand_path(path.dup << point, Word, matrix)
  end
end

def find_paths(Word, matrix)
  result = []
  matrix.each_with_index do |c, i, j|
    result += expand_path([[i, j]], Word, matrix) if c.eql?(Word[0])
  end
  result
end

def solve(matrix, corpus, min_Word_length: 3)
  boggleable_corpus = filter_corpus(matrix, corpus, min_Word_length)
  boggleable_corpus.inject({}) do |result, w|
    paths = find_paths(w, matrix)
    result[w] = paths unless paths.empty?
    result
  end
end

# Script

options = { corpus_path: DEFAULT_CORPUS_PATH }
option_parser = OptionParser.new do |opts|
  opts.banner = 'Usage: boggle-solver --board <value> [--corpus <value>]'

  opts.on('--board BOARD', String, 'The board (e.g. "fxi aml ewb ast")') do |b|
    options[:board] = b
  end

  opts.on('--corpus CORPUS_PATH', String, 'Corpus file path') do |c|
    options[:corpus_path] = c
  end

  opts.on_tail('-h', '--help', 'Shows usage') do
    STDOUT.puts opts
    exit
  end
end
option_parser.parse!

unless options[:board]
  STDERR.puts option_parser
  exit false
end

unless File.file? options[:corpus_path]
  STDERR.puts "No corpus exists - #{options[:corpus_path]}"
  exit false
end

rows = options[:board].downcase.scan(/\S+/).map{ |row| row.scan(/./) }

raw_corpus = File.readlines(options[:corpus_path])
corpus = raw_corpus.map{ |w| w.downcase.rstrip }.uniq.sort

solution = solve(Matrix.rows(rows), corpus)
solution.each_pair do |w, paths|
  STDOUT.puts w
  paths.each do |path|
    STDOUT.puts "\t" + path.map{ |point| point.inspect }.join(', ')
  end
end
STDOUT.puts "TOTAL: #{solution.count}"
2
mon4goos

J'ai implémenté une solution dans OCaml . Il précompile un dictionnaire sous forme de dictionnaire et utilise des fréquences de séquence de deux lettres pour éliminer les arêtes qui ne pourraient jamais apparaître dans un mot afin d’accélérer le traitement. 

Il résout votre exemple de carte en 0,35 ms (avec un temps de démarrage supplémentaire de 6 ms, principalement lié au chargement du fichier dans la mémoire). 

Les solutions trouvées: 

["swami"; "emile"; "limbs"; "limbo"; "limes"; "amble"; "tubs"; "stub";
 "swam"; "semi"; "seam"; "awes"; "buts"; "bole"; "boil"; "west"; "east";
 "emil"; "lobs"; "limb"; "Lime"; "lima"; "mesa"; "mews"; "mewl"; "maws";
 "milo"; "mile"; "awes"; "amie"; "axle"; "elma"; "fame"; "ubs"; "Tux"; "tub";
 "twa"; "twa"; "stu"; "saw"; "sea"; "sew"; "sea"; "awe"; "awl"; "but"; "btu";
 "box"; "bmw"; "was"; "wax"; "oil"; "lox"; "lob"; "leo"; "lei"; "lie"; "mes";
 "mew"; "mae"; "maw"; "max"; "mil"; "mix"; "awe"; "awl"; "Elm"; "eli"; "fax"]
1
Victor Nicollet

Je voulais donc ajouter un autre moyen de résoudre ce problème PHP, car tout le monde aime PHP . Il y a un peu de refactoring que je voudrais faire, comme utiliser une correspondance d'expression rationnelle avec le fichier du dictionnaire, Suis en train de charger le fichier de dictionnaire entier dans une liste de mots.

Je l'ai fait en utilisant une idée de liste chaînée. Chaque nœud a une valeur de caractère, une valeur d'emplacement et un pointeur suivant.

La valeur d'emplacement est la façon dont j'ai découvert si deux nœuds sont connectés.

1     2     3     4
11    12    13    14
21    22    23    24
31    32    33    34

Donc, en utilisant cette grille, je sais que deux nœuds sont connectés si l'emplacement du premier nœud est égal à l'emplacement des deuxièmes nœuds +/- 1 pour la même rangée, +/- 9, 10, 11 pour la rangée supérieure et inférieure.

J'utilise la récursion pour la recherche principale. Il enlève un mot de la liste de mots, trouve tous les points de départ possibles, puis trouve récursivement la prochaine connexion possible, en gardant à l'esprit qu'il ne peut pas se rendre à un emplacement déjà utilisé (c'est pourquoi j'ai ajouté $ notInLoc).

Quoi qu'il en soit, je sais que cela nécessite une refactorisation, et j'aimerais connaître votre avis sur la façon de le rendre plus propre, mais les résultats obtenus sont corrects en fonction du fichier de dictionnaire que j'utilise. Selon le nombre de voyelles et de combinaisons sur le tableau, cela prend environ 3 à 6 secondes. Je sais qu'une fois que j'aurai obtenu les résultats du dictionnaire, cela réduira considérablement.

<?php
    ini_set('xdebug.var_display_max_depth', 20);
    ini_set('xdebug.var_display_max_children', 1024);
    ini_set('xdebug.var_display_max_data', 1024);

    class Node {
        var $loc;

        function __construct($value) {
            $this->value = $value;
            $next = null;
        }
    }

    class Boggle {
        var $root;
        var $locList = array (1, 2, 3, 4, 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34);
        var $wordList = [];
        var $foundWords = [];

        function __construct($board) {
            // Takes in a board string and creates all the nodes
            $node = new Node($board[0]);
            $node->loc = $this->locList[0];
            $this->root = $node;
            for ($i = 1; $i < strlen($board); $i++) {
                    $node->next = new Node($board[$i]);
                    $node->next->loc = $this->locList[$i];
                    $node = $node->next;
            }
            // Load in a dictionary file
            // Use regexp to elimate all the words that could never appear and load the 
            // rest of the words into wordList
            $handle = fopen("dict.txt", "r");
            if ($handle) {
                while (($line = fgets($handle)) !== false) {
                    // process the line read.
                    $line = trim($line);
                    if (strlen($line) > 2) {
                        $this->wordList[] = trim($line);
                    }
                }
                fclose($handle);
            } else {
                // error opening the file.
                echo "Problem with the file.";
            } 
        }

        function isConnected($node1, $node2) {
        // Determines if 2 nodes are connected on the boggle board

            return (($node1->loc == $node2->loc + 1) || ($node1->loc == $node2->loc - 1) ||
               ($node1->loc == $node2->loc - 9) || ($node1->loc == $node2->loc - 10) || ($node1->loc == $node2->loc - 11) ||
               ($node1->loc == $node2->loc + 9) || ($node1->loc == $node2->loc + 10) || ($node1->loc == $node2->loc + 11)) ? true : false;

        }

        function find($value, $notInLoc = []) {
            // Returns a node with the value that isn't in a location
            $current = $this->root;
            while($current) {
                if ($current->value == $value && !in_array($current->loc, $notInLoc)) {
                    return $current;
                }
                if (isset($current->next)) {
                    $current = $current->next;
                } else {
                    break;
                }
            }
            return false;
        }

        function findAll($value) {
            // Returns an array of nodes with a specific value
            $current = $this->root;
            $foundNodes = [];
            while ($current) {
                if ($current->value == $value) {
                    $foundNodes[] = $current;
                }
                if (isset($current->next)) {
                    $current = $current->next;
                } else {
                    break;
                }
            }
            return (empty($foundNodes)) ? false : $foundNodes;
        }

        function findAllConnectedTo($node, $value, $notInLoc = []) {
            // Returns an array of nodes that are connected to a specific node and 
            // contain a specific value and are not in a certain location
            $nodeList = $this->findAll($value);
            $newList = [];
            if ($nodeList) {
                foreach ($nodeList as $node2) {
                    if (!in_array($node2->loc, $notInLoc) && $this->isConnected($node, $node2)) {
                        $newList[] = $node2;
                    }
                }
            }
            return (empty($newList)) ? false : $newList;
        }



        function inner($Word, $list, $i = 0, $notInLoc = []) {
            $i++;
            foreach($list as $node) {
                $notInLoc[] = $node->loc;
                if ($list2 = $this->findAllConnectedTo($node, $Word[$i], $notInLoc)) {
                    if ($i == (strlen($Word) - 1)) {
                        return true;
                    } else {
                        return $this->inner($Word, $list2, $i, $notInLoc);
                    }
                }
            }
            return false;
        }

        function findWord($Word) {
            if ($list = $this->findAll($Word[0])) {
                return $this->inner($Word, $list);
            }
            return false;
        }

        function findAllWords() {
            foreach($this->wordList as $Word) {
                if ($this->findWord($Word)) {
                    $this->foundWords[] = $Word;
                }
            }
        }

        function displayBoard() {
            $current = $this->root;
            for ($i=0; $i < 4; $i++) {
                echo $current->value . " " . $current->next->value . " " . $current->next->next->value . " " . $current->next->next->next->value . "<br />";
                if ($i < 3) {
                    $current = $current->next->next->next->next;
                }
            }
        }

    }

    function randomBoardString() {
        return substr(str_shuffle(str_repeat("abcdefghijklmnopqrstuvwxyz", 16)), 0, 16);
    }

    $myBoggle = new Boggle(randomBoardString());
    $myBoggle->displayBoard();
    $x = microtime(true);
    $myBoggle->findAllWords();
    $y = microtime(true);
    echo ($y-$x);
    var_dump($myBoggle->foundWords);

    ?>
1
Nate

Voici la solution Utilisation de mots prédéfinis dans la boîte à outils NLTK NLTK contient le paquet nltk.corpus car il contient des mots et il contient plus de 2 mots anglais de Lakhs que vous pouvez simplement utiliser dans votre programme.

Une fois votre matrice créée, convertissez-la en un tableau de caractères et exécutez ce code.

import nltk
from nltk.corpus import words
from collections import Counter

def possibleWords(input, charSet):
    for Word in input:
        dict = Counter(Word)
        flag = 1
        for key in dict.keys():
            if key not in charSet:
                flag = 0
        if flag == 1 and len(Word)>5: #its depends if you want only length more than 5 use this otherwise remove that one. 
            print(Word)


nltk.download('words')
Word_list = words.words()
# prints 236736
print(len(Word_list))
charSet = ['h', 'e', 'l', 'o', 'n', 'v', 't']
possibleWords(Word_list, charSet)

Sortie:

eleven
eleventh
elevon
entente
entone
ethene
ethenol
evolve
evolvent
hellhole
helvell
hooven
letten
looten
nettle
nonene
nonent
nonlevel
notelet
novelet
novelette
novene
teenet
teethe
teevee
telethon
tellee
tenent
tentlet
theelol
toetoe
tonlet
toothlet
tootle
tottle
vellon
velvet
velveteen
venene
vennel
venthole
voeten
volent
volvelle
volvent
voteen

J'espère que vous l'obtenez.

1
lava kumar

Une solution JavaScript Node.JS. Calcule tous les 100 mots uniques en moins d'une seconde, ce qui inclut la lecture d'un fichier de dictionnaire (MBA 2012).

Sortie:
["FAM", "Tux", "TUB", "FAE", "ELI", "Elm", "ELB", "TWA", "TWA", "SAW", "AMI", "SWA "," SWA "," AME "," SEA "," SEW "," AES "," AWL "," AWE "," SEA "," AWA "," MIX "," MIL "," AST ", "ASE", "MAX", "MAE", "MAW", "MEW", "AWE", "MES", "AWL", "LIE", "LIM", "AWA", "AES", "MAIS" "," BLO "," WAS "," WAE "," WEA "," LEI "," LEO "," LOB "," LOX "," WEM "," OIL "," OLM "," WEA ", "WAE", "WAX", "WAF", "MILO", "EAST", "WAME", "TWAS", "TWAE", "EMIL", "WEAM", "OIME", "AXIL", "OUEST "," TWAE "," LIMB "," WASE "," WAST "," BLEO "," STUB "," BOIL "," BOLE "," Lime "," SAWT "," LIMA "," MESA ", "MEWL", "AXE", "FAME", "ASEM", "MILE", "AMIL", "SEAX", "SEAM", "SEMI", "SWAM", "AMBO", "AMLI", "AXILE "," AMBLE "," SWAMI "," AWEST "," AWEST "," LIMAX "," LIMES "," LIMBU "," LIMBO "," EMBOX "," SEMBLE "," EMBOLE "," WAMBLE ", "FAMBLE"]

Code:

var fs = require('fs')

var Node = function(value, row, col) {
    this.value = value
    this.row = row
    this.col = col
}

var Path = function() {
    this.nodes = []
}

Path.prototype.Push = function(node) {
    this.nodes.Push(node)
    return this
}

Path.prototype.contains = function(node) {
    for (var i = 0, ii = this.nodes.length; i < ii; i++) {
        if (this.nodes[i] === node) {
            return true
        }
    }

    return false
}

Path.prototype.clone = function() {
    var path = new Path()
    path.nodes = this.nodes.slice(0)
    return path
}

Path.prototype.to_Word = function() {
    var Word = ''

    for (var i = 0, ii = this.nodes.length; i < ii; ++i) {
        Word += this.nodes[i].value
    }

    return Word
}

var Board = function(nodes, dict) {
    // Expects n x m array.
    this.nodes = nodes
    this.words = []
    this.row_count = nodes.length
    this.col_count = nodes[0].length
    this.dict = dict
}

Board.from_raw = function(board, dict) {
    var ROW_COUNT = board.length
      , COL_COUNT = board[0].length

    var nodes = []

    // Replace board with Nodes
    for (var i = 0, ii = ROW_COUNT; i < ii; ++i) {
        nodes.Push([])
        for (var j = 0, jj = COL_COUNT; j < jj; ++j) {
            nodes[i].Push(new Node(board[i][j], i, j))
        }
    }

    return new Board(nodes, dict)
}

Board.prototype.toString = function() {
    return JSON.stringify(this.nodes)
}

Board.prototype.update_potential_words = function(dict) {
    for (var i = 0, ii = this.row_count; i < ii; ++i) {
        for (var j = 0, jj = this.col_count; j < jj; ++j) {
            var node = this.nodes[i][j]
              , path = new Path()

            path.Push(node)

            this.dfs_search(path)
        }
    }
}

Board.prototype.on_board = function(row, col) {
    return 0 <= row && row < this.row_count && 0 <= col && col < this.col_count
}

Board.prototype.get_unsearched_neighbours = function(path) {
    var last_node = path.nodes[path.nodes.length - 1]

    var offsets = [
        [-1, -1], [-1,  0], [-1, +1]
      , [ 0, -1],           [ 0, +1]
      , [+1, -1], [+1,  0], [+1, +1]
    ]

    var neighbours = []

    for (var i = 0, ii = offsets.length; i < ii; ++i) {
        var offset = offsets[i]
        if (this.on_board(last_node.row + offset[0], last_node.col + offset[1])) {

            var potential_node = this.nodes[last_node.row + offset[0]][last_node.col + offset[1]]
            if (!path.contains(potential_node)) {
                // Create a new path if on board and we haven't visited this node yet.
                neighbours.Push(potential_node)
            }
        }
    }

    return neighbours
}

Board.prototype.dfs_search = function(path) {
    var path_Word = path.to_Word()

    if (this.dict.contains_exact(path_Word) && path_Word.length >= 3) {
        this.words.Push(path_Word)
    }

    var neighbours = this.get_unsearched_neighbours(path)

    for (var i = 0, ii = neighbours.length; i < ii; ++i) {
        var neighbour = neighbours[i]
        var new_path = path.clone()
        new_path.Push(neighbour)

        if (this.dict.contains_prefix(new_path.to_Word())) {
            this.dfs_search(new_path)
        }
    }
}

var Dict = function() {
    this.dict_array = []

    var dict_data = fs.readFileSync('./web2', 'utf8')
    var dict_array = dict_data.split('\n')

    for (var i = 0, ii = dict_array.length; i < ii; ++i) {
        dict_array[i] = dict_array[i].toUpperCase()
    }

    this.dict_array = dict_array.sort()
}

Dict.prototype.contains_prefix = function(prefix) {
    // Binary search
    return this.search_prefix(prefix, 0, this.dict_array.length)
}

Dict.prototype.contains_exact = function(exact) {
    // Binary search
    return this.search_exact(exact, 0, this.dict_array.length)
}

Dict.prototype.search_prefix = function(prefix, start, end) {
    if (start >= end) {
        // If no more place to search, return no matter what.
        return this.dict_array[start].indexOf(prefix) > -1
    }

    var middle = Math.floor((start + end)/2)

    if (this.dict_array[middle].indexOf(prefix) > -1) {
        // If we prefix exists, return true.
        return true
    } else {
        // Recurse
        if (prefix <= this.dict_array[middle]) {
            return this.search_prefix(prefix, start, middle - 1)
        } else {
            return this.search_prefix(prefix, middle + 1, end)
        }
    }
}

Dict.prototype.search_exact = function(exact, start, end) {
    if (start >= end) {
        // If no more place to search, return no matter what.
        return this.dict_array[start] === exact
    }

    var middle = Math.floor((start + end)/2)

    if (this.dict_array[middle] === exact) {
        // If we prefix exists, return true.
        return true
    } else {
        // Recurse
        if (exact <= this.dict_array[middle]) {
            return this.search_exact(exact, start, middle - 1)
        } else {
            return this.search_exact(exact, middle + 1, end)
        }
    }
}

var board = [
    ['F', 'X', 'I', 'E']
  , ['A', 'M', 'L', 'O']
  , ['E', 'W', 'B', 'X']
  , ['A', 'S', 'T', 'U']
]

var dict = new Dict()

var b = Board.from_raw(board, dict)
b.update_potential_words()
console.log(JSON.stringify(b.words.sort(function(a, b) {
    return a.length - b.length
})))
1
Charlie Liang Yuan

Je sais que je suis vraiment en retard à la soirée, mais j’ai implémenté, comme exercice de codage, un logiciel de résolution de problèmes dans plusieurs langages de programmation (C++, Java, Go, C #, Python, Ruby, JavaScript, Julia, Lua, PHP, Perl) et Je pensais que cela intéresserait peut-être quelqu'un, alors je laisse le lien ici: https://github.com/AmokHuginnsson/boggle-solvers

0
AmokHuginnsson

Pourquoi ne pas trier simplement et utiliser le recherche binaire dans le dictionnaire?

Renvoie la liste entière en 0,35 s et peut être optimisé (en supprimant par exemple les mots avec des lettres non utilisées, etc.).

from bisect import bisect_left

f = open("dict.txt")
D.extend([line.strip() for line in f.readlines()])
D = sorted(D)

def neibs(M,x,y):
    n = len(M)
    for i in xrange(-1,2):
        for j in xrange(-1,2):
            if (i == 0 and j == 0) or (x + i < 0 or x + i >= n or y + j < 0 or y + j >= n):
                continue
            yield (x + i, y + j)

def findWords(M,D,x,y,prefix):
    prefix = prefix + M[x][y]

    # find Word in dict by binary search
    found = bisect_left(D,prefix)

    # if found then yield
    if D[found] == prefix: 
        yield prefix

    # if what we found is not even a prefix then return
    # (there is no point in going further)
    if len(D[found]) < len(prefix) or D[found][:len(prefix)] != prefix:
        return

    # recourse
    for neib in neibs(M,x,y):
        for Word in findWords(M,D,neib[0], neib[1], prefix):
            yield Word

def solve(M,D):
    # check each starting point
    for x in xrange(0,len(M)):
        for y in xrange(0,len(M)):
            for Word in findWords(M,D,x,y,""):
                yield Word

grid = "fxie amlo ewbx astu".split()
print [x for x in solve(grid,D)]
0
Łukasz Kidziński

J'ai résolu ceci en c. Il faut environ 48 ms pour s'exécuter sur ma machine (environ 98% du temps passé à charger le dictionnaire à partir du disque et à créer le fichier). Le dictionnaire est/usr/share/dict/american-english qui a 62886 mots.

Code source

0
matzahboy

Cette solution donne également le sens de la recherche dans le tableau donné

Algo:

1. Uses trie to save all the Word in the english to fasten the search
2. The uses DFS to search the words in Boggle

Sortie:

Found "pic" directions from (4,0)(p) go  → →
Found "pick" directions from (4,0)(p) go  → → ↑
Found "pickman" directions from (4,0)(p) go  → → ↑ ↑ ↖ ↑
Found "picket" directions from (4,0)(p) go  → → ↑ ↗ ↖
Found "picked" directions from (4,0)(p) go  → → ↑ ↗ ↘
Found "pickle" directions from (4,0)(p) go  → → ↑ ↘ →

Code:

from collections import defaultdict
from nltk.corpus import words
from nltk.corpus import stopwords
from nltk.tokenize import Word_tokenize

english_words = words.words()

# If you wan to remove stop words
# stop_words = set(stopwords.words('english'))
# english_words = [w for w in english_words if w not in stop_words]

boggle = [
    ['c', 'n', 't', 's', 's'],
    ['d', 'a', 't', 'i', 'n'],
    ['o', 'o', 'm', 'e', 'l'],
    ['s', 'i', 'k', 'n', 'd'],
    ['p', 'i', 'c', 'l', 'e']
]

# Instead of X and Y co-ordinates
# better to use Row and column
lenc = len(boggle[0])
lenr = len(boggle)

# Initialize trie datastructure
trie_node = {'valid': False, 'next': {}}

# lets get the delta to find all the nighbors
neighbors_delta = [
    (-1,-1, "↖"),
    (-1, 0, "↑"),
    (-1, 1, "↗"),
    (0, -1, "←"),
    (0,  1, "→"),
    (1, -1, "↙"),
    (1,  0, "↓"),
    (1,  1, "↘"),
]


def gen_trie(Word, node):
    """udpates the trie datastructure using the given Word"""
    if not Word:
        return

    if Word[0] not in node:
        node[Word[0]] = {'valid': len(Word) == 1, 'next': {}}

    # recursively build trie
    gen_trie(Word[1:], node[Word[0]])


def build_trie(words, trie):
    """Builds trie data structure from the list of words given"""
    for Word in words:
        gen_trie(Word, trie)
    return trie


def get_neighbors(r, c):
    """Returns the neighbors for a given co-ordinates"""
    n = []
    for neigh in neighbors_delta:
        new_r = r + neigh[0]
        new_c = c + neigh[1]

        if (new_r >= lenr) or (new_c >= lenc) or (new_r < 0) or (new_c < 0):
            continue
        n.append((new_r, new_c, neigh[2]))
    return n


def dfs(r, c, visited, trie, now_Word, direction):
    """Scan the graph using DFS"""
    if (r, c) in visited:
        return

    letter = boggle[r][c]
    visited.append((r, c))

    if letter in trie:
        now_Word += letter

        if trie[letter]['valid']:
            print('Found "{}" {}'.format(now_Word, direction))

        neighbors = get_neighbors(r, c)
        for n in neighbors:
            dfs(n[0], n[1], visited[::], trie[letter], now_Word, direction + " " + n[2])


def main(trie_node):
    """Initiate the search for words in boggle"""
    trie_node = build_trie(english_words, trie_node)

    # print the board
    print("Given board")
    for i in range(lenr):print (boggle[i])
    print ('\n')

    for r in range(lenr):
        for c in range(lenc):
            letter = boggle[r][c]
            dfs(r, c, [], trie_node, '', 'directions from ({},{})({}) go '.format(r, c, letter))


if __== '__main__':
    main(trie_node)
0
naren

J'ai résolu cela parfaitement et très rapidement. Je l'ai mis dans une application Android. Regardez la vidéo sur le lien Play Store pour la voir en action. 

Word Cheats est une application qui "craque" n'importe quel jeu de mots de style matriciel. Cette application a été construite. Pour m'aider à tricher avec Word Scrambler. Il peut être utilisé pour les recherches de mots, les ruptures, les mots, le localisateur de mots, le crack, le boggle et plus encore!

Vous pouvez le voir ici https://play.google.com/store/apps/details?id=com.harris.wordcracker

Visionnez l'application en action dans la vidéo https://www.youtube.com/watch?v=DL2974WmNAI

0
Josh Harris

J'ai également résolu ce problème avec Java. Mon implémentation est longue de 269 lignes et assez facile à utiliser. Vous devez d’abord créer une nouvelle instance de la classe Boggler, puis appeler la fonction de résolution avec la grille en tant que paramètre. Il faut environ 100 ms pour charger le dictionnaire de 50 000 mots sur mon ordinateur et trouver les mots en 10-20 ms environ. Les mots trouvés sont stockés dans une ArrayList, foundWords.

import Java.io.BufferedReader;
import Java.io.File;
import Java.io.FileInputStream;
import Java.io.FileNotFoundException;
import Java.io.IOException;
import Java.io.InputStreamReader;
import Java.net.URISyntaxException;
import Java.net.URL;
import Java.util.ArrayList;
import Java.util.Arrays;
import Java.util.Comparator;

public class Boggler {
    private ArrayList<String> words = new ArrayList<String>();      
    private ArrayList<String> roundWords = new ArrayList<String>(); 
    private ArrayList<Word> foundWords = new ArrayList<Word>();     
    private char[][] letterGrid = new char[4][4];                   
    private String letters;                                         

    public Boggler() throws FileNotFoundException, IOException, URISyntaxException {
        long startTime = System.currentTimeMillis();

        URL path = GUI.class.getResource("words.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path.toURI()).getAbsolutePath()), "iso-8859-1"));
        String line;
        while((line = br.readLine()) != null) {
            if(line.length() < 3 || line.length() > 10) {
                continue;
            }

            this.words.add(line);
        }
    }

    public ArrayList<Word> getWords() {
        return this.foundWords;
    }

    public void solve(String letters) {
        this.letters = "";
        this.foundWords = new ArrayList<Word>();

        for(int i = 0; i < letters.length(); i++) {
            if(!this.letters.contains(letters.substring(i, i + 1))) {
                this.letters += letters.substring(i, i + 1);
            }
        }

        for(int i = 0; i < 4; i++) {
            for(int j = 0; j < 4; j++) {
                this.letterGrid[i][j] = letters.charAt(i * 4 + j);
            }
        }

        System.out.println(Arrays.deepToString(this.letterGrid));               

        this.roundWords = new ArrayList<String>();      
        String pattern = "[" + this.letters + "]+";     

        for(int i = 0; i < this.words.size(); i++) {

            if(this.words.get(i).matches(pattern)) {
                this.roundWords.add(this.words.get(i));
            }
        }

        for(int i = 0; i < this.roundWords.size(); i++) {
            Word word = checkForWord(this.roundWords.get(i));

            if(Word != null) {
                System.out.println(Word);
                this.foundWords.add(Word);
            }
        }       
    }

    private Word checkForWord(String Word) {
        char initial = Word.charAt(0);
        ArrayList<LetterCoord> startPoints = new ArrayList<LetterCoord>();

        int x = 0;  
        int y = 0;
        for(char[] row: this.letterGrid) {
            x = 0;

            for(char letter: row) {
                if(initial == letter) {
                    startPoints.add(new LetterCoord(x, y));
                }

                x++;
            }

            y++;
        }

        ArrayList<LetterCoord> letterCoords = null;
        for(int initialTry = 0; initialTry < startPoints.size(); initialTry++) {
            letterCoords = new ArrayList<LetterCoord>();    

            x = startPoints.get(initialTry).getX(); 
            y = startPoints.get(initialTry).getY();

            LetterCoord initialCoord = new LetterCoord(x, y);
            letterCoords.add(initialCoord);

            letterLoop: for(int letterIndex = 1; letterIndex < Word.length(); letterIndex++) {
                LetterCoord lastCoord = letterCoords.get(letterCoords.size() - 1);  
                char currentChar = Word.charAt(letterIndex);                        

                ArrayList<LetterCoord> letterLocations = getNeighbours(currentChar, lastCoord.getX(), lastCoord.getY());

                if(letterLocations == null) {
                    return null;    
                }       

                for(int foundIndex = 0; foundIndex < letterLocations.size(); foundIndex++) {
                    if(letterIndex != Word.length() - 1 && true == false) {
                        char nextChar = Word.charAt(letterIndex + 1);
                        int lastX = letterCoords.get(letterCoords.size() - 1).getX();
                        int lastY = letterCoords.get(letterCoords.size() - 1).getY();

                        ArrayList<LetterCoord> possibleIndex = getNeighbours(nextChar, lastX, lastY);
                        if(possibleIndex != null) {
                            if(!letterCoords.contains(letterLocations.get(foundIndex))) {
                                letterCoords.add(letterLocations.get(foundIndex));
                            }
                            continue letterLoop;
                        } else {
                            return null;
                        }
                    } else {
                        if(!letterCoords.contains(letterLocations.get(foundIndex))) {
                            letterCoords.add(letterLocations.get(foundIndex));

                            continue letterLoop;
                        }
                    }
                }
            }

            if(letterCoords != null) {
                if(letterCoords.size() == Word.length()) {
                    Word w = new Word(word);
                    w.addList(letterCoords);
                    return w;
                } else {
                    return null;
                }
            }
        }

        if(letterCoords != null) {
            Word foundWord = new Word(word);
            foundWord.addList(letterCoords);

            return foundWord;
        }

        return null;
    }

    public ArrayList<LetterCoord> getNeighbours(char letterToSearch, int x, int y) {
        ArrayList<LetterCoord> neighbours = new ArrayList<LetterCoord>();

        for(int _y = y - 1; _y <= y + 1; _y++) {
            for(int _x = x - 1; _x <= x + 1; _x++) {
                if(_x < 0 || _y < 0 || (_x == x && _y == y) || _y > 3 || _x > 3) {
                    continue;
                }

                if(this.letterGrid[_y][_x] == letterToSearch && !neighbours.contains(new LetterCoord(_x, _y))) {
                    neighbours.add(new LetterCoord(_x, _y));
                }
            }
        }

        if(neighbours.isEmpty()) {
            return null;
        } else {
            return neighbours;
        }
    }
}

class Word {
    private String Word;    
    private ArrayList<LetterCoord> letterCoords = new ArrayList<LetterCoord>();

    public Word(String Word) {
        this.Word = Word;
    }

    public boolean addCoords(int x, int y) {
        LetterCoord lc = new LetterCoord(x, y);

        if(!this.letterCoords.contains(lc)) {
            this.letterCoords.add(lc);

            return true;
        }

        return false;
    }

    public void addList(ArrayList<LetterCoord> letterCoords) {
        this.letterCoords = letterCoords;
    } 

    @Override
    public String toString() {
        String outputString = this.Word + " ";
        for(int i = 0; i < letterCoords.size(); i++) {
            outputString += "(" + letterCoords.get(i).getX() + ", " + letterCoords.get(i).getY() + ") ";
        }

        return outputString;
    }

    public String getWord() {
        return this.Word;
    }

    public ArrayList<LetterCoord> getList() {
        return this.letterCoords;
    }
}

class LetterCoord extends ArrayList {
    private int x;          
    private int y;          

    public LetterCoord(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

    @Override
    public boolean equals(Object o) {
        if(!(o instanceof LetterCoord)) {
            return false;
        }

        LetterCoord lc = (LetterCoord) o;

        if(this.x == lc.getX() &&
                this.y == lc.getY()) {
            return true;
        }

        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + this.x;
        hash = 24 * hash + this.y;
        return hash;
    }
}
0
MikkoP
    package ProblemSolving;

import Java.util.HashSet;
import Java.util.Set;

/**
 * Given a 2-dimensional array of characters and a
 * dictionary in which a Word can be searched in O(1) time.
 * Need to print all the words from array which are present
 * in dictionary. Word can be formed in any direction but
 * has to end at any Edge of array.
 * (Need not worry much about the dictionary)
 */
public class DictionaryWord {
    private static char[][] matrix = new char[][]{
            {'a', 'f', 'h', 'u', 'n'},
            {'e', 't', 'a', 'i', 'r'},
            {'a', 'e', 'g', 'g', 'o'},
            {'t', 'r', 'm', 'l', 'p'}
    };
    private static int dim_x = matrix.length;
    private static int dim_y = matrix[matrix.length -1].length;
    private static Set<String> wordSet = new HashSet<String>();

    public static void main(String[] args) {
        //dictionary
        wordSet.add("after");
        wordSet.add("hate");
        wordSet.add("hair");
        wordSet.add("air");
        wordSet.add("eat");
        wordSet.add("tea");

        for (int x = 0; x < dim_x; x++) {
            for (int y = 0; y < dim_y; y++) {
                checkAndPrint(matrix[x][y] + "");
                int[][] visitedMap = new int[dim_x][dim_y];
                visitedMap[x][y] = 1;
                recursion(matrix[x][y] + "", visitedMap, x, y);
            }
        }
    }

    private static void checkAndPrint(String Word) {
        if (wordSet.contains(Word)) {
            System.out.println(Word);
        }
    }

    private static void recursion(String Word, int[][] visitedMap, int x, int y) {
        for (int i = Math.max(x - 1, 0); i < Math.min(x + 2, dim_x); i++) {
            for (int j = Math.max(y - 1, 0); j < Math.min(y + 2, dim_y); j++) {
                if (visitedMap[i][j] == 1) {
                    continue;
                } else {
                    int[][] newVisitedMap = new int[dim_x][dim_y];
                    for (int p = 0; p < dim_x; p++) {
                        for (int q = 0; q < dim_y; q++) {
                           newVisitedMap[p][q] = visitedMap[p][q];
                        }
                    }
                    newVisitedMap[i][j] = 1;
                    checkAndPrint(Word + matrix[i][j]);
                    recursion(Word + matrix[i][j], newVisitedMap, i, j);
                }
            }
        }
    }

}
0
Dheeraj Sachan
import Java.util.HashSet;
import Java.util.Set;

/**
 * @author Sujeet Kumar ([email protected]) It prints out all strings that can
 *         be formed by moving left, right, up, down, or diagonally and exist in
 *         a given dictionary , without repeating any cell. Assumes words are
 *         comprised of lower case letters. Currently prints words as many times
 *         as they appear, not just once. *
 */

public class BoggleGame 
{
  /* A sample 4X4 board/2D matrix */
  private static char[][] board = { { 's', 'a', 's', 'g' },
                                  { 'a', 'u', 't', 'h' }, 
                                  { 'r', 't', 'j', 'e' },
                                  { 'k', 'a', 'h', 'e' }
};

/* A sample dictionary which contains unique collection of words */
private static Set<String> dictionary = new HashSet<String>();

private static boolean[][] visited = new boolean[board.length][board[0].length];

public static void main(String[] arg) {
    dictionary.add("sujeet");
    dictionary.add("sarthak");
    findWords();

}

// show all words, starting from each possible starting place
private static void findWords() {
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            StringBuffer buffer = new StringBuffer();
            dfs(i, j, buffer);
        }

    }

}

// run depth first search starting at cell (i, j)
private static void dfs(int i, int j, StringBuffer buffer) {
    /*
     * base case: just return in recursive call when index goes out of the
     * size of matrix dimension
     */
    if (i < 0 || j < 0 || i > board.length - 1 || j > board[i].length - 1) {
        return;
    }

    /*
     * base case: to return in recursive call when given cell is already
     * visited in a given string of Word
     */
    if (visited[i][j] == true) { // can't visit a cell more than once
        return;
    }

    // not to allow a cell to reuse
    visited[i][j] = true;

    // combining cell character with other visited cells characters to form
    // Word a potential Word which may exist in dictionary
    buffer.append(board[i][j]);

    // found a Word in dictionary. Print it.
    if (dictionary.contains(buffer.toString())) {
        System.out.println(buffer);
    }

    /*
     * consider all neighbors.For a given cell considering all adjacent
     * cells in horizontal, vertical and diagonal direction
     */
    for (int k = i - 1; k <= i + 1; k++) {
        for (int l = j - 1; l <= j + 1; l++) {
            dfs(k, l, buffer);

        }

    }
    buffer.deleteCharAt(buffer.length() - 1);
    visited[i][j] = false;
  }
}
0
Sujeet

J'ai résolu ceci en C #, en utilisant un algorithme DFA. Vous pouvez consulter mon code à

https://github.com/attilabicsko/wordshuffler/

En plus de rechercher des mots dans une matrice, mon algorithme enregistre les chemins réels des mots. Ainsi, pour concevoir un jeu Word Finder, vous pouvez vérifier s’il existe un mot sur un chemin réel. 

0
Attila Bicskó

Voici mon implémentation Java: https://github.com/zouzhile/interview/blob/master/src/com/interview/algorithms/tree/BoggleSolver.Java

La construction de Trie a pris 0 heures, 0 minutes, 1 secondes et 532 millisecondes
La recherche du mot a pris 0 heures, 0 minutes, 0 secondes, 92 milliseconds

eel eeler eely eer eke eker eld eleut elk ell 
elle epee epihippus ere erept err error erupt eurus eye 
eyer eyey hip hipe hiper hippish hipple hippus his hish 
hiss hist hler hsi ihi iphis isis issue issuer ist 
isurus kee keek keeker keel keeler keep keeper keld kele 
kelek kelep kelk kell kelly kelp kelper kep kepi kept 
ker kerel kern keup keuper key kyl kyle lee leek 
leeky leep leer lek leo leper leptus lepus ler leu 
ley lleu lue lull luller lulu lunn lunt lunule luo 
lupe lupis lupulus lupus lur lure lurer lush lushly lust 
lustrous lut lye nul null nun nupe nurture nurturer nut 
oer ore ort ouphish our oust out outpeep outpeer outpipe 
outpull outpush output outre outrun outrush outspell outspue outspurn outspurt 
outstrut outstunt outsulk outturn outusure oyer pee peek peel peele 
peeler peeoy peep peeper peepeye peer pele peleus pell peller 
pelu pep peplus pepper pepperer pepsis per pern pert pertussis 
peru perule perun peul phi pip pipe piper pipi pipistrel 
pipistrelle pipistrellus pipper pish piss pist plup plus plush ply 
plyer psi pst puerer pul pule puler pulk pull puller 
pulley pullus pulp pulper pulu puly pun punt pup puppis 
pur pure puree purely purer purr purre purree purrel purrer 
puru purupuru pus Push puss pustule put putt puture ree 
reek reeker reeky reel reeler reeper rel rely reoutput rep 
repel repeller repipe reply repp reps reree rereel rerun reuel 
roe roer roey roue rouelle roun roup rouper roust rout 
roy rue ruelle ruer rule ruler rull ruller run runt 
rupee rupert rupture ruru rus Rush russ Rust rustre rut 
shi shih ship shipper shish shlu sip sipe siper sipper 
sis sish sisi siss sissu sist sistrurus speel speer spelk 
spell speller splurt spun spur spurn spurrer spurt sput ssi 
ssu stre stree streek streel streeler streep streke streperous strepsis 
strey stroup stroy stroyer strue strunt strut stu stue stull 
stuller stun stunt stupe stupeous stupp sturnus sturt stuss stut 
sue suer suerre suld sulk sulker sulky sull sully sulu 
Sun sunn sunt sunup sup supe super superoutput supper supple 
supplely supply sur sure surely surrey sus susi susu susurr 
susurrous susurrus sutu suture suu tree treey trek trekker trey 
troupe trouper trout troy true truer trull truller truly trun 
trush truss trust tshi tst tsun tsutsutsi tue tule tulle 
tulu tun tunu tup tupek tupi tur turn turnup turr 
turus tush tussis tussur tut tuts tutu tutulus ule ull 
uller ulu ululu unreel unrule unruly unrun unrust untrue untruly 
untruss untrust unturn unurn upper upperer uppish uppishly uppull uppush 
upspurt upsun upsup uptree uptruss upturn ure urn uro uru 
urus urushi ush ust usun usure usurer utu yee yeel 
yeld yelk yell yeller Yelp yelper yeo yep yer yere 
yern yoe yor yore you youl youp your yourn yoy 

Note: J'ai utilisé le dictionnaire et la matrice de caractères au début de cette discussion. Le code a été exécuté sur mon MacBookPro. Vous trouverez ci-dessous des informations sur la machine. 

Nom du modèle: MacBook Pro
Identificateur de modèle: MacBookPro8,1
Nom du processeur: Intel Core i5
Vitesse du processeur: 2,3 GHz
Nombre de processeurs: 1
Nombre total de noyaux: 2
Cache L2 (par cœur): 256 Ko
Cache L3: 3 Mo
Mémoire: 4 Go
Démarrez ROM Version: MBP81.0047.B0E
SMC Version (système): 1.68f96

0
Zhile Zou

C’est la solution que j’ai trouvée pour résoudre le problème d’argument. Je suppose que c'est la façon la plus "pythonique" de faire les choses:

from itertools import combinations
from itertools import izip
from math import fabs

def isAllowedStep(current,step,length,doubleLength):
            # for step == length -1 not to be 0 => trivial solutions are not allowed
    return length > 1 and \
           current + step < doubleLength and current - step > 0 and \
           ( step == 1 or step == -1 or step <= length+1 or step >= length - 1)

def getPairwiseList(someList):
    iterableList = iter(someList)
    return izip(iterableList, iterableList)

def isCombinationAllowed(combination,length,doubleLength):

    for (first,second) in  getPairwiseList(combination):
        _, firstCoordinate = first
        _, secondCoordinate = second
        if not isAllowedStep(firstCoordinate, fabs(secondCoordinate-firstCoordinate),length,doubleLength):
            return False
    return True

def extractSolution(combinations):
    return ["".join([x[0] for x in combinationTuple]) for combinationTuple in combinations]


length = 4
text = Tuple("".join("fxie amlo ewbx astu".split()))
textIndices = Tuple(range(len(text)))
coordinates = Zip(text,textIndices)

validCombinations = [combination for combination in combinations(coordinates,length) if isCombinationAllowed(combination,length,length*length)]
solution = extractSolution(validCombinations)

Cette partie, je vous conseille de ne pas utiliser pour toutes les correspondances possibles mais vous fournirait en fait une possibilité de vérifier si les mots que vous avez générés constituent réellement des mots valides:

import mechanize
def checkWord(Word):
    url = "https://en.oxforddictionaries.com/search?filter=dictionary&query="+Word
    br = mechanize.Browser()
    br.set_handle_robots(False)
    response = br.open(url)
    text = response.read()
    return "no exact matches"  not in text.lower()

print [valid for valid in solution[:10] if checkWord(valid)]