public void AddSuffix( string suffix_ ) { if ( suffix_.Length == 0 ) { _finished = true; } else { char ch = suffix_[0]; if ( ! _next.ContainsKey( ch ) ) { _next[ch] = new DictionaryNode(); } _next[ch].AddSuffix( suffix_.Substring( 1 ) ); } }
public static void Main( string[] args ) { if ( args.Length > 0 ) { TextReader dictFile = new StreamReader( args[0] ); DictionaryNode dictionaryRoot = new DictionaryNode(); string line; while ( ( line = dictFile.ReadLine() ) != null ) { dictionaryRoot.AddSuffix( line ); } // dictionaryRoot.PrintAll(); Console.WriteLine( "[ OK ] Ready" ); string letters = ""; while ( ( line = Console.ReadLine() ) != null ) { if ( ( letters.Length > 0 ) && ( line.Length == 0 ) ) { Console.WriteLine( "[ OK ] Solving" ); try { Board board = new Board( letters ); foreach ( string word in board.Solve( dictionaryRoot ) ) { Console.WriteLine( "(" + word.Length + ") " + word + "" ); } Console.WriteLine( "[ OK ] Solved" ); } catch ( Exception e ) { Console.WriteLine( e.Message ); } letters = ""; } else { letters += line; } } } else { Console.WriteLine( "solve, error: please specify path." ); } }
void SolveRecursive( HashSet<string> result_, string prefix_, Cube cube_, DictionaryNode dictNode_ ) { DictionaryNode nextNode = dictNode_.Get( cube_.Letter ); if ( nextNode == null ) { return; } cube_.Visited = true; string newPrefix = prefix_ + cube_.Letter; if ( nextNode.Finished && ( newPrefix.Length >= 3 ) ) { result_.Add( newPrefix ); } foreach ( Cube neighbor in cube_.Neighbors ) { if ( ! neighbor.Visited ) { SolveRecursive( result_, newPrefix, neighbor, nextNode ); } } cube_.Visited = false; }
public List<string> Solve( DictionaryNode dictionary_ ) { HashSet<string> result = new HashSet<string>(); foreach ( Cube cube in _cubes ) { SolveRecursive( result, "", cube, dictionary_ ); } List<string> sorted = new List<string>( result ); return ( sorted.OrderBy( o => o.Length ).ToList() ); }