The code runs fine on my laptop, but gives runtime error on OJ. Please help.


  • 0
    L

    class Solution {
    public:

    bool check(vector<vector<char> > &board, string word, int i , int j, int k, int ** t)
    {
        if(k == word.size() - 1)
            return true;
        if((i<(board.size() - 1))  && (board[i+1][j] == word[k]) && (t[i+1][j] == 0))
        {
            t[i+1][j] = 1;
            if(check(board, word, i+1, j, k+1, t) == 1)
                return true;
            t[i+1][j] = 0;
        }
        if((i>0) && (board[i-1][j] == word[k]) && (t[i-1][j] == 0))
        {
            t[i-1][j] = 1;
            if(check(board, word, i-1, j, k+1, t) == 1)
                return true;
            t[i-1][j] = 0;
        }
        if((j<(board.size() - 1)) && (board[i][j+1] == word[k]) && (t[i][j+1] == 0))
        {
            t[i][j-1] = 1;
            if(check(board, word, i, j+1, k+1, t) == 1)
                return true;
            t[i][j+1] = 0;
        }
        if((j>0) && (board[i][j-1] == word[k]) && (t[i][j-1] == 0))
        {
            t[i][j-1] = 1;
            if(check(board, word, i, j-1, k+1, t) == 1)
                return true;
            t[i][j-1] = 0;
        }
        return false;
        
        
    }
    bool exist(vector<vector<char> > &board, string word) {
        int **t = new int*[board.size() + 1];
            for(int w = 0; w<board.size(); w++)
            {
                t[w] = new int[board[0].size() + 1];
            }
        for(int i = 0; i<board.size(); i++)
        {
            vector<char> l;
            l = board[i];
           // vector<vector<bool> > t(board.size(), vector<bool>(board[0].size(), false));
            
            for(int j = 0; j<l.size(); j++)
            {
                if(board[i][j] == word[0] && word.size() == 1)
                    return true;
                if(board[i][j] == word[0])
                {
                    t[i][j] = 1;
                    if(check(board,word,i,j,1,t)== 1)
                        return true;
                }
            }
        }
        return false;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.