【问题标题】:Find complete word inside a two-dimensional array在二维数组中查找完整的单词
【发布时间】:2020-12-27 21:35:58
【问题描述】:

我有一个艰巨的任务。当它们在二维数组中匹配时,我需要将单词写入“stringBuilder”单元格。这意味着我必须找到避免重复单元格的完整单词。 为了更好的理解,我会附上黄色单元格中正确遍历的照片,其中红色单元格是错误的路径。

结论应该是:[0,2]->[1,2]->[2,2]->[2,3]->[2,4]->[3 ,4]->[4,4]->[5,4]->[5,3]->[5,2]->[5,1]->[4,1]->[4, 0]->[5,0] 现在我的结论是:[5, 0]->[4, 0]->[4, 1]->[4, 2]->[3, 2]->[3, 1]->[2, 1]->[2, 2]->[1, 2]

我不明白故障在哪里以及如何解决,请帮忙。

我的代码:

public class GFS {
    private static int R;
    private static int C;
    private static int[] x = {-1, 0, 1, 0};
    private static int[] y = {0, 1, 0, -1};
    private static StringBuilder stringBuilder = new StringBuilder();
    private static int indexForWord = 1;

    public static void main(String[] args) {
        R = 7;
        C = 7;
        /*String word = "BOBA";
        String cross = "QWBOABOBGSBSERTY";*/
        /*String word = "KING";
        String cross = "QLGNAEKIRLRNGEAE";*/
        /*String word = "APPLE";
        String cross = "UKJVXNAPBXELPLHVNLDKBVVNM";*/
        String word = "DISABILITATING";
        String cross = "FBDHBAAGNITISTDASABIDDBITILBNILALASGTATIGIYGNTGND";
        char[][] grid = createMatrix(cross);

        search2D(grid, word, 2, 0);
        System.out.println(stringBuilder.toString());
    }

    static char[][] createMatrix(String input) {
        char[][] newArr = new char[R][C];
        int index = 0;
        for (int i = 0; i < newArr.length; i++) {
            for (int j = 0; j < newArr.length; j++) {
                newArr[i][j] = input.charAt(index++);
            }
        }
        return newArr;
    }

    static void print(char[][] grid) {
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid.length; j++) {
                System.out.print(grid[i][j] + "  ");
            }
            System.out.println();
        }
    }

    static boolean search2D(char[][] grid, String word, int positionX, int positionY) {
        char oldChar = grid[positionY][positionX];

        if (indexForWord >= word.length()) {
            return true;
        }
        int top = positionY - 1 < 0 ? positionY : positionY - 1;
        int bottom = positionY + 1 >= grid.length ? positionY : positionY + 1;
        int right = positionX + 1 >= grid.length ? positionX : positionX + 1;
        int left = positionX - 1 < 0 ? positionX : positionX - 1;

        if (grid[top][positionX] == word.charAt(indexForWord)) {
            indexForWord++;
            grid[positionY][positionX] = ' ';
            boolean check = search2D(grid, word, positionX, top);
            if (check) {
                stringBuilder.append("[").append(top).append(", ").append(positionX).append("]");
                return true;
            } else {
                for (int j = indexForWord; j >= 0; j--) {
                    if (word.charAt(j) == oldChar) {
                        indexForWord = j + 1;
                        grid[positionY][positionX] = oldChar;
                        break;
                    }
                }
            }
        }
        if (grid[bottom][positionX] == word.charAt(indexForWord)) {
            indexForWord++;
            grid[positionY][positionX] = ' ';
            boolean check = search2D(grid, word, positionX, bottom);
            if (check) {
                stringBuilder.append("[").append(bottom).append(", ").append(positionX).append("]");
                return true;
            } else {
                for (int j = indexForWord; j >= 0; j--) {
                    if (word.charAt(j) == oldChar) {
                        indexForWord = j + 1;
                        grid[positionY][positionX] = oldChar;
                        break;
                    }
                }
            }
        }
        if (grid[positionY][left] == word.charAt(indexForWord)) {
            indexForWord++;
            grid[positionY][positionX] = ' ';
            boolean check = search2D(grid, word, left, positionY);
            if (check) {
                stringBuilder.append("[").append(positionY).append(", ").append(left).append("]");
                return true;
            } else {
                for (int j = indexForWord; j >= 0; j--) {
                    if (word.charAt(j) == oldChar) {
                        indexForWord = j + 1;
                        grid[positionY][positionX] = oldChar;
                        break;
                    }
                }
            }
        }
        if (grid[positionY][right] == word.charAt(indexForWord)) {
            indexForWord++;
            grid[positionY][positionX] = ' ';
            boolean check = search2D(grid, word, right, positionY);
            if (check) {
                stringBuilder.append("[").append(positionY).append(", ").append(right).append("]");
                return true;
            } else {
                for (int j = indexForWord; j >= 0; j--) {
                    if (word.charAt(j) == oldChar) {
                        indexForWord = j + 1;
                        grid[positionY][positionX] = oldChar;
                        break;
                    }
                }
            }
        }
        return false;
    }
}

【问题讨论】:

    标签: java arrays search


    【解决方案1】:

    这是因为indexForWord 在从错误的路径回溯时没有设置正确并且有重复的字符(在你的情况下,它是 T)

    else {
        for (int j = indexForWord; j >= 0; j--) {
            if (word.charAt(j) == oldChar) {
               indexForWord = j + 1;
               grid[positionY][positionX] = oldChar;
               break;
            }
    }
    

    相反,每次check 为假时(在所有 4 种情况下)只需后退一次就足够了:

    else {
           grid[positionY][positionX] = oldChar;
           indexForWord--;
         }
    

    结果也将是相反的([5, 0] -> [4, 0] -> [4, 1] -> [5, 1] 等),因此您必须将其反转或找出另一种方法。

    【讨论】:

      【解决方案2】:

      除了 Bahij 所说的:

          private static int indexForWord = 1;
      

      应该用 0 而不是 1 初始化。

      结合 Bahij 的解决方案,得到

      [5, 0][4, 0][4, 1][5, 1][5, 2][5, 3][5, 4][4, 4][3, 4][2, 4][2, 3][2, 2][1, 2][0, 2]
      

      (很遗憾,我无法评论他/她的回答,因为我没有足够的声誉哈哈)

      【讨论】:

        猜你喜欢
        • 2020-07-16
        • 2021-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 2017-07-22
        相关资源
        最近更新 更多