Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program is supposed to find all the words in a boggle grid using a normal dictionary file and limiting it to only words of

This program is supposed to find all the words in a boggle grid using a normal dictionary file and limiting it to only words of 3 or higher length to be found. It works for the most part but for some reason only some of the words are found not all. For example in this 4x4 grid it only finds 148 out of 191.

4 qu e a i n e r e t w s t o r k y 

These are the words it finds:

image text in transcribed

and it should find these:

image text in transcribed\

I dont know how to fix this. Here is my program:

import java.io.FileNotFoundException; import java.io.FileReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner;

//please make sure you have dict.txt containing mentioned dict in the same directory from where you are executing code

public class Project10 { private static Map dict = new HashMap(); public static void readDict(String filename) throws FileNotFoundException { FileReader fr = new FileReader(filename); Scanner sc = new Scanner(fr); while(sc.hasNext()) { dict.put(sc.next().toLowerCase(), 1); } sc.close(); } // A given function to check if a given string is present in // dictionary. public static boolean isWord(String word) { return word.length() >=3 && dict.containsKey(word.toLowerCase()); } public static List foundWordList = new ArrayList(); // A recursive function to print all words present on boggle public static void findWordsUtil(String board[][], boolean visited[][], int i, int j, String str) { // Mark current cell as visited and append current character // to str visited[i][j] = true; if (str.length() == 15) { return; } str = str + board[i][j];

// If str is present in dictionary, then print it if (isWord(str)) { if (!foundWordList.contains(str)) foundWordList.add(str); //System.out.println(str); } int M = board.length; int N = board[0].length; // Traverse 8 adjacent cells of boggle[i][j] for (int row=i-1; row=0 && col>=0 && !visited[row][col]) findWordsUtil(board,visited, row, col, str); } }

// Erase current character from string and mark visited // of current cell as false if (str != null && str.length() > 0 ) { str = str.substring(0, str.length()-1); } visited[i][j] = false; } // Prints all words present in dictionary. public static void findWords(String board[][]) { int M = board.length; int N = board[0].length; // Mark all characters as not visited boolean visited[][] = new boolean[M][N];

// Initialize current string String str = "";

// Consider every character and look for all words // starting with this character for (int i=0; i

findWords(board); Collections.sort(foundWordList); for(String word : foundWordList) System.out.println(word); } }

and this is the dictionary file to use in the code:

http://people.cs.pitt.edu/~hoffmant/S17-401/project-10/dictionary.txt

Document Word out References Mailings Review View V Tell me what you want to do te airest te erts rent tree airt nest te treen airts ester tes trees request tret tries tsk stere ewsy newt street twee arts strew tween artsy strewn stria rest striae weariest ret rets eeriest enter tea queen entera tear queer quest teel west erst uester ten wester este terai sent terse wriest Document Word out References Mailings Review View V Tell me what you want to do te airest te erts rent tree airt nest te treen airts ester tes trees request tret tries tsk stere ewsy newt street twee arts strew tween artsy strewn stria rest striae weariest ret rets eeriest enter tea queen entera tear queer quest teel west erst uester ten wester este terai sent terse wriest

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

LO3 Define the difference between job satisfaction and engagement.

Answered: 1 week ago