Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interesting question. I would solve this by first building a list of "possible word holders" (sequences of characters which can possibly hold one of the given words) by traversing the puzzle horizontally, vertically and diagonally (in both directions). I would then see if the given words (or their reverse) are present (using contains() method in Java) in each of the obtained "possible word holders". Here is the code I wrote in Java. I haven't tested it properly, but I guess it works!</p> <pre><code>import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; public class WordPuzzle { public Set&lt;String&gt; findWords(char[][] puzzle, Set&lt;String&gt; words) { Set&lt;String&gt; foundWords = new HashSet&lt;String&gt;(); int minimumWordLength = findMinimumWordLength(words); Set&lt;String&gt; possibleWords = findPossibleWords(puzzle, minimumWordLength); for(String word : words) { for(String possibleWord : possibleWords) { if(possibleWord.contains(word) || possibleWord.contains(new StringBuffer(word).reverse())) { foundWords.add(word); break; } } } return foundWords; } private int findMinimumWordLength(Set&lt;String&gt; words) { int minimumLength = Integer.MAX_VALUE; for(String word : words) { if(word.length() &lt; minimumLength) minimumLength = word.length(); } return minimumLength; } private Set&lt;String&gt; findPossibleWords(char[][] puzzle, int minimumWordLength) { Set&lt;String&gt; possibleWords = new LinkedHashSet&lt;String&gt;(); int dimension = puzzle.length; //Assuming puzzle is square if(dimension &gt;= minimumWordLength) { /* Every row in the puzzle is added as a possible word holder */ for(int i = 0; i &lt; dimension; i++) { if(puzzle[i].length &gt;= minimumWordLength) { possibleWords.add(new String(puzzle[i])); } } /* Every column in the puzzle is added as a possible word holder */ for(int i = 0; i &lt; dimension; i++) { StringBuffer temp = new StringBuffer(); for(int j = 0; j &lt; dimension; j++) { temp = temp.append(puzzle[j][i]); } possibleWords.add(new String(temp)); } /* Adding principle diagonal word holders */ StringBuffer temp1 = new StringBuffer(); StringBuffer temp2 = new StringBuffer(); for(int i = 0; i &lt; dimension; i++) { temp1 = temp1.append(puzzle[i][i]); temp2 = temp2.append(puzzle[i][dimension - i - 1]); } possibleWords.add(new String(temp1)); possibleWords.add(new String(temp2)); /* Adding non-principle diagonal word holders */ for(int i = 1; i &lt; dimension - minimumWordLength; i++) { temp1 = new StringBuffer(); temp2 = new StringBuffer(); StringBuffer temp3 = new StringBuffer(); StringBuffer temp4 = new StringBuffer(); for(int j = i, k = 0; j &lt; dimension &amp;&amp; k &lt; dimension; j++, k++) { temp1 = temp1.append(puzzle[j][k]); temp2 = temp2.append(puzzle[k][j]); temp3 = temp3.append(puzzle[dimension - j - 1][k]); temp4 = temp4.append(puzzle[dimension - k - 1][j]); } possibleWords.add(new String(temp1)); possibleWords.add(new String(temp2)); possibleWords.add(new String(temp3)); possibleWords.add(new String(temp4)); } } return possibleWords; } public static void main(String args[]) { WordPuzzle program = new WordPuzzle(); char[][] puzzle = { {'F','Y','Y','H','N','R','D'}, {'R','L','J','C','I','N','U'}, {'A','A','W','A','A','H','R'}, {'N','T','K','L','P','N','E'}, {'C','I','L','F','S','A','P'}, {'E','O','G','O','T','P','N'}, {'H','P','O','L','A','N','D'} }; Set&lt;String&gt; words = new HashSet&lt;String&gt;(); words.add("FRANCE"); words.add("POLAND"); words.add("INDIA"); words.add("JAPAN"); words.add("USA"); words.add("HOLLAND"); Set&lt;String&gt; wordsFound = program.findWords(puzzle, words); for(String word : wordsFound) { System.out.println(word); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload