Runtime Error: need help


  • 0
    S
    class Solution {
    public:
        vector<vector<int>>dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        void bfs(int i, int j, vector<vector<char>>& board) {
            board[i][j] = '1';
            for (int k = 0; k < 4; k++) {
                int I = i + dir[k][0], J = j + dir[k][1];
                if (I >= 0 && I < board.size() && J >= 0 && J < board[0].size() && board[I][J] == 'O')
                    bfs(I, J, board);
            }
        }
        void solve(vector<vector<char>>& board) {
            if (board.empty() || board[0].empty())
                return;
            //vector<vector<bool>>visited(board.size(), vector<int>(board[0].size(), 0));
            int n = board.size(), m = board[0].size();
            for (int i = 0; i < n; i++)
                if (board[i][0] == 'O')
                    bfs(i, 0, board);
            for (int i = 0; i < n; i++)
                if (board[i][m - 1] == 'O')
                    bfs(i, m - 1, board);
            for (int j = 0; j < m; j++)
                if (board[0][j] == 'O')
                    bfs(0, j, board);
            for (int j = 0; j < m; j++)
                if (board[n - 1][j] == 'O')
                    bfs(n - 1, j, board);
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    if (board[i][j] == 'O')
                        board[i][j] = 'X';
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    if (board[i][j] == '1')
                        board[i][j] = 'O';
            return;
        }
    };
    

  • 2

    @sidawang shrink the range of your search can avoid it

    if (I > 0 && I < board.size()-1 && J > 0 && J < board[0].size()-1 && board[I][J] == 'O')

    @StefanPochmann Stefan would you please explain a little bit about this shrinking? Thanks !


  • 0
    S

    Thanks a lot!


Log in to reply
 

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