簡體   English   中英

查找單詞字母網格

[英]Find word letter grid

我正在研究Boggle游戲,並且正在創建一個名為findWord的方法,如果可以在“網格”中找到“ word”,則該方法返回true。 返回false,否則私有成員變量grid具有字母網格。 但是,當我運行我的主要方法時,它會一直打印“未找到”,並且我無法弄清楚我在哪里出錯了! 這是我的代碼

  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;
    }


}

我看到的一個問題是您兩次調用this.find(curr, row+1, col) ,第二個應該是this.find(curr, row+1, col+1) 這將使您無法對角向下/向右檢查,而看不到測試用例,我無法說這是否實際上導致它總是失敗。

您可能會發現這很有趣,它會水平,垂直和對角地找到單詞(但不是反方向):

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;
}

編輯:也可以沿相反方向查找字符串:

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在這里不支持將布爾值隱式轉換為int(例如C或C ++一樣-哦,C#可以嗎?),否則我們可以這樣寫:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM