【问题标题】:Java recursive method for word matching用于单词匹配的Java递归方法
【发布时间】:2016-09-15 14:29:13
【问题描述】:

我一直在拼命想弄清楚为什么这个递归方法在匹配用户提供的单词时没有返回 true。

我正在用这个逻辑创建一个二维数组:

charTest = letterString.toCharArray();

    char[][] twoDimCharArray = new char[][] 
            {{charTest[0],charTest[1],charTest[2],charTest[3]},
            {charTest[4],charTest[5],charTest[6],charTest[7]},
            {charTest[8],charTest[9],charTest[10],charTest[11]},
            {charTest[12],charTest[13],charTest[14],charTest[15]}};

用户提供的字符串被传递到下面的方法中,如果它检查二维数组并在相邻位置找到字符串的每个字符,它将从主方法返回 true:

public boolean findWord(String word) {
    for (int row = 0; row < this.board2.length; row++) {
        for (int col = 0; col < this.board2.length; col++) {
            if (this.findWord(word, row, col)) {
                return true;
            }
        }
    }
    return false;
}

private boolean findWord(String word, int row, int col) {
   if (    row < 0 || row >= this.board2.length ||
           col < 0 || col >= this.board2.length ||
           this.board2[row][col] != word.charAt(0)) {
        return false;
    }
    else {
        char safe = this.board2[row][col];
        this.board2[row][col] = '*';
        String rest = word.substring(1, word.length());
       Log.v("rest", rest + "");
        boolean result = this.findWord(rest, row-1, col-1) ||
                this.findWord(rest, row-1,   col) ||
                this.findWord(rest, row-1, col+1) ||
                this.findWord(rest,   row, col-1) ||
                this.findWord(rest,   row, col+1) ||
                this.findWord(rest, row+1, col-1) ||
                this.findWord(rest, row+1,   col) ||
                this.findWord(rest, row+1, col+1);
        this.board2[row][col] = safe;
        return result;
    }
}

然而,无论字符的位置如何,该方法总是返回 false。当我在调试时,它似乎确实传递了数组中的每个位置,但没有识别出第一个字符的匹配项并开始检查第二个字符。有什么突出的地方吗?

【问题讨论】:

  • 我猜this.board2 在递归调用之间不正确地共享并且应该被复制以便每个分支都有自己的版本,而不是已经被相邻分支搞砸的版本。
  • 你做了什么来调试这个?至少,在每个例程的顶部和底部插入打印语句以打印调用参数和返回状态。这可能会发现问题;如果没有,它将帮助我们向我们展示在哪里寻找。

标签: java android arrays recursion


【解决方案1】:

问题是您缺少一个正递归终止案例。已修改findWord()

private boolean findWord(String word, int row, int col) {

    if (row < 0 || row >= this.board2.length ||
        col < 0 || col >= this.board2.length ||
        this.board2[row][col] != word.charAt(0)) {
        return false;
    }

    if (word.length() == 1) {
        return true;
    }

    String rest = word.substring(1, word.length());

    char saved = this.board2[row][col];
    this.board2[row][col] = '*';

    boolean result = this.findWord(rest, row-1, col-1) ||
        this.findWord(rest, row-1,   col) ||
        this.findWord(rest, row-1, col+1) ||
        this.findWord(rest,   row, col-1) ||
        this.findWord(rest,   row, col+1) ||
        this.findWord(rest, row+1, col-1) ||
        this.findWord(rest, row+1,   col) ||
        this.findWord(rest, row+1, col+1);

    this.board2[row][col] = saved;

    return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    相关资源
    最近更新 更多