OJ problem? This code passed C++ but not C


  • 2
    D
    int rows[9] = {0}, cols[9] = {0}, squares[9] = {0};
    int i, j;
    int tmp, b, sq;
    
    for (i = 0; i < 9; i++) {
        for (j = 0; j < 9; j++) {
            if (board[i][j] == '.') {
                continue;
            }
            
            b = board[i][j] - '1';
            if (b > 8 || b < 0) {
                return false;
            }
            
            tmp = 1 << b;
            
            if (rows[i] & tmp) {
                return false;
            }
            rows[i] |= tmp;
            
            if (cols[j] & tmp) {
                return false;
            }
            cols[j] |= tmp;
            
            sq = (i / 3) * 3 + (j / 3);
            if (squares[sq] & tmp) {
                return false;
            }
            squares[sq] |= tmp;
        }
    }
    
    return true;
    

    343 / 501 test cases passed.

    This wrong case is :

    Input: [".87654321","2........","3........","4........","5........","6........","7........","8........","9........"]

    Output: false

    Expected: true


  • 2
    L

    Hi,
    I encountered the same error, but it's correct on my local computer.


  • 0
    X

    me, too.... anybody can tell me the bug.


  • 1

    Thanks for reporting this issue. I have just fixed this issue. For anyone curious what had happened, it is due to the driver program is allocating the 2d array as a pointer of pointers (ie, char ** instead of char[9][9]).

    Since this is a 2d array with fixed row and column sizes, a simple char[9][9] should work so I've changed that. Now your program should get accepted.


Log in to reply
 

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