Wrong answer by the judgement, but correct on my own IDE...


  • 0
    N

    My code returned "wrong answer" for a fairly simple case: ["xxx","xox","xxx]. It said nothing changed. But I tested my code on VC++ 2010 express and Code::Blocks with GCC, both correct. Any suggestion on this issue?

    class Solution {
    public:
        void solve(vector<vector<char>> &board) {
            int n=board.size();
            if (n==0)   return;
            int m=board[0].size(), i, j;
            if (m==0)   return;
            // expand every 'o' on the boundary, change to '*'
            for (i=0; i<n; i++)
            {   if (board[i][0]=='o')
                    expand_boundary(board, i, 0);
                if (board[i][m-1]=='o')
                    expand_boundary(board, i, m-1);
            }
            for (j=1; j<m-1; j++)
            {   if (board[0][j]=='o')
                    expand_boundary(board, 0, j);
                if (board[n-1][j]=='o')
                    expand_boundary(board, n-1, j);
            }
            for (i=0; i<n; i++)
            for (j=0; j<m; j++)
            {
                if (board[i][j]=='o')   // flip all remaining 'o' to 'x'
                    board[i][j] = 'x';
                if (board[i][j]=='*')   // restore all '*' to 'o'
                    board[i][j] = 'o';
            }
            return;
        }
    private:
        void expand_boundary(vector<vector<char>> &board, int x, int y)
        {   // expand board[x][y] to all connected 'o', change to '*'
            if (board[x][y]!='o')   return;
            stack<pair<int,int>> S;
            S.push(make_pair(x,y));
            while (!S.empty())
            {   pair<int,int> p = S.top();
                S.pop();
                board[p.first][p.second] = '*';
                if (valid(board,p.first+1,p.second) && board[p.first+1][p.second]=='o')
                    S.push(make_pair(p.first+1,p.second));
                if (valid(board,p.first-1,p.second) && board[p.first-1][p.second]=='o')
                    S.push(make_pair(p.first-1,p.second));
                if (valid(board,p.first,p.second+1) && board[p.first][p.second+1]=='o')
                    S.push(make_pair(p.first,p.second+1));
                if (valid(board,p.first,p.second-1) && board[p.first][p.second-1]=='o')
                    S.push(make_pair(p.first,p.second-1));
            }
            return;
        }
        bool valid(vector<vector<char>> &board, int i, int j)
        {
            if (i<0 || i>=board.size())    return false;
            if (j<0 || j>=board[0].size())    return false;
            return true;
        }	
    };

  • 0
    R

    I have the same issue. Do anyone know how to solve this?


  • 1
    R

    I figure it out right now! For me, it is just a stupid mistake. The loop-liked letter in problem should be 'O'( o in upper case).


  • 0
    N

    Thanks a lot~!
    I used 'x','o' instead of upper case 'X','O'...
    Accepted now.


Log in to reply
 

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