Advertisement
Guest User

Untitled

a guest
Dec 13th, 2021
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. import edu.princeton.cs.algs4.TrieSET;
  7. public class BoggleWordFinder {
  8.  
  9. // some useful constants
  10. public static final String WORD_LIST = "src/words";
  11. public static final int ROWS = 5;
  12. public static final int COLUMNS = 5;
  13. public static final int SEED = 137;
  14.  
  15. public BoggleWordFinder(String filename) throws Exception {
  16. TrieSET trie = new TrieSET();
  17. ArrayList<String> answers = new ArrayList<String>();
  18. initDictionary(filename, trie);
  19. BoggleBoard board = new BoggleBoard();
  20. System.out.println(board);
  21.  
  22. for (int i = 0; i < board.getColumns()-1; i++) {
  23. for (int j = 0; j < board.getRows()-1; j++) {
  24. dfs(i, j, new String(), answers, board, trie);
  25. board = clearVisited(board);
  26. }
  27. }
  28. //System.out.println(answers);
  29. }
  30.  
  31. private static BoggleBoard clearVisited(BoggleBoard board) {
  32. for (int i = 0; i < board.getColumns()-1; i++) {
  33. for (int j = 0; j < board.getRows()-1; j++) {
  34. board.setUnvisited(i,j);
  35. }
  36. }
  37. return board;
  38. }
  39.  
  40. public static void initDictionary(String filename, TrieSET trie) throws FileNotFoundException {
  41. File file = new File(filename);
  42. Scanner sc = new Scanner(file);
  43. while(sc.hasNextLine()) {
  44. if(Character.isLowerCase(sc.nextLine().charAt(0))) {
  45. trie.add(sc.nextLine());
  46. }
  47. }
  48. }
  49.  
  50. private static ArrayList<String> checkDictionary(String str, BoggleBoard board, TrieSET trie, ArrayList<String> answers) {
  51. for(String word : trie) {
  52. if(word.equals(str)) {
  53. System.out.println(str);
  54. answers.add(str);
  55. }
  56. }
  57. return answers;
  58. }
  59.  
  60. private static boolean isNull(TrieSET trie, String word) {
  61. int i = 0;
  62. for (String str: trie.keysWithPrefix(word)) {
  63. i++;
  64. }
  65. return i == 0 ? true : false;
  66. }
  67.  
  68. public static void dfs(int i, int j, String str, ArrayList<String> answers, BoggleBoard board, TrieSET trie) {
  69. if (i < 0 || j < 0 || i > board.getColumns()-1 || j > board.getRows()-1 || board.isVisited(i, j)) return;
  70. str += board.getCharAt(i,j);
  71. if(isNull(trie, str)) return;
  72. //System.out.println(str);
  73. //System.out.println(trie.keysWithPrefix(str));
  74. board.setVisited(i,j);
  75.  
  76. answers = checkDictionary(str, board, trie, answers);
  77. //north
  78. dfs(i, j+1, str, answers, board, trie);
  79. //NW
  80. dfs(i-1, j+1, str, answers, board, trie);
  81. //west
  82. dfs(i-1, j, str, answers, board, trie);
  83. //SW
  84. dfs(i-1, j-1, str, answers, board, trie);
  85. //south
  86. dfs(i, j-1, str, answers, board, trie);
  87. //SE
  88. dfs(i+1, j-1, str, answers, board, trie);
  89. //east
  90. dfs(i+1, j, str, answers, board, trie);
  91. //NE
  92. dfs(i+1, j+1, str, answers, board, trie);
  93.  
  94. board.setUnvisited(i,j);
  95. }
  96.  
  97.  
  98. public static void main(String[] args) throws Exception {
  99. System.out.println(new File("words").getAbsolutePath());
  100. BoggleWordFinder bwf = new BoggleWordFinder("D:\\Desktop\\Fall 2021\\CIS27 D&S\\Boggle Solver\\src\\words");
  101. }
  102.  
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement