gpt4 book ai didi

java - 谜题中的单词搜索

转载 作者:行者123 更新时间:2023-12-01 15:42:16 26 4
gpt4 key购买 nike

这里是新手,我编写了一段代码来搜索拼图中的单词,但是在我运行程序后,单词搜索似乎没有返回任何内容。我将字典存储在二叉树中。我需要根据我的树中的字符检查每个字符组合。您能帮忙吗?谢谢....

这就是解决方法

public String solve() 
{
int row = puzzle.length;
int coloumns = puzzle[0].length;
this.foundWords = new ArrayList<String>();
if (this.dictionary == null)
return null;
for (int i = 0; i < this.puzzle[0].length; ++i)
{
for (int j = 0; j < this.puzzle.length; ++j)
{
if (this.getWord(i, j, 0, 1) == null)
continue;
if(this.inDictionary(this.getWord(i,j,0,1)))
this.foundWords.add(getWord(i,j,0,1).concat("\n" + this.mapDirection(0)));
for(int d = 0; d<8; ++d)
{
int n = 2;
String word = this.getWord(i,j,d,n);
while(word !=null)
{
if(this.inDictionary(word))
this.foundWords.add(word.concat("\n" + this.mapDirection(d)));
word = this.getWord(i,j,d,n);
n++;
}
}

}
}
String temp = "";
for(int i= 0; i < foundWords.size(); i++)
{
temp = temp.concat(foundWords.get(i));
}
return temp;
}

这就是这个词..

public String getWord(int row, int column, int d, int length)
{
if (length < 1)
return null;
d %= 8;

StringBuilder rBuild = new StringBuilder();
rBuild.append(this.puzzle[row][column]);
length--;
while (length >= 0)
{
if ((d == 3) || (d == 4) || (d == 5))
column--;
if ((d == 1) || (d == 0) || (d == 7))
column++;

if ((d == 1) || (d == 2) || (d == 3))
row--;
if ((d == 5) || (d == 6) || (d == 7))
row++;

if ((row < 0) || (row >= this.puzzle.length)
|| (column < 0) || (column >= this.puzzle[0].length))
return null;

rBuild.append(this.puzzle[row][column]);
length--;
}

return rBuild.toString();
}

方向..

public String mapDirection(int direction)
{
direction %=8;
switch(direction)
{
case 0: return " right";
case 1: return " up and right";
case 2: return " up";
case 3: return " up and left";
case 4: return " left";
case 5: return " down and left";
case 6: return " down and left";
case 7: return " down and right";
}
return null;
}

最佳答案

您可以查看 Jumble 中采用的方法。 ,它实现了所示的第一个算法 here 。它比排列(第二种)方法更快并且可扩展性更好。

关于java - 谜题中的单词搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7864015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com