Why my code get a run time error with the case "[]", but it works well when I test in Run Code


  • 0
    class Solution {
    public:
        struct Point{
            int r,c;
        };
        bool vis[1005][1005];
        int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
        bool isInBorder(int r, int c, int n) {
            if(r == 0 || r == n - 1 || c == 0 || c == n - 1) {
                return true;
            }
            return false;
        }
        void BFS(int r, int c, int n, vector<vector<char>>& board) {
            Point s;
            s.r = r;
            s.c = c;
            vector<Point> Q;
            Q.push_back(s);
            int head = 0, tail = 1;
            bool flag = true;
            while(head < tail) {
                Point now = Q[head++];
                for(int i = 0 ; i < 4 ; i++) {
                    int nr = now.r + dir[i][0];
                    int nc = now.c + dir[i][1];
                    if(nr >= 0 && nr < n && nc >= 0 && nc < n) {
                        if(board[nr][nc] == 'O' && !vis[nr][nc]) {
                            vis[nr][nc] = true;
                            Point next;
                            next.r = nr;
                            next.c = nc;
                            Q.push_back(next);
                            tail++;
                            if(isInBorder(nr, nc, n)) {
                                flag = false;
                            }
                        }
                    }
                }
            }
            if(flag) {
                for(int i = 0 ; i < tail ; i++) {
                    board[Q[i].r][Q[i].c] = 'X';
                }
            }
            return;
        }
        void solve(vector<vector<char>>& board) {
            int len = board.size();
            if(len == 0) {
                return;
            }
            for(int i = 0 ; i < len ; i++) {
                for(int j = 0 ; j < len ; j++) {
                    vis[i][j] = false;
                }
            }
            for(int i = 0 ; i < len ; i++) {
                for(int j = 0 ; j < len ; j++) {
                    if(board[i][j] == 'O' && !vis[i][j] && !isInBorder(i, j, len)) {
                        vis[i][j] = true;
                        BFS(i, j, len, board);
                    }
                }
            }
        }
    };

  • 0

    Even this "solution" gets a runtime error despite "doing nothing":

    class Solution {
    public:
        bool vis[1005][1005];
        void solve(vector<vector<char>>& board) {
        }
    };
    

    It changes to "wrong answer" if you remove vis or make it smaller. So looks like you're not allowed to use that much memory like that, and custom tests allow more.


  • 0

    Thanks a lot ! It confused me for a long time, and I finally solved it.


  • 0

    How did you fix it now? Did you just make it smaller, or did you use a different data type, or something else?


  • 0

    There was another mistake in my code, I considered the board size was NN at first, but I realized it was NM actually, I fixed the bug and got an accept.


Log in to reply
 

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