Why my code show runtime error ?


  • 0
    V
    int movex[4]={0,0,1,-1};
    int movey[4]={1,-1,0,0};
    void fill(vector<vector<char>>& board,int x,int y,int n,int m)
    {
        int i,j;
        board[x][y]='O';
        for(i=0;i<4;i++)
        {
            if(x+movex[i]>=0 && x+movex[i]<n && y+movey[i]>=0 && y+movey[i]<m && board[x+movex[i]][y+movey[i]]=='-')
            {
                fill(board,x+movex[i],y+movey[i],n,m);   
            }
        }
    }
    
    void solve(vector<vector<char>>& board) {
        
        int n=board.size();
        if(n<=0)
            return;
        int m=board[0].size();
        int i,j,k;
        if(m<=0)
            return;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(board[i][j]=='O')
                {
                    board[i][j]='-';
                }
            }
        }
        
        for(i=0;i<m;i++)
        {
            if(board[0][i]=='-')
            {
                fill(board,0,i,n,m);
            }
        }
        for(i=0;i<m;i++)
        {
            if(board[n-1][i]=='-')
            {
                fill(board,n-1,i,n,m);
            }
        }
        for(i=0;i<n;i++)
        {
            if(board[i][0]=='-')
            {
                fill(board,i,0,n,m);
            }
        }
        for(i=0;i<n;i++)
        {
            if(board[i][m-1]=='-')
            {
                fill(board,i,m-1,n,m);
            }
        }
        
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(board[i][j]=='-')
                {
                    board[i][j]='X';
                }
            }
        }
        
    }
    

    I think the runtime stack is overflow,correct me if i am wrong.


Log in to reply
 

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