【问题标题】:Continue iterating a 2D array from certain element继续从某个元素迭代二维数组
【发布时间】:2016-01-28 17:37:39
【问题描述】:

我正在创建一个单词搜索游戏,但我被算法困住了。我需要在数据结构之类的表中找到单词的出现。我选择使用我知道长度和高度的二维数组。我的想法是寻找单词的第一个字母,如果找到,则寻找所有可能的方向。我无法掌握的是找到第一个字母后如何开始搜索。我想将第一个字母的位置传递给将在各个方向进行搜索的方法。这是我目前所拥有的:

 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){} 但是我无法从这个确切的位置行和列数组继续。

你有什么建议吗?另外,如果结构是正确的?

【问题讨论】:

  • 这个结构代表什么——某种词典?为什么不简单HashSet&lt;string&gt;
  • 它是一个像 N * M 这样的二维网格。如果我使用 HashSet,我将如何进行对角线方向的搜索?
  • 我的评论是在Also if the structure is the right one?。但可能我不理解用例 - 所以它是某种游戏板,而不仅仅是我第一个问题中的字典?为什么不删除那句话,它具有误导性。
  • 是的,它是一个像填字游戏一样的游戏板。
  • char[,] 中是否有一些特殊符号表示单词的结尾?我们还应该搜索整个单词,即如果我们搜索 let say "as" 并找到 "assume",这算作匹配吗?

标签: c# loops multidimensional-array wordsearch


【解决方案1】:

给你:

public static class Sample
{
    public static int CountOccurrences(char[,] table, string word)
    {
        if (string.IsNullOrEmpty(word)) return 0;
        int count = 0;
        int rowCount = table.GetLength(0);
        int colCount = table.GetLength(1);
        for (int row = 0; row < rowCount; row++)
        {
            for (int col = 0; col < colCount; col++)
            {
                if (table[row, col] != word[0]) continue;
                if (word.Length == 1) { count++; continue; }
                for (int dy = -1; dy <= 1; dy++)
                    for (int dx = -1; dx <= 1; dx++)
                        if ((dx != 0 || dy != 0) && Match(word, table, rowCount, colCount, row, col, dy, dx)) count++;
            }
        }
        return count;
    }
    private static bool Match(string word, char[,] table, int rowCount, int colCount, int startRow, int startCol, int dy, int dx)
    {
        int row = startRow + dy;
        if (dy != 0 && (dy < 0 ? row : rowCount - row) < word.Length - 1) return false;
        int col = startCol + dx;
        if (dx != 0 && (dx < 0 ? col : colCount - col) < word.Length - 1) return false;
        for (int charPos = 1; charPos < word.Length; row += dy, col += dx, charPos++)
            if (table[row, col] != word[charPos]) return false;
        return true;
    }
}

【讨论】:

    猜你喜欢
    • 2018-07-28
    • 2021-12-04
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 2015-04-04
    相关资源
    最近更新 更多