2

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. 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.

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.

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.

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 ?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Zvika
  • 41
  • 3
  • well - just to be helpful - you are doing what is called error-hiding which is typically an anti-pattern. see here: https://en.wikipedia.org/wiki/Error_hiding to explain a little - you are using try/catch and then not doing anything with the exception. – geekzster Jul 24 '17 at 14:44
  • if you mean to the tries and catches that I put in the GetNeighbors function – Zvika Jul 24 '17 at 14:46
  • Without knowing what the hidden test is - how can we be expected to help. You need to present a well defined problem & explain exactly why your code is not working. – PaulF Jul 24 '17 at 14:46
  • it doesn't fail there – Zvika Jul 24 '17 at 14:47
  • 1
    could you hint at where it does fail? – Dave Becker Jul 24 '17 at 14:47
  • no , it just saying "You didn't pass the full test set. Use custom tests to try and fix it. Sample tests: 5/5 Hidden tests: 3/5 " – Zvika Jul 24 '17 at 14:51
  • Well if you cant tell us what the problem is, how can we help you. This site is not here to help you pass skill tests. If you have a well defined & reproducible problem then we may help - but we are not going to search through your code trying to find a problem that you cannot tell us. – PaulF Jul 24 '17 at 14:54
  • I don't want to utilize this site to solve my skill test , I just wanted to know if anyone who is familiar with this kind of algorithm , can help my to find out whether my algorithm is complete or not. – Zvika Jul 24 '17 at 15:04
  • You may want to use [this site](https://codereview.stackexchange.com/) to get your code checked out - SO is more directed at well defined problems. – PaulF Jul 24 '17 at 15:14
  • 2
    So, they found 2 inputs that broke your code. You need to think about feeding values into this algorithm that will break it. For instance if you pass `null` as the second parameter your first line of code throws an exception. is the grid always 3x3, does your code work for 4x4? does the grid have to even be square what about 2x5? `nulls`, bounds and hard-coded values are usually the culprits in such things. – Dave Becker Jul 24 '17 at 15:19

1 Answers1

2

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;
}
Van Ba Cao
  • 31
  • 4