0

现在是我为你祖母编写她的第一个 Java 单词搜索程序的时候了。但不是让她通过在字母网格中查找单词来完成所有工作,而是使用递归函数4WaySearch为她做这件事!

唯一的问题是:

我发现很难概念化一个递归算法,该算法在网格中一次开始时构建每个可能的字母组合。

这是我已经编写的代码,我认为这是朝着正确方向迈出的一大步:

/* 
* This is the method that calls itself repeatedly to wander it's way
* through the grid using a 4 way pattern,
* creating every possibly letter combination and checking it against a
* dictionary. If the word is found in the dictionary, it gets added to a
* collection of found words.
* 
* Here an example of a 3x3 grid with the valid words of RATZ and BRATZ, but
* the word CATZ isn't valid. (the C is not tangent to the A).
* 
* CXY
* RAT
* BCZ
*
* @param row Current row position of cursor
* @param col Current column position of cursor
*/
private void 4WaySearch(int row, int col) {

    // is cursor outside grid boundaries?
    if (row < 0 || row > ROWS - 1 || col < 0 || col > COLS - 1)
        return; 

    GridEntry<Character> entry = getGridEntry(row, col);

    // has it been visited?
    if (entry.hasBreadCrumb())
        return; 

    // build current word
    currentWord += entry.getElement(); // returns character

    // if dictionay has the word add to found words list
    if (dictionary.contains(currentWord))
        foundWords.add(currentWord);

    // add a mark to know we visited
    entry.toggleCrumb();

    // THIS CANT BE RIGHT
    4WaySearch(row, col + 1);   // check right
    4WaySearch(row + 1, col);   // check bottom
    4WaySearch(row, col - 1);   // check left
    4WaySearch(row - 1, col);   // check top

    // unmark visited
    entry.toggleCrumb();

    // strip last character
    if (currentWord.length() != 0)
        currentWord = currentWord.substring(
        0, 
        (currentWord.length() > 1) ? 
            currentWord.length() - 1 : 
            currentWord.length()
        );
}

在我的脑海中,我将搜索算法可视化为递归树遍历算法,但每个节点(本例中的条目)有 4 个子节点(切线条目),叶节点是网格的边界。

此外,初始进入函数时光标的位置由此处伪编码的简单 for 循环确定:

for (int r = 0; r < ROWS; r++)
  for (int c = 0; r < COLS; c++)
    4WaySearch(r,c);
  end for;
end for;

我一直在考虑这个问题,并尝试了不同的方法......但我似乎仍然无法将我的思想包裹在它周围并让它发挥作用。有人可以给我看灯吗?(为了我和你的祖母!:D)

4

3 回答 3

0

所以你需要做的是首先建立网格。在这种情况下,您选择了 3x3 。您需要的是一种跟踪网格上所有有效点的方法,我是否可以推荐一个名为的对象,该对象Point以两个ints 作为其构造函数。接下来需要的是一个由 aPoint和 a组成的类char,这将使我们能够看到每个可用的字母Point

现在我们已经有了数据结构,我们需要跟踪游戏的有效动作。例如,如果我在位置 3,3(右下角,或者如果你是从零开始的 2,2),我需要意识到我唯一有效的移动是向上或向左。确定这一点的方法是保留 a SetofPoint告诉我我去过的所有地方,这将使递归算法终止。

一些可能有帮助的递归伪代码

if(!validMovesRemaining)  
     return
else  
    removeValidPosition(pointToRemove);  
    captureLetter(composedObject);
    recurse(pointsRemaining);
于 2011-10-26T20:29:50.030 回答
0

我要做的是建立一个树结构。像这样定义节点的地方

public class Node {
    private String data;
    private int row,col;
    private Node parent;
    public Node(Node parent,int row,int col) {
        this.parent = parent;
        this.col = col;
        this.row = row;
    }
    public boolean hasParent(int row, int col) {
        Node current = this;
        while((current=current.parent)!=null) {
            if(current.row == row && current.col==col)
                return true;
        }
        return false;
    }

    public String toString() {
        Node current = this;
        StringBuffer sb = new StringBuffer();
        do {
            sb.append(current.data);
        }while((current = current.parent)!=null);
        return sb.reverse().toString();
    }
}

每次你有一个新的起点时,你都想为树创建一个新的根节点

for (int r = 0; r < ROWS; r++)
  for (int c = 0; r < COLS; c++)
    4WaySearch(new Node(null,r,c);   //it is the root and has no parent
  end for;
end for;

然后你想在你去的时候建造树

private void FourWaySearch(Node c) {
    if (c.row < 0 || c.row > ROWS - 1 || c.col < 0 || c.col > COLS - 1 || c.hasParent(c.row,c.col))
        return; 
    else {  

        c.data = grid[c.row][c.col];  //get the string value from the word search grid
        if(dictionary.contains(c.toString()))
            foundWords.add(c.toString());
        FourWaySearch(new Node(c,c.row-1,c.col));
        FourWaySearch(new Node(c,c.row,c.col-1));
        FourWaySearch(new Node(c,c.row+1,c.col));
        FourWaySearch(new Node(c,c.row,c.col+1));
    }
}

这可能不是最好的方法,但对我来说它似乎简单易懂。

于 2011-10-26T20:57:24.753 回答
0

我认为一棵树是要走的路,但不是其他答案似乎使用它的方式。我要做的是为您要查找的单词(来自字典)构建一个解析树——每个字母都是一个节点,有 26 个可能的子节点,每个字母对应一个(在递归之前null检查子节点),以及一个标志,说明它是否是一个完整的单词。检查每个方向,看看你是否可以朝那个方向移动,然后相应地移动。

我要说困难的部分是构建树,它不应该递归地完成,但我只是有了另一个想法。构建树的最佳方式也是递归的。

这显然只是一个概述,但希望足以让您开始。如果您再次陷入困境,请发表评论,我会看看我是否可以将您推向正确的方向。

于 2011-10-26T21:54:41.530 回答