我正在开发一个使用递归的应用程序。
void Keres(char[,] array, int width, int height)
{
_found = Search(array, height, width, _words);
if (_found.Count < 6)
{
_found.Clear();
Keres(array, width, height);
}
}
Search 是一个递归的方法,它返回一个字符串List。我需要它的计数大于 5。但如果不是,我必须一次又一次地调用 Keres 方法,直到它的计数为 6 或更大,但我的应用程序卡住。
这里是我调用 Keres 方法的地方:
if ((string)appSettings["gamelanguage"] == "english")
{
szo = EngInput(3, 3); //szo is a char[,] array
Keres(szo, 3, 3);
}
我该怎么做才能避免递归或避免崩溃,并获得我的 >6 项?
编辑:搜索方法
List<string> Search(char[,] letter_table, int height, int width, List<string> words_list)
{
List<string> possible_words = new List<string>();
char[,] _tmp_letter_table = _tmp_letter_table = new char[height, width];
bool possible = false;
foreach (String word in words_list)
{
possible = false;
Array.Copy(letter_table, _tmp_letter_table, width * height);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (_tmp_letter_table[i, j] == word[0])
{
if (IsNeighborTest(word, i, j, height, width, _tmp_letter_table, 0) == true)
{
possible = true;
break;
}
else
{
Array.Copy(letter_table, _tmp_letter_table, width * height);
}
}
}
if (possible == true)
{
possible_words.Add(word);
break;
}
}
}
return possible_words;
}
请您参考如下方法:
你的代码不是正确的递归,实际上你总是调用相同的方法,每次调用递归方法时一定有一些改变,显然在你的代码中你永远不会退出该方法并且应用程序卡住。
我想,如果我理解你想要什么,你面临的问题是递归无法解决的。
也许数组会发生变化,直到更改为 >6 您想使用 Keres 方法检查?那么递归就不是办法了。
