Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. package rec.probs;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.Locale;
  8.  
  9. public class WordSolver {
  10. public class Piece{
  11. public final int row, column;
  12. public final char chr;
  13. public Piece(char chr, int row, int column){
  14. this.row = row;
  15. this.column = column;
  16. this.chr = chr;
  17. }
  18.  
  19. @Override
  20. public boolean equals(Object o){
  21. if(o instanceof Piece == false) return false;
  22. Piece oth = (Piece) o;
  23. return oth.row == this.row && oth.column == this.column && oth.chr == this.chr;
  24. }
  25.  
  26. @Override
  27. public String toString(){
  28. return this.row + "," + this.column + "=" + this.chr;
  29. }
  30. }
  31.  
  32. private final Piece[][] grid;
  33. public static final Locale locale = Locale.forLanguageTag("tr");
  34. private final Piece[] adjacents = new Piece[8]; // For 4x4
  35.  
  36. public WordSolver(char[][] grid){
  37. this.grid = new Piece[grid.length][];
  38.  
  39. for(int i = 0; i < grid.length; i++){
  40. char[] row = grid[i];
  41. this.grid[i] = new Piece[row.length];
  42. int x = 0;
  43.  
  44. for(char c : row){
  45. this.grid[i][x] = new Piece(Character.toLowerCase(c), i, x);
  46. x++;
  47. }
  48. }
  49. }
  50.  
  51. public Piece getPiece(int row, int column){
  52. return isValid(row, column) ? this.grid[row][column] : null;
  53. }
  54.  
  55. public boolean isValid(int row, int column){
  56. return row >= 0 && row < grid.length && column >= 0 && column < grid[row].length;
  57. }
  58.  
  59. public void fillAdjacents(Piece piece, Piece[] adjacents, ArrayList<Piece> exclude){
  60. int index = 0;
  61.  
  62. for(int column = piece.column - 1; column <= piece.column + 1; column++){
  63. for(int row = piece.row - 1; row <= piece.row + 1; row++){
  64. Piece adjacent = this.getPiece(row, column);
  65.  
  66. if(adjacent != null && (row != piece.row || column != piece.column)){
  67. if(exclude != null && exclude.contains(adjacent)) continue;
  68. adjacents[index++] = adjacent;
  69. }
  70. }
  71. }
  72.  
  73. if(index < adjacents.length) adjacents[index] = null;
  74. }
  75.  
  76. public void permutate(ArrayList<Piece> head, Piece piece){
  77. if(head.size() == 0) head.add(piece);
  78. this.fillAdjacents(piece, adjacents, head);
  79.  
  80. head.add(null);
  81. int index = 0, last = head.size() - 1;
  82.  
  83.  
  84. while(index < adjacents.length && adjacents[index] != null){
  85. Piece adjacent = adjacents[index++];
  86.  
  87. head.set(last, adjacent);
  88. permutate(head, adjacent);
  89. }
  90.  
  91. head.remove(last);
  92. }
  93.  
  94. private boolean search(ArrayList<Piece> head, String word, Piece piece, int index){
  95. if(index == word.length()) return true;
  96. if(head.size() == 0) head.add(piece);
  97. Piece[] adjacents = new Piece[8];
  98. char cmp = word.charAt(index);
  99. this.fillAdjacents(piece, adjacents, head);
  100. head.add(null);
  101. int i = 0, last = head.size() - 1;
  102.  
  103. while(i < adjacents.length && adjacents[i] != null){
  104. Piece p = adjacents[i++];
  105.  
  106. if(p.chr == cmp){
  107. head.set(last, p);
  108. if(search(head, word, p, index + 1)) return true;
  109. }
  110. }
  111.  
  112. head.remove(last);
  113. return false;
  114. }
  115.  
  116. private final ArrayList<Piece> test = new ArrayList<Piece>();
  117. public boolean search(String word){
  118. word = word.trim().toLowerCase();
  119. char first = word.charAt(0);
  120.  
  121. for(int r = 0; r < grid.length; r++){
  122. for(int c = 0; c < grid[r].length; c++){
  123. Piece p = this.grid[r][c];
  124.  
  125. if(p.chr == first) test.clear();
  126. if(p.chr == first && this.search(test, word, p, 1)) return true;
  127. }
  128. }
  129.  
  130. return false;
  131. }
  132.  
  133. public static void main(String[] args) {
  134. WordSolver solver = new WordSolver(new char[][]{
  135. {'A', 'M', 'A', 'C'},
  136. {'L', 'T', 'I', 'I'},
  137. {'L', 'N', 'A', 'D'},
  138. {'Y', 'R', 'İ', 'Y'}
  139. });
  140. Locale.setDefault(locale);
  141.  
  142. int count = 0;
  143. long start = System.currentTimeMillis();
  144. try {
  145. BufferedReader dict = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Yengas\\Desktop\\WordList\\turkishWords.lst"), "UTF-8"));
  146. String line = null;
  147.  
  148. while((line = dict.readLine()) != null){
  149. if(solver.search(line)){ System.out.println(line); count++; }
  150. }
  151.  
  152. dict.close();
  153. } catch (Exception e) {
  154. e.printStackTrace();
  155. }
  156. System.out.println("Took: " + (System.currentTimeMillis() - start) + "ms");
  157. System.out.println("Found: " + count);
  158. }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement