未从字典返回有效单词

xt0899hw  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(395)

下面是我的代码片段:package com.dynamicprogramming;

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class BoogleProblem {
  4. public static int[] Path_Row = {0, 0, 1, 1, -1, 1, -1, -1};
  5. public static int[] Path_Col = {1, -1, -1, 1, 1, 0, 0, -1};
  6. public static void findWord(char[][] board, boolean[][] visited, int row, int col, String word,
  7. List<String> englishDictionary) {
  8. if (englishDictionary.contains(word)) {
  9. System.out.println(word);
  10. }
  11. if (board.length == word.length()) {
  12. return;
  13. }
  14. visited[row][col] = true;
  15. for (int index = 0 ; index < board.length; index++) {
  16. int rowNew = row + Path_Row[index];
  17. int colNew = col + Path_Col[index];
  18. if (ifValid(rowNew, colNew, visited)) {
  19. visited[rowNew][colNew] = true;
  20. findWord(board, visited, rowNew, colNew, word + board[rowNew][colNew], englishDictionary);
  21. // Backtracking logic goes here
  22. visited[rowNew][colNew] = false;
  23. }
  24. }
  25. }
  26. public static boolean ifValid(int row, int col, boolean[][] visited) {
  27. if ((row >= 0) && (row < 3) && (col >= 0) && (col < 3) && !visited[row][col]) {
  28. return true;
  29. } else {
  30. return false;
  31. }
  32. }
  33. public static void main(String[] args) {
  34. char[][] board = {{'G', 'I', 'Z'},
  35. {'U', 'E', 'K'},
  36. {'Q', 'S', 'E'}};
  37. boolean[][] visited = new boolean[board.length][board[0].length];
  38. for (int i = 0; i < board.length ; i++) {
  39. for (int j = 0; j < board[0].length; j++) {
  40. visited[i][j] = false;
  41. }
  42. }
  43. List<String> englishDictionary = new ArrayList<String>();
  44. englishDictionary.add("GEEKS");
  45. englishDictionary.add("QUIZ");
  46. englishDictionary.add("FOR");
  47. englishDictionary.add("GO");
  48. String word = "";
  49. for (int i = 0; i < board.length; i++) {
  50. for (int j = 0 ; j < board[0].length; j++) {
  51. findWord(board, visited, 0, 0, word + board[i][j], englishDictionary);
  52. }
  53. }
  54. }
  55. }

这是行不通的&什么也没做。正如我所观察到的,在字符串多于2个字符的大多数情况下,ifvalid()方法都返回false。请告诉我,这里出了什么问题。

fdbelqdn

fdbelqdn1#

试试这个。

  1. public static int[] Path_Row = {0, 0, 1, 1, -1, 1, -1, -1};
  2. public static int[] Path_Col = {1, -1, -1, 1, 1, 0, 0, -1};
  3. public static void findWord(char[][] board, boolean[][] visited, int row, int col, String word,
  4. List<String> englishDictionary) {
  5. if (englishDictionary.contains(word)) {
  6. System.out.println(word);
  7. }
  8. // if (board.length == word.length()) { // DELETE
  9. // return; // DELETE
  10. // } // DELETE
  11. visited[row][col] = true;
  12. // for (int index = 0 ; index < board.length; index++) { // CHANGE
  13. for (int index = 0 ; index < Path_Row.length; index++) { // CHANGE
  14. int rowNew = row + Path_Row[index];
  15. int colNew = col + Path_Col[index];
  16. if (ifValid(rowNew, colNew, visited)) {
  17. visited[rowNew][colNew] = true;
  18. findWord(board, visited, rowNew, colNew, word + board[rowNew][colNew], englishDictionary);
  19. // Backtracking logic goes here
  20. visited[rowNew][colNew] = false;
  21. }
  22. }
  23. visited[row][col] = false; // ADD
  24. }
  25. public static boolean ifValid(int row, int col, boolean[][] visited) {
  26. if ((row >= 0) && (row < 3) && (col >= 0) && (col < 3) && !visited[row][col]) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32. public static void main(String[] args) {
  33. char[][] board = {{'G', 'I', 'Z'},
  34. {'U', 'E', 'K'},
  35. {'Q', 'S', 'E'}};
  36. boolean[][] visited = new boolean[board.length][board[0].length];
  37. for (int i = 0; i < board.length ; i++) {
  38. for (int j = 0; j < board[0].length; j++) {
  39. visited[i][j] = false;
  40. }
  41. }
  42. List<String> englishDictionary = new ArrayList<String>();
  43. englishDictionary.add("GEEKS");
  44. englishDictionary.add("QUIZ");
  45. englishDictionary.add("FOR");
  46. englishDictionary.add("GO");
  47. String word = "";
  48. for (int i = 0; i < board.length; i++) {
  49. for (int j = 0 ; j < board[0].length; j++) {
  50. // findWord(board, visited, 0, 0, word + board[i][j], englishDictionary); // CHANGE
  51. findWord(board, visited, i, j, word + board[i][j], englishDictionary); // CHANGE
  52. }
  53. }
  54. }

输出

  1. GEEKS
  2. QUIZ
展开查看全部

相关问题