Java中的递归搜索
作者:互联网
所以我一直在为游戏设计一个程序.我创建了一个小板供用户使用,但是问题是我不知道如何递归检查该单词是否在板上.我希望能够检查输入的单词是否确实在黑板上并且有效.我的意思是,单词的字母必须彼此相邻.对于那些玩过惊奇的人,您会明白我的意思.我要做的就是检查单词是否在板上.
这是我到目前为止所拥有的….
import java.io.*;
public class boggle {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
private String s = "";
private int [][] lettersNum = new int [5][5];
private char [][] letters = new char [5][5];
private char [] word = new char [45]; // Size at 45, because the longest word in the dictionary is only 45 letters long
private char [] temp;
public void generateNum()
{
for (int row = 0; row < 5; row ++)
{
for (int col = 0; col < 5; col++)
{
lettersNum [row][col] = (int) (Math.random() * 26 + 65);
}
}
}
public void numToChar()
{
for (int row = 0; row < 5; row ++)
{
for (int col = 0; col < 5; col++)
{
letters [row][col] = (char)(lettersNum[row][col]);
}
}
}
public void display()
{
for (int row = 0; row < 5; row ++)
{
for (int col = 0; col < 5; col++)
{
System.out.print(letters[row][col]);
}
System.out.println("");
}
}
public void getInput() throws IOException
{
System.out.println("Please enter a word : ");
s=br.readLine();
s=s.toUpperCase();
word = s.toCharArray();
}
public int search(int row, int col)
{
if((row <0) || (row >= 5) || (col < 0) || (col >= 5))
{
return (0);
}
else
{
temp = word;
return (1+ search(row +1, col) +
search(row -1, col) +
search(row, col + 1) +
search(row, col-1) +
search(row +1, col +1)+
search(row +1, col -1)+
search(row -1, col +1)+
search(row -1, col -1));
}
}
}
搜索是我的搜索算法,用于检查单词是否在面板上,但我不知道它是否正确或是否可以工作.此外,我不知道如何实际告诉用户该单词是有效的!
感谢所有的帮助:)
因此,我尝试使用您在下面提出的建议,但我不太了解int [5] [5].所以这是我尝试过的方法,但是我总是越界错误!这是酱汁…
public void locate()
{
temp = word[0];
for (int row = 0; row <5; row++)
{
for (int col = 0; col <5; col++)
{
if(temp == letters[row][col])
{
search(row,col);
}
}
}
}
public int search(int row, int col)
{
if(letters[row][col-1]==word[count]) // Checks the letter to the left
{
count++;
letters[row][col-1] = '-'; // Just to make sure the program doesn't go back on itself
return search(row, col-1);
}
else if (letters[row][col+1] == word[count])// Checks the letter to the right
{
count++;
letters[row][col+1] = '-';// Just to make sure the program doesn't go back on itself
return search(row, col +1);
}
else if (letters[row+1][col]== word[count])// Checks the letter below
{
count++;
letters[row+1][col] = '-';// Just to make sure the program doesn't go back on itself
return search(row +1 , col);
}
else if (letters[row-1][col]== word[count])// Checks the letter above
{
count++;
letters[row-1][col] = '-';// Just to make sure the program doesn't go back on itself
return search(row -1 , col);
}
else if (letters[row-1][col-1]== word[count])// Checks the letter to the top left
{
count++;
letters[row-1][col-1] = '-';// Just to make sure the program doesn't go back on itself
return search(row -1 , col-1);
}
else if (letters[row-1][col+1]== word[count])// Checks the letter to the top right
{
count++;
letters[row-1][col+1] = '-';// Just to make sure the program doesn't go back on itself
return search(row -1 , col+1);
}
else if (letters[row+1][col-1]== word[count])// Checks the letter to the bottom left
{
count++;
letters[row+1][col-1] = '-';// Just to make sure the program doesn't go back on itself
return search(row +1 , col-1);
}
else if (letters[row+1][col+1]== word[count])// Checks the letter to the bottom right
{
count++;
letters[row+1][col+1] = '-';// Just to make sure the program doesn't go back on itself
return search(row +1 , col+1);
}
return 0;
}
private int count = 0; (在类的顶部声明,以防您想知道我从何处获得[count]一词.
解决方法:
您当前的搜索功能实际上没有任何作用.我假设这是家庭作业,所以没有免费的午餐;)
最简单的方法是具有两个递归函数:
public boolean findStart(String word, int x, int y)
这将对木板进行线性搜索,以查找单词中的第一个字符.如果您当前的位置不匹配,请使用下一组坐标给自己打电话.找到匹配项后,它将使用word,当前位置和一个新的空4×4矩阵来调用您的第二个递归函数:
public boolean findWord(String word, int x, int y, int[][] visited)
此功能首先检查当前位置是否与单词中的第一个字母匹配.如果是的话,它将标记访问者的当前位置,并遍历所有相邻的正方形,但通过用word.substring(1)和这些坐标调用自身来标记访问者中的那些除外.如果单词中的字母用完了,您将找到它并返回true.请注意,如果返回的是false,则需要从Visited中删除当前位置.
您可以使用一个功能来执行此操作,但是我认为通过分解逻辑,可以更轻松地进行管理.缺点是它会对单词中的每个第一个字母做额外的比较.要使用单个功能,您需要使用布尔值或深度计数器来跟踪您所处的“模式”.
编辑:最长的单词只能是16.Boggle使用4×4面板,并且一个单词不能两次使用相同的位置.并不是说这真的很重要,但是对于任务来说可能如此.另外请注意,我只是在脑海中做到这一点,不知道我100%正确-评论表示赞赏.
编辑以回应评论:
使用上面概述的方法,这就是您的迭代定位的样子:
public boolean locate(String word)
{
for (int row = 0; row < 4; row++)
{
for (int col =0; col < 4; col++)
{
if (word.charAt(0) == letters[row][col])
{
boolean found = findWord(word, row, col, new boolean[4][4]);
if (found)
return true;
}
}
}
return false;
}
递归相同的内容如下所示,这应该有所帮助:
public boolean findStart(String word, int x, int y)
{
boolean found = false;
if (word.charAt(0) == letters[x][y])
{
found = findWord(word, x, y, new boolean[4][4]);
}
if (found)
return true;
else
{
y++;
if (y > 3)
{
y = 0;
x++;
}
if (x > 3)
return false;
}
return findStart(word, x, y);
}
因此,这里有findWord()和辅助方法getAdjoining(),向您展示了这一切的工作方式.请注意,我将访问数组更改为布尔值,只是因为它很有意义:
public boolean findWord(String word, int x, int y, boolean[][] visited)
{
if (letters[x][y] == word.charAt(0))
{
if (word.length() == 1) // this is the last character in the word
return true;
else
{
visited[x][y] = true;
List<Point> adjoining = getAdjoining(x,y,visited);
for (Point p : adjoining)
{
boolean found = findWord(word.substring(1), p.x, p.y, visited);
if (found)
return true;
}
visited[x][y] = false;
}
}
return false;
}
public List<Point> getAdjoining(int x, int y, boolean[][] visited)
{
List<Point> adjoining = new LinkedList<Point>();
for (int x2 = x-1; x2 <= x+1; x2++)
{
for (int y2 = y-1; y2 <= y+1; y2++)
{
if (x2 < 0 || x2 > 3 ||
y2 < 0 || y2 > 3 ||
(x2 == x && y2 == y) ||
visited[x2][y2])
{
continue;
}
adjoining.add(new Point(x2,y2));
}
}
return adjoining;
}
因此,现在,当您从用户获得String(单词)的输入后,您只需调用:
boolean isOnBoard = findStart(word,0,0);
我最初是在脑海中做到这一点,然后沿着那条路尝试向您展示它是如何工作的.如果我要实际执行此操作,我会做一些不同的事情(主要是消除单词中第一个字母的双重比较,虽然可以通过重新排列当前方法中的逻辑来做到这一点,但这可能是通过将两者合并为一个方法),但是上面的代码确实起作用,并且应该可以帮助您更好地理解递归搜索.
标签:depth-first-search,search,java,recursion 来源: https://codeday.me/bug/20191208/2091818.html