简体   繁体   English

C#中的Word Boggle算法

[英]word Boggle algorithm in c#

I am trying to solve the skill test at CodeFights and I have encountered a problem while I was trying to solve their word boggle algorithm. 我正在尝试解决CodeFights的技能测试,并且在尝试解决他们的单词变形算法时遇到了问题。 here is the description of the challenge : 这是挑战的描述:

Boggle is a popular word game in which players attempt to find words in sequences of adjacent letters on a rectangular board. Boggle是一种流行的文字游戏,玩家尝试在矩形板上按相邻字母的顺序查找单词。

Given a two-dimensional array board that represents the character cells of the Boggle board and an array of unique strings words, find all the possible words from words that can be formed on the board. 给定一个表示Boggle板上字符单元的二维阵列板和一个唯一的字符串单词阵列,请从板上可以形成的单词中找到所有可能的单词。

Note that in Boggle when you're finding a word, you can move from a cell to any of its 8 neighbors, but you can't use the same cell twice in one word. 请注意,在Boggle中,当您找到一个单词时,可以将其从一个单元格移至它的8个邻居中的任何一个,但是不能在一个单词中两次使用同一单元格。

I wrote a code that looks like it works , however it doesn't pass the hidden test that they have , and now I am stuck at that point. 我编写了一个看起来可以正常工作的代码,但是它没有通过他们所拥有的隐藏测试,现在我被困在这一点上。

Here is my code : 这是我的代码:

  string[] wordBoggle(char[][] board, string[] words)
    {
        string[] foundWords = new string[words.Length];

        for (int i = 0; i < words.Length; i++)
        {
            List<int[]> Prefixs = new List<int[]>();
            for (int j = 0; j < board.Length; j++)
            {
                for (int k = 0; k < board[j].Length; k++)
                {
                    if (board[j][k] == words[i][0])
                    {
                        int[] match = new int[] { j, k };
                        Prefixs.Add(match);
                    }
                }
            }
            if (Prefixs.Count > 0)
            {
                for (int l = 0; l < Prefixs.Count; l++)
                {
                    bool[][] bitMap = new bool[board.Length][];
                    ResetBitMap(bitMap, board.Length, board[0].Length);
                    bitMap[Prefixs[l][0]][Prefixs[l][1]] = true;
                    bool match = search(Prefixs[l][0], Prefixs[l][1], board, words[i], bitMap, 1);
                    if (match)
                    {
                        foundWords[i] = words[i];
                        break;
                    }
                }
            }

        }
        var res = foundWords.Where(q => q != null).OrderBy(e => e).Distinct().ToArray();
        return res;
    }

     bool search(int col, int row, char[][] board, string word, bool[][] bitMap, int index)
    {
        var found = false;
        if (index == word.Length)
        {
            return true;
        }
        Dictionary<int[], char> neighbors = getNeightbors(col, row, board);
        var allNextMatchs = neighbors.Where(a => a.Value == word[index]).ToArray();
        if (allNextMatchs.Length < 1 && index < word.Length - 1)
        {
            return false;
        }
        var count = 0;
        for (int i = 0; i < bitMap.Length; i++)
        {
            for (int k = 0; k < bitMap[i].Length; k++)
            {
                if (bitMap[i][k] == true)
                {
                    count++;
                }
            }
        }
        if (count == word.Length)
        {
            return true;
        }
        foreach (var item in allNextMatchs)
        {
            if (item.Value != 0 && bitMap[item.Key[0]][item.Key[1]] == false)
            {
                col = item.Key[0];
                row = item.Key[1];
                bitMap[col][row] = true;
                if (index < word.Length - 1)
                {
                    index += 1;
                }
                found = search(col, row, board, word, bitMap, index);
                if (found)
                {
                    return true;
                }
                else
                {
                    ResetBitMap(bitMap, board.Length, board[0].Length);
                }
            }
            else
            {
                continue;
            }
        }
        return found;

    }

     Dictionary<int[], char> getNeightbors(int col, int row, char[][] board)
    {
        Dictionary<int[], char> neighbors = new Dictionary<int[], char>(8);

        try
        {
            int[] cr = new int[] { col - 1, row - 1 };
            neighbors.Add(cr, board[col - 1][row - 1]); ; // left up 
        }
        catch (IndexOutOfRangeException e) {
            Console.WriteLine(e.Message);
        }
        try
        {
            int[] cr = new int[] { col, row - 1 };
            neighbors.Add(cr, board[col][row - 1]); 
        }
        catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
        try
        {
            int[] cr = new int[] { col + 1, row - 1 };
            neighbors.Add(cr, board[col + 1][row - 1]);
        }
        catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }

        try
        {
            int[] cr = new int[] { col - 1, row };
            neighbors.Add(cr, board[col - 1][row]);
        }
        catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
        try
        {
            int[] cr = new int[] { col + 1, row };
            neighbors.Add(cr, board[col + 1][row]);
        }
        catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
        try
        {
            int[] cr = new int[] { col + 1, row + 1 };
            neighbors.Add(cr, board[col + 1][row + 1]);
        }
        catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
        try
        {
            int[] cr = new int[] { col, row + 1 };
            neighbors.Add(cr, board[col][row + 1]);
        }
        catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
        try
        {
            int[] cr = new int[] { col - 1, row + 1 };
            neighbors.Add(cr, board[col - 1][row + 1]);
        }
        catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
        return neighbors;
    }

     void ResetBitMap(bool[][] map, int col, int row)
    {
        for (int i = 0; i < map.Length; ++i)
        {
            map[i] = new bool[row];
        }

        for (int j = 0; j < map.Length; ++j)
        {
            for (int k = 0; k < map[0].Length; ++k)
            {
                map[j][k] = false;
            }
        }
    }

