找到单词字母网格

时间:2016-07-15 15:30:50

标签: java recursion boggle

我正在处理Boggle游戏,我正在创建一个名为findWord的方法,如果" word"可以在" grid"中找到。返回false否则私有成员变量网格具有字母网格。但是,当我运行我的主要方法时,它会一直打印出来"未找到"而且我无法弄清楚我犯了什么错误!这是我的代码

  public class BoggleGame_old {
  LetterGrid grid;
  private char[][]board;
  boolean[][] visited;
 public BoggleGame_old(LetterGrid g) 
{
    grid = g;
}
public boolean findWord(String word) {

    for(int row=0;row<this.board.length;row++){
        for(int col=0;col<this.board.length;col++){
            if(this.find(word, row, col)){
                return true;
            }
        }
    }
    return false; 
}
   //helping function
   private boolean find(String word, int row, int col){
    if(word.equals(""))
    {
        return true;
    }
    else if(row<0||row>=this.board.length||

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

    }
    else{
        char c=this.board[row][col];
        this.board[row][col]='*';
        String curr=word.substring(1,word.length());
        boolean res=this.find(curr, row-1, col-1)||
                this.find(curr, row-1, col)||
                this.find(curr, row-1, col+1)||
                this.find(curr, row, col-1)||
                this.find(curr, row, col+1)||
                this.find(curr, row+1, col-1)||
                this.find(curr, row+1, col)||
                this.find(curr, row+1, col);
             this.board[row][col]=c;
             return res;
    }


}

2 个答案:

答案 0 :(得分:0)

我看到的一个问题是您拨打this.find(curr, row+1, col)两次,第二个应该是this.find(curr, row+1, col+1)。它会阻止你对角线向下/向右检查,而不会看到测试用例我无法说出它是否真的导致它始终失败。

答案 1 :(得分:0)

你可能会发现这很有趣,它可以水平,垂直和对角地找到单词(但不是反方向):

public boolean findWord(String word)
{
    if(word == null || word.isEmpty())
        return true;
    int rowMax = board.length - word.length();
    int colMax = board[0].length - word.length();
    if(rowMax < 0 || colMax < 0)
        return false;
    for (int row = 0; row < rowMax; ++row)
    {
        for (int col = 0; col < colMax; ++col)
        {
            boolean v = true;
            boolean h = true;
            boolean d = true;
            for(int c = 0; c < word.length(); ++c)
            {
                v &= board[row + c][col] == word.charAt(c);
                h &= board[row][col + c] == word.charAt(c);
                d &= board[row + c][col + c] == word.charAt(c);
                if(!(v | h | d))
                    break;
            }
            if(v|h|d)
                return true;
        }
    }
    return false;
}

编辑: Variant在相反方向查找字符串:

public boolean findWord(String word)
{
    if(word == null || word.isEmpty())
        return true;
    int rowMax = board.length - word.length();
    int colMax = board[0].length - word.length();
    if(rowMax < 0 || colMax < 0)
        return false;
    StringBuilder reverse = new StringBuilder(word).reverse();
    for (int row = 0; row < rowMax; ++row)
    {
        for (int col = 0; col < colMax; ++col)
        {
            boolean v = true;
            boolean h = true;
            boolean d = true;
            boolean rv = true;
            boolean rh = true;
            boolean rd = true;
            for(int c = 0; c < word.length(); ++c)
            {
                v &= board[row + c][col] == word.charAt(c);
                h &= board[row][col + c] == word.charAt(c);
                d &= board[row + c][col + c] == word.charAt(c);
                rv &= board[row + c][col] == reverse.charAt(c);
                rh &= board[row][col + c] == reverse.charAt(c);
                rd &= board[row + c][col + c] == reverse.charAt(c);
                if(!(v | h | d | rv | rh | rd))
                    break;
            }
            if(v | h | d | rv | rh | rd)
                return true;
        }
    }
    return false;
}

编辑2:这么多布尔人...... - 有点紧张:

int flags = 0;
for(int c = 0; flags != 0b111111 && c < word.length(); ++c)
{
    flags |= board[row + c][col    ] == word.charAt(c)    ? 0 : 1 << 0;
    flags |= board[row    ][col + c] == word.charAt(c)    ? 0 : 1 << 1;
    flags |= board[row + c][col + c] == word.charAt(c)    ? 0 : 1 << 2;
    flags |= board[row + c][col    ] == reverse.charAt(c) ? 0 : 1 << 3;
    flags |= board[row    ][col + c] == reverse.charAt(c) ? 0 : 1 << 4;
    flags |= board[row + c][col + c] == reverse.charAt(c) ? 0 : 1 << 5;
 }
 if(flags != 0b111111)
     return true;

遗憾的是,Java不支持在这里隐式地将boolean转换为int(例如C或C ++吗 - 哦,顺便说一下,C#?),否则我们可以写:

flags |= (a == b) << n;