2

我正在尝试解决 CodeFights 的技能测试,但在尝试解决他们的 word boggle 算法时遇到了问题。这是挑战的描述:

Boggle 是一种流行的文字游戏,玩家试图在矩形板上的相邻字母序列中找到单词。

给定一个表示 Boggle 板的字符单元的二维数组板和一个唯一字符串单词的数组,从板上可以形成的单词中找到所有可能的单词。

请注意,在 Boggle 中,当您查找一个单词时,您可以从一个单元格移动到其 8 个相邻单元格中的任何一个,但您不能在一个单词中两次使用同一个单元格。

我写了一个看起来可以工作的代码,但是它没有通过他们拥有的隐藏测试,现在我被困在了这一点上。

这是我的代码:

  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;
            }
        }
    }

有谁知道我在这里想念什么?

4

1 回答 1

2

我有解决方案,请参考:

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;
}
于 2018-05-14T13:05:22.657 回答