Why the out put is wrong but it gets the right answer on my own PC?


  • 0
    J

    The all code is underbelow, any one tell me why?
    for the test case
    ["XXX","XOX","XXX"]
    , my program get ["XXX","XXX","XXX"] but OJ get ["XXX","XOX","XXX"].

    #include <iostream>
    #include <queue>
    #include <vector>
    
    using namespace std;
    
    class Solution {
        inline bool isValid(int x, int y, int N) {
            return !(x < 0 || y < 0 || x > N || y > N);
        }
        void bfs(int x, int y, vector<vector<char>>& board) {
            const int N = board.size() - 1;
            queue<pair<int, int>> Q;
            if (!isValid(x, y, N))
                return;
            Q.push(make_pair(x, y));
            while (!Q.empty()) {
                int i = Q.front().first, j = Q.front().second;
                Q.pop();
                if (board[i][j] == 'o') {
                    board[i][j] = 's';
                    if (isValid(i+1, j, N))
                        Q.push(make_pair(i+1, j));
                    if (isValid(i-1, j, N))
                        Q.push(make_pair(i-1, j));
                    if (isValid(i, j+1, N))
                        Q.push(make_pair(i, j+1));
                    if (isValid(i, j-1, N))
                        Q.push(make_pair(i, j-1));
                }
            }
        }
    public:
        void solve(vector<vector<char>>& board) {
            int n = board.size();
            if (n < 1)
                return;
    
            //bfs line [0][0...n-1]
            for (int i = 0; i < n; i++)
                bfs(0, i, board);
    
            //bfs b[0...n-1][n-1]
            for (int i = 0; i < n; i++)
                bfs(i, n-1, board);
    
            // b[n-1][0...n-1]
            for (int i = 0; i < n; i++)
                bfs(n-1, i, board);
    
            // b[0...n-1][0]
            for (int i = 0; i < n; i++)
                bfs(i, 0, board);
    
            // o->x
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++)
                    if (board[i][j] == 'o')
                        board[i][j] = 'x';
            }
            // s->o
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++)
                    if (board[i][j] == 's')
                        board[i][j] = 'o';
            }
        }
    };
    void print(vector<vector<char>> & b)
    {
        for (int i = 0; i < b.size(); i++) {
            for (int j = 0; j < b.size(); j++) {
                cout<< b[i][j] << " ";
            }
            cout<< endl;
        }
    }
    int main()
    {
        vector<vector<char>> b;
        b.push_back(vector<char>{'x','x','x'});
        b.push_back(vector<char>{'x','o','x'});
        b.push_back(vector<char>{'x','x','x'});
        print(b);
        cout<< "------" << endl;
        Solution().solve(b);
        print(b);
        return 0;
    }

  • 0

    Typo? Try uppercase 'O' and uppercase 'X'?


  • 0
    J

    That's it,
    Thanks.


  • 0
    Y

    I have same problem with you. But my '0' and 'X' is correct.


Log in to reply
 

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