だから私はJavaに比較的慣れていないので(現在学校でAP Javaを取っています)、n * nボードを解決するための再帰アルゴリズムを開発しようとしています。送信している文字が単語であるかどうかなどを調べるために辞書を走査するためにすべて書き出されています。可能なすべての組み合わせを見つけるためにあらゆる方向に進みます。(n,p) で始まるすべての組み合わせが見つかったら、行の最後に到達するまで p をインクリメントし、n をインクリメントして p を 0 から再び開始します。(組み合わせは前後同じなので半分しか通らない)
私が問題を抱えている部分は、再帰シーケンスです。ボード上の特定の位置に移動したら、シーケンスの残りの部分で二度と移動しないようにマークしたいからです。それは完全には機能しません。誰かが理由を教えてくれたり、より良いアルゴリズムを書くのを手伝ってくれないかと思っていました。前もって感謝します
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);
}
}
}