簡體   English   中英

我的算法無法解決轉向板問題

[英]Trouble with my algorithm to solve a boggle board

因此,我對Java相對較新(目前我正在學校學習AP Java),並且我正在嘗試開發一種遞歸算法來解決n * n電路板,我覺得我已經很接近了,但是還不很完善。 我已寫出所有內容來遍歷字典,以查找要發送的字母是否是單詞等。我的算法是在數組中將起始字母設為(n,p),然后將這些坐標發送給另一種方法全方位尋找所有可能的組合。 一旦找到以(n,p)開頭的所有組合,我將遞增p直到它到達行的末尾,然后我將遞增n並再次從0開始p。 (我只瀏覽一半字母,因為組合的前后方向相同)

我遇到的問題是遞歸序列,因為一旦我超過了板上的某個位置,我就希望對其進行標記,以確保在序列的其余部分中再也不會重復它。 它不能完全正常工作,我想知道是否有人可以告訴我為什么/幫助我編寫更好的算法。 提前致謝

public void AllLetters(int n, int p, int x, int y,String word, String MyLetteres[][]){
    int temp=0;
    int StartLetter =(int)(Math.pow(MyLetteres.length,2));
    while(temp<StartLetter)//runs through every letter
    {   
        if(temp==0)
            getPaths(p, n,x,y,word, MyLetteres);
        else if(temp%(MyLetteres.length-1)==temp){
            getPaths(p, n+1,x,y,word, MyLetteres);
        }
        else {
            getPaths(p+1, 0,x,y,word, MyLetteres);
        }
        if(temp==(StartLetter/2-1)){
            temp=StartLetter;
        }
        temp++;
    }

}


public void getPaths(int p, int n, int x, int y,String word, String MyLetteres[][]){
    if( x ==p-1 && y == n-1){//reach the (n,p) point
    System.out.print("");
    }else if( x >= MyLetteres.length || y >= MyLetteres.length||x < 0 || y < 0){//out of bounds
    return;
    }else {
        if(x+1<MyLetteres.length&&!MyLetteres[x+1][y].equals("0")){//up{
            word=word+""+MyLetteres[x+1][y];
            Check(word);//function that checks if it is a word
            reverse(word);//checks its a word backwards (for efficenicy)
            MyLetteres[x+1][y]="0";//marking that I've used this position
            System.out.print("1");//debugging purposes
            getPaths(n,p, x +1, y,word , MyLetteres);
        }
        if(x-1>0&&!MyLetteres[x-1][y].equals("0")){//down
            word=word+""+MyLetteres[x-1][y];
            Check(word);
            reverse(word);
            MyLetteres[x-1][y]="0";
            System.out.print("2");
            getPaths(n,p, x -1, y ,word, MyLetteres);
        }
        if(y+1<MyLetteres.length&&!MyLetteres[x][y+1].equals("0")){//right
            word=word+""+MyLetteres[x][y+1];
            Check(word);
            reverse(word);
            MyLetteres[x][y+1]="0";
            System.out.print("3");
            getPaths(n, p,x , y +1,word, MyLetteres);
        }
        if(y-1>0&&!MyLetteres[x][y-1].equals("0")){//left
            word=word+""+MyLetteres[x][y-1];
            Check(word);
            reverse(word);
            MyLetteres[x][y-1]="0";
            System.out.print("4");
            getPaths(n,p, x , y -1,word, MyLetteres);
        }
        if(x+1<MyLetteres.length&&y+1<MyLetteres.length&&!MyLetteres[x+1][y+1].equals("0")){//right, up
            word=word+""+MyLetteres[x+1][y+1];
            Check(word);
            reverse(word);
            MyLetteres[x+1][y+1]="0";
            System.out.print("5");
            getPaths(n,p, x +1, y +1,word, MyLetteres);
        }
        if(x-1>0&&y-1>0&&!MyLetteres[x-1][y-1].equals("0")){//down, left
            word=word+""+MyLetteres[x-1][y-1];
            Check(word);
            reverse(word);
            MyLetteres[x-1][y-1]="0";
            System.out.print("6");
            getPaths(n,p, x-1 , y -1,word, MyLetteres);
        }
        if(x-1>0&&y+1<MyLetteres.length&&!MyLetteres[x-1][y+1].equals("0")){//down, right
            word=word+""+MyLetteres[x-1][y+1];
            Check(word);
            reverse(word);
            MyLetteres[x-1][y+1]="0";
            System.out.print("7");
            getPaths(n,p, x+1, y-1, word,MyLetteres);
        }
        if(x+1<MyLetteres.length&&y-1>0&&!MyLetteres[x+1][y-1].equals("0")){//up, left
            word=word+""+MyLetteres[x+1][y-1];
            Check(word);
            reverse(word);
            MyLetteres[x+1][y-1]="0";
            System.out.print("8");
            getPaths(n, p,x-1 , y +1, word,MyLetteres);
        }
    }
}

您可以在MyLetteres寫入0,以防止遞歸循環回到自身。 但是,一旦遞歸調用返回,則需要恢復該位置的原始字母。 否則,搜索只能嘗試遍歷所有分支的每個位置一次。

(此外,您可以通過遍歷(x,y)偏移量列表而不是為每個偏移量使用單獨的if語句而大大簡化代碼)

編輯:

int[][] offsets = { {-1, -1}, {0, -1}, {1, -1}, 
                    {-1,  0},          {1,  0}, 
                    {-1,  1}, {0,  1}, {1,  1} };

for(int[] off : offsets) {
   nx = x + off[0];
   ny = y + off[1];
   if(nx < 0 || ny < 0 || nx >= MyLetteres.length || ny >= MyLetteres[nx].length) {
      continue;
   }
   String letter = MyLetteres[nx][ny];
   if(letter.equals("0")) {
      continue;
   }
   MyLetteres[nx][ny] = "0";
   Check(word + letter);
   reverse(word + letter);
   getPaths(n,p, nx, ny, word + letter, MyLetteres);
   MyLetteres[nx][ny] = letter;
}

暫無
暫無

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

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