Does anyone have any idea what am I missing here ? 有人知道我在这里想念什么吗?

i have solution, follow refer: 我有解决方案,请参阅:

int Arrtick[4][4];

void BT(vector<std::vector<char>> boggle, string words,int lengOfword, int N, int x, int y, int currID, bool &flag)
{
    if (currID >= lengOfword){
        flag = true; return;
    }
    //1.
    if (y + 1 < N && boggle[x][y + 1] == words[currID] && Arrtick[x][y + 1] == 0)
    {
        Arrtick[x][y + 1] = 1;
        BT(boggle, words, lengOfword, N, x, y + 1, currID + 1, flag);
        Arrtick[x][y + 1] = 0;
    }
    if (flag) return;
    //2.
    if (x + 1 < N && y + 1 < N && boggle[x + 1][y + 1] == words[currID] && Arrtick[x + 1][y + 1] == 0)
    {
        Arrtick[x + 1][y + 1] = 1;
        BT(boggle, words, lengOfword, N, x + 1, y + 1, currID + 1, flag);
        Arrtick[x + 1][y + 1] = 0;
    }
    if (flag) return;
    //3.
    if (x + 1 < N && boggle[x + 1][y] == words[currID] && Arrtick[x + 1][y] == 0)
    {
        Arrtick[x + 1][y] = 1;
        BT(boggle, words, lengOfword, N, x + 1, y, currID + 1, flag);
        Arrtick[x + 1][y] = 0;
    }
    if (flag) return;
    //4.
    if (x + 1 < N && y - 1 >= 0 && boggle[x + 1][y - 1] == words[currID] && Arrtick[x + 1][y - 1] == 0)
    {
        Arrtick[x + 1][y - 1] = 1;
        BT(boggle, words, lengOfword, N, x + 1, y - 1, currID + 1, flag);
        Arrtick[x + 1][y - 1] = 0;
    }
    if (flag) return;
    //5.
    if (y - 1 >= 0 && boggle[x][y - 1] == words[currID] && Arrtick[x][y - 1] == 0)
    {
        Arrtick[x][y - 1] = 1;
        BT(boggle, words, lengOfword, N, x, y - 1, currID + 1, flag);
        Arrtick[x][y - 1] = 0;
    }
    if (flag) return;
    //6.
    if (x - 1 >= 0 && y - 1 >= 0 && boggle[x - 1][y - 1] == words[currID] && Arrtick[x - 1][y - 1] == 0)
    {
        Arrtick[x - 1][y - 1] = 1;
        BT(boggle, words, lengOfword, N, x - 1, y - 1, currID + 1, flag);
        Arrtick[x - 1][y - 1] = 0;
    }
    if (flag) return;
    //7.
    if (x - 1 >= 0 && boggle[x - 1][y] == words[currID] && Arrtick[x - 1][y] == 0)
    {
        Arrtick[x - 1][y] = 1;
        BT(boggle, words, lengOfword, N, x - 1, y, currID + 1, flag);
        Arrtick[x - 1][y] = 0;
    }
    if (flag) return;
    //8.
    if (x - 1 >= 0 && y + 1 < N &&  boggle[x - 1][y + 1] == words[currID] && Arrtick[x - 1][y + 1] == 0)
    {
        Arrtick[x - 1][y + 1] = 1;
        BT(boggle, words, lengOfword, N, x - 1, y + 1, currID + 1, flag);
        Arrtick[x - 1][y + 1] = 0;
    }

}
// Prints all words present in dictionary.
std::vector<std::string> wordBoggle(std::vector<std::vector<char>> boggle, std::vector<std::string> words)
{
    int Nx = boggle[0].size();
    int Ny = boggle.size();
    bool flagg;
    vector<std::string> strOutput;
    for (int k = 0; k < words.size(); k++)
    {
        flagg = false;
        memset(Arrtick, 0, sizeof(Arrtick));
        for (int i = 0; i < Nx; i++)
        {
            for (int j = 0; j < Ny; j++)
            {
                if (boggle[i][j] == words[k][0])
                {
                    Arrtick[i][j] = 1;
                    BT(boggle, words[k], words[k].size(), Nx, i, j, 1, flagg);
                    Arrtick[i][j] = 0;
                }
                if (flagg)
                    break;
            }
            if (flagg)
            {
                strOutput.push_back(words[k]);
                break;
            }
        }

    }

    return strOutput;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM