我正在创建一个单词搜索游戏,但我被算法困住了。我需要在数据结构之类的表中找到单词的出现。我选择使用我知道长度和高度的二维数组。我的想法是寻找单词的第一个字母,如果找到,则寻找所有可能的方向。我无法掌握的是找到第一个字母后如何开始搜索。我想将第一个字母的位置传递给将在各个方向进行搜索的方法。这是我到目前为止所拥有的:
public void SearchWord(char[,] input, string name)
{
//1. loop through the array and look for the first letter of the string
//2. if found search in all directions "iterative"
//3. if one direction doesn't find it break out of the method and continue to search in other directions
//4. if found mark the positions so you don't find the same word more than once
char firstLetter = name[0];
//go look for it in the 2d array
for (int y = 0; y < 5; y++)
{
for (int x = 0; x < 4; x++)
{
if (results[x, y] == firstLetter)//found the letter
{
Console.WriteLine("Found it " + " " + firstLetter);
Console.WriteLine(x + " " + y);
SearchRightDirection(x, y);
SearchLeftDirection(x, y);
}
}
}
}
我尝试将位置作为参数传递,例如 SearchRightDirection(char[,], int x, int y){} 但是我无法从这个确切的位置行和列继续数组。
你有什么建议吗?另外,如果结构是正确的?