Wrong answer, help


  • 0
    K

    Hello,
    This is regarding test case 37.
    The input of the test case is:
    o x x o x
    x o o x o
    x o x o x
    o x o o o
    x x o x o

    The expected output is
    o x x o x
    x x x x o
    x x x o x
    o x o o o
    x x o x o

    The answer I got is :
    o x x o x
    x x x x o
    x x x x x
    o x o o o
    x x o x o

    why shouldn't the 'O' at row 2 and column 3 (starting from 0) be flipped to 'X' ?

    my code:

    class Solution {
    public:
        void solve(vector<vector<char>>& board) {
            for(int r=0; r < board.size(); r++){
                for(int c = 0; c < board[r].size(); c++){
                    if (isSorrounded(board,'X', r,c)){    
                            board[r][c] = 'X';
                        }
                    }
                }
            }
        }
        
        bool test_isSorrounded(const vector<vector<char>>& board,char sorroundChar,int r, int c){
            bool up = sorroundUp(board, 'X', r, c);
            bool down = sorroundDown(board, 'X', r, c);
            bool left = sorroundLeft(board, 'X', r, c) ;
            bool right = sorroundRight(board, 'X', r, c);
            
            cout <<up << down << left << right << endl;
            return(up && down && left && right);
                    
        }
        
        bool isSorrounded(const vector<vector<char>>& board,char sorroundChar,int r, int c){
            return(sorroundUp(board, 'X', r, c) &&
                    sorroundDown(board, 'X', r, c) &&
                    sorroundLeft(board, 'X', r, c) &&
                    sorroundRight(board, 'X', r, c) );
        }
        
        bool sorroundUp(const vector<vector<char>>& board,char sorroundChar,int r, int c){
            if(r-1 < 0)
                return false;
            else if(board[r-1][c] == sorroundChar)
                return true;
            else
                return sorroundUp(board, sorroundChar, r-1, c);
        }
        
        bool sorroundDown(const vector<vector<char>>& board,char sorroundChar,int r, int c){
            if(r+1 > board.size()-1)
                return false;
            else if(board[r+1][c] == sorroundChar)
                return true;
            else
                return sorroundDown(board, sorroundChar, r+1, c);
        }
        
        bool sorroundLeft(const vector<vector<char>>& board,char sorroundChar,int r, int c){
            if(c-1 < 0)
                return false;
            else if(board[r][c-1] == sorroundChar)
                return true;
            else
                return sorroundLeft(board, sorroundChar, r, c-1);
        }
        
        bool sorroundRight(const vector<vector<char>>& board,char sorroundChar,int r, int c){
            if(c+1 > board[r].size()-1)
                return false;
            else if(board[r][c+1] == sorroundChar)
                return true;
            else
                return sorroundRight(board, sorroundChar, r, c+1);
        }
    };
    

Log in to reply
 

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