Can someone help me debug my code please? not sure where went wrong


  • 0
    O
     * Return an array of arrays of size *returnSize.
     * The sizes of the arrays are returned as *columnSizes array.
     * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
     */
    int exist(int x, int y, int boardRowSize, int boardColSize){
        if((x >= 0) && (x < boardRowSize) && (y >= 0) && (y < boardColSize)){
            return 1;
        }else{
            return 0;
        }
    }
    
    void reveal(char** board, int boardRowSize, int boardColSize, int x, int y){
        if(exist(x, y, boardRowSize, boardColSize) == 1){
            if(board[x][y] == 'E'){
                int count = 0;
                if(exist(x-1, y-1, boardRowSize, boardColSize) == 1 && board[x-1][y-1] == 'M') count++;
                if(exist(x-1, y, boardRowSize, boardColSize) == 1 && board[x-1][y] == 'M') count++;
                if(exist(x-1, y+1, boardRowSize, boardColSize) == 1 && board[x-1][y+1] == 'M') count++;
                if(exist(x, y-1, boardRowSize, boardColSize) == 1 && board[x][y-1] == 'M') count++;
                if(exist(x, y+1, boardRowSize, boardColSize) == 1 && board[x][y+1] == 'M') count++;
                if(exist(x+1, y-1, boardRowSize, boardColSize) == 1 && board[x+1][y-1] == 'M') count++;
                if(exist(x+1, y, boardRowSize, boardColSize) == 1 && board[x+1][y] == 'M') count++;
                if(exist(x+1, y+1, boardRowSize, boardColSize) == 1 && board[x+1][y+1] == 'M') count++;
                //printf("%d\n", count);
                if(count > 0) board[x][y] = '0' + count;
                else{
                    board[x][y] = 'B';
                    //printf("changed to B");
                    reveal(board, boardRowSize, boardColSize, x-1, y-1);
                    reveal(board, boardRowSize, boardColSize, x-1, y);
                    reveal(board, boardRowSize, boardColSize, x-1, y+1);
                    reveal(board, boardRowSize, boardColSize, x, y-1);
                    reveal(board, boardRowSize, boardColSize, x, y+1);
                    reveal(board, boardRowSize, boardColSize, x+1, y-1);
                    reveal(board, boardRowSize, boardColSize, x+1, y);
                    reveal(board, boardRowSize, boardColSize, x+1, y+1);
                } 
            }
        }
    }
    char** updateBoard(char** board, int boardRowSize, int boardColSize, int* click, int clickSize, int** columnSizes, int* returnSize) {
        //printf("boardRowSize = %d, boardColSize = %d\n", boardRowSize, boardColSize);
        //printf("%c", *board[0]);
        *returnSize = boardRowSize;
        *columnSizes = (int*)malloc(sizeof(int)*boardRowSize);
        int i;
        for(i = 0; i < boardRowSize; i++){
            (*columnSizes)[i] = boardColSize;
            //printf("columnSizes[%d] = %d\n", i, (*columnSizes)[i]);
        }
        //printf("columnsize %d", **columnSizes);
        
        //printf("returnSize = %d", *returnSize);
        if(exist(click[0], click[1], boardRowSize, boardColSize) == 0) return board;
        if(board[click[0]][click[1]] == 'M') {
            board[click[0]][click[1]] = 'X';
            return board;
        }
        reveal(board, boardRowSize, boardColSize, click[0], click[1]);
        //printf("board[0][2] = %c", board[0][2]);
        return board;
    }
    

  • 0
    T

    I think the C version has problem.
    When we set value in columnSizes or malloc,it will raise Runtime Error.I don't know how to solve it.


Log in to reply
 

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