What's wrong with my C++ code? Runtime Error...TIA!


  • 0
    L
    class Solution {
    public:
        void solve(vector<vector<char>>& board) 
        {
            int row=board.size();
            if(row == 0 || row == 1)
            {
                return;
            }
            int col=board[0].size();
            if(col==0 || col == 1)
            {
                return;
            }
            vector<vector<bool>> matrix(row, vector<bool>(col, true));
            for(int i=0; i<row; i++)
            {
                if(board[i][0]=='O')
                {
                    matrix[i][0]=false;
                    mark(board, matrix, i, 0);
                }
                if(board[i][col-1]=='O')
                {
                    matrix[i][col-1]=false;
                    mark(board, matrix, i, col-1);
                }
            }
            for(int i=0; i<col; i++)
            {
                if(board[0][i]=='O')
                {
                    matrix[0][i]=false;
                    mark(board, matrix, 0, i);
                }
                if(board[row-1][i]=='O')
                {
                    matrix[row-1][i]=false;
                    mark(board, matrix, row-1, i);
                }
            }
            for(int i=0; i<row; i++)
            {
                for(int j=0; j<col; j++)
                {
                    if(matrix[i][j]==false)
                    {
                        board[i][j]='O';
                    }
                    if(matrix[i][j]==true)
                    {
                        board[i][j]='X';
                    }
                }
            }
        }
        void mark(const vector<vector<char>> &board, vector<vector<bool>> &matrix, int i, int j)
        {
            int row=board.size();
            int col=board[0].size();
            if(i>0 && board[i-1][j]=='O')
            {
                matrix[i-1][j]=false;
                mark(board, matrix, i-1, j);
            }
            if(j>0 && board[i][j-1]=='O')
            {
                matrix[i][j-1]=false;
                mark(board, matrix, i, j-1);
            }
            if(i<row-1 && board[i+1][j]=='O')
            {
                matrix[i+1][j]=false;
                mark(board, matrix, i+1, j);
            }
            if(j<col-1 && board[i][j+1]=='O')
            {
                matrix[i][j+1]=false;
                mark(board, matrix, i, j+1);
            }
        }
    };

Log in to reply
 

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