Can anybody explain this runtime error?


  • 0
    O

    I guess the problem might be stack overflow caused by dfs. But when I revised the code a little, it got AC.

        void solve(vector<vector<char>>& board) {
            if(!board.empty()){
                int row=board.size(), column=board[0].size();
                for(int r=0;r<row;r++){
                    check(board, 0, r);
                    if(column>1)
                        check(board, column-1, r);
                }
                for(int c=1;c<column-1;c++){
                    check(board, c, 0);
                    if(row>1)
                        check(board, c, row-1);
                }
                for(auto &b:board)
                    for(auto &bl:b)
                        bl=='O' ? bl='X' : bl=='o' ? bl='O' : bl;
            }
        }
        void check(vector<vector<char>> &board, int column, int row){
            if(board[row][column]=='O'){
                board[row][column]='o';
                if(row>0 && board[row-1][column]=='O')
                    check(board, column, row-1);
                if(row<board.size()-1 && board[row+1][column]=='O')
                    check(board, column, row+1);
                if(column>0 && board[row][column-1]=='O')
                    check(board, column-1, row);
                if(column<board[0].size()-1 && board[row][column+1]=='O')
                    check(board, column+1, row);
            }
        }
    

    I can get the correct answer of the problematic test case on Custom Testcase but it is still runtime error after submit solution. That's confusing!
    Below is the AC code.

        void solve(vector<vector<char>>& board) {
            if(!board.empty()){
                int row=board.size(), column=board[0].size();
                for(int r=0;r<row;r++){  
                    check(board, 0, r);
                    if(column>1)
                        check(board, column-1, r);
                }
                for(int c=1;c<column-1;c++){
                    check(board, c, 0);
                    if(row>1)
                        check(board, c, row-1);
                }
                for(auto &b:board)
                    for(auto &bl:b)
                        bl=='O' ? bl='X' : bl=='o' ? bl='O' : bl;
            }
        }
        void check(vector<vector<char>> &board, int column, int row){
            if(board[row][column]=='O'){
                board[row][column]='o';
                if(row>1 && board[row-1][column]=='O')   // changed here
                    check(board, column, row-1);
                if(column>1 && board[row][column-1]=='O') // changed here
                    check(board, column-1, row);
                if(row<board.size()-1 && board[row+1][column]=='O') 
                    check(board, column, row+1);
                if(column<board[0].size()-1 && board[row][column+1]=='O')
                    check(board, column+1, row);
            }
        }

  • 0
    O

    The problematic test case is

    ["OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO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