private static List <string> GetWords(Graph.Graph graph, Trie.Trie trie) { ISet <string> results = new HashSet <string>(); for (int i = 0; i < graph.Vertices.Count; i++) { string content = graph.Vertices[i].Content; bool[] visited = new bool[graph.Vertices.Count]; visited[i] = true; DepthFirstSearch(graph, i, trie, content, visited, results); } return(results.ToList()); }
private void btnRefresh_Click(object sender, RoutedEventArgs e) { string[,] table = new string[4, 4]; //table[0, 0] = "A"; //table[0, 1] = "B"; //table[0, 2] = "C"; //table[0, 3] = "D"; //table[1, 0] = "E"; //table[1, 1] = "F"; //table[1, 2] = "G"; //table[1, 3] = "H"; //table[2, 0] = "I"; //table[2, 1] = "J"; //table[2, 2] = "K"; //table[2, 3] = "L"; //table[3, 0] = "M"; //table[3, 1] = "N"; //table[3, 2] = "O"; //table[3, 3] = "P"; for (int column = 0; column < gridInput.ColumnDefinitions.Count; column++) { for (int row = 0; row < gridInput.RowDefinitions.Count; row++) { TextBox input = gridInput.Children.OfType <TextBox>().First(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == column); if (input != null) { table[row, column] = input.Text; } } } Graph.Graph graph = new Graph.Graph(table); List <string> words = GetWords(graph, _trie); _validWords = words; lstResults.Items.Clear(); foreach (string word in words.OrderByDescending(x => x.Length).ThenBy(x => x)) { lstResults.Items.Add(word); } }
private static void DepthFirstSearch(Graph.Graph graph, int vertexIndex, Trie.Trie trie, string content, bool[] visited, ISet <string> results) { if (trie.Contains(content) && content.Length >= 3) { results.Add(content); } foreach (int edgeIndex in graph.Vertices[vertexIndex].Edges) { if (!visited[edgeIndex]) { string newContent = content + graph.Vertices[edgeIndex].Content; Trie.TrieNode node = trie.GetNode(newContent); if (node != null) { visited[edgeIndex] = true; DepthFirstSearch(graph, edgeIndex, trie, newContent, visited, results); visited[edgeIndex] = false; } } } }