Run time error.


  • 0
    G

    Hi,

    When I am clicking submit I am getting Run-time error for the following code. But when I click on run-code with same input for which its giving Run-time error, its working fine. Is there bug with ojleetcode judge?

    class Solution {
    void mark_visited(vector<vector<char>>& board, int x, int y) {
    int m = board.size();
    int n = board[0].size();
    if ( x < 0 || y < 0 || x >= m || y >= n || board[x][y] != 'O') {
    return;
    } else {
    board[x][y] = '?';
    mark_visited(board, x + 1, y);
    mark_visited(board, x - 1, y);
    mark_visited(board, x , y - 1);
    mark_visited(board, x , y + 1);
    }
    }
    public:
    void solve(vector<vector<char>>& board) {
    if (board.size() == 0) {
    return;
    }

        int m = board.size();
        int n = board[0].size();
        
        for (int i=0; i<m; i++) {
            if (board[i][0] == 'O') {
                 mark_visited(board, i, 0);
            }
            if (board[i][n-1] == 'O') {
                mark_visited(board, i, n-1);
            }
        }
        
        for (int i=1; i<n-1; i++) {
            if (board[0][i] == 'O') {
                 mark_visited(board, 0, i);
            }
            if (board[m-1][i] == 'O') {
                mark_visited(board, m-1, i);
            }
        }
        
        for (int i=0; i<m ;i++) {
            for (int j=0; j<n; j++) {
                if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                } else if (board[i][j] == '?') {
                    board[i][j] = 'O';
                }
            }
        }
        
    }
    

    };

    Input: is too long.


  • 0
    T

    Hi,

    I come across the same problem as yours.

    class Solution {
    private:
        vector<vector<int>> myI;
        vector<vector<int>> myJ;
        vector<vector<int>> myBoard;
        int m, n;
        
        void paint(int i, int j, int label)
        {
            if(myBoard[i][j]==0)
            {
                myBoard[i][j]=label;
                myI[label-1].push_back(i);
                myJ[label-1].push_back(j);
                
                if(i-1>=0&&myBoard[i-1][j]==0)
                    paint(i-1, j, label);
                if(i+1<m&&myBoard[i+1][j]==0)
                    paint(i+1, j, label);
                if(j-1>=0&&myBoard[i][j-1]==0)
                    paint(i, j-1, label);
                if(j+1<n&&myBoard[i][j+1]==0)
                    paint(i, j+1, label);
            }
        }
        
    public:
        void solve(vector<vector<char>>& board) {
            m = board.size();
            if(m==0)
                return;
            n = board[0].size();
            if(n==0)
                return;
                
            myI.clear();
            myJ.clear();
            myBoard.resize(m, vector<int>(n, 0));
            int i, j;
            for(i=0; i<m; i++)
                for(j=0; j<n; j++)
                {
                    if(board[i][j]=='X')
                        myBoard[i][j]=-1;
                }
                
            int label=1;
            for(i=0; i<m; i++)
                for(j=0; j<n; j++)
                    if(myBoard[i][j]==0)
                    {
                        myI.push_back(vector<int>(0));
                        myJ.push_back(vector<int>(0));
                        paint(i, j, label);
                        label++;
                    }
                    
            
            for(label=0; label<myI.size(); label++)
            {
                vector<int> vI = myI[label];
                vector<int> vJ = myJ[label];
                bool yes = true;
                for(i=0; i<vI.size(); i++)
                {
                    if(vI[i]==0 || vI[i]==m-1 || vJ[i]==0 || vJ[i]==n-1)
                    {
                        yes = false;
                        break;
                    }
                    if(yes==false)
                        break;
                }
                
                if(yes)
                {
                    for(i=0; i<vI.size(); i++)
                    {
                        board[vI[i]][vJ[i]] = 'X';
                    }
                }
            }
            
        }
    };

  • 0
    C

    Hello,I have corrected your code so that it can pass the runtime error.
    The Runtime error happens because your recursion is too deep.
    Since we all start the dfs at the boundary point .
    A small trick with your mark_visited function can reduce the Recursion deep .

    class Solution { 
                void mark_visited(vector<vector<char>>& board, int x, int y) 
                 { 
                        int m = board.size(); 
                        int n = board[0].size(); 
                        board[x][y]='?';
                
                           if (x > 1 && board[x - 1][y] == 'O') mark_visited(board, x - 1, y);  // we al
                           if (x < m - 2 && board[x + 1][y] == 'O') mark_visited(board, x + 1, y);
                           if (y > 1 && board[x][y - 1] == 'O')  mark_visited(board, x, y - 1);
                           if (y< n - 2 && board[x][y+ 1] == 'O')  mark_visited(board, x, y + 1);
                      } 
                
                public: void solve(vector<vector<char>>& board) {
                
                 if (board.size() == 0) return; 
                
                    int m = board.size();
                    int n = board[0].size();
                
                    
                        for (int i=0; i<m; i++) {
                            if (board[i][0] == 'O') {
                                 mark_visited(board, i, 0);
                            }
                            if (board[i][n-1] == 'O') {
                                mark_visited(board, i, n-1);
                            }
                        }
                    
                        for (int i=1; i<n-1; i++) {
                            if (board[0][i] == 'O') {
                                 mark_visited(board, 0, i);
                            }
                            if (board[m-1][i] == 'O') {
                                mark_visited(board, m-1, i);
                            }
                        }
                    
                        for (int i=0; i<m ;i++) {
                            for (int j=0; j<n; j++) {
                                if (board[i][j] == 'O') {
                                    board[i][j] = 'X';
                                } else if (board[i][j] == '?') {
                                    board[i][j] = 'O';
                                }
                            }
                        }
                    
                    }
            };

Log in to reply
 

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