Wrong answer in OJ but correct locally - case: {XXX,XOX,XXX}


  • 0
    J

    What's wrong with this code? I don't see anything wrong that leads to this result.
    On every '0's, I switched it to '1' first and do dfs to see if the surroundings are 'X's and '0's. if not, switch it back to '0', otherwise to 'X'.
    on test case {XXX,XOX,XXX}, it shows correct answer locally but fails in OJ.
    Helps will be appreciated...

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

    Input: ["XXX","XOX","XXX"]
    Output: ["XXX","XOX","XXX"]
    Expected: ["XXX","XXX","XXX"]


  • 0
    H

    Oh, I was in the same situation.
    I figured out the reason is that it's "O", not "0"(zero)... Try change your "0"(zero) into capital "O"...


  • 0
    J

    OK, that was the problem = =!!!
    Thanks a lot, haojie2~


Log in to reply
 

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