Why my verify-sudoku failed on OJ, but ok on my native host.


  • 0
    J

    My solution on sudoku verification is running ok on my native machine, but filed on OJ:

    #include <bandit/bandit.h>
    #include <vector>
    
    using namespace std;
    
    class Solution {
     public:
      bool isValidSudoku(vector<vector<char> > &board) {
        // bitmap
        unsigned int   check_3x3[3] = {0};
    
        for(int i=0; i<9; i++) {
          unsigned short check_row = 0;
          auto &row = board[i];
          int k = i % 3;
          for(int j=0; j < 9; j++) {
            if (check_row & (1<<(row[j]-1))) return false;
            if (row[j]) {
              check_row |= 1<<(row[j]-1);
              int b = (j / 3)*9-1;
              check_3x3[k] |= 1<<(row[j]+b);
            }
          }
          if (k == 2) {
            if (check_3x3[0] & check_3x3[1]) return false;
            if (check_3x3[1] & check_3x3[2]) return false;
            if (check_3x3[0] & check_3x3[2]) return false;
            check_3x3[0] = check_3x3[1] = check_3x3[2] = 0;
          }
        }
        return true;
      }
    
    };
    
    using namespace bandit;
    typedef vector<vector<char>> sudoku_t;
    go_bandit([]{
      describe("verify sudoku",[]{
        Solution sol;
        it("ok case", [&]{
          sudoku_t s {
            {5,3,0,0,7,0,0,0,0},
            {6,0,0,1,9,5,0,0,0},
            {0,9,8,0,0,0,0,6,0},
            {8,0,0,0,6,0,0,0,3},
            {4,0,0,8,0,3,0,0,1},
            {7,0,0,0,2,0,0,0,6},
            {0,6,0,0,0,0,2,8,0},
            {0,0,0,4,1,9,0,0,5},
            {0,0,0,0,8,0,0,7,9}
          };
          AssertThat(sol.isValidSudoku(s), Equals(true));
        });
        it("fail case", [&]{
          sudoku_t s {
            {5,3,0,0,7,0,0,0,0},
            {6,0,0,1,9,5,0,0,0},
            {0,9,8,0,0,0,0,6,0},
            {8,0,0,0,6,0,0,0,3},
            {4,0,0,8,0,3,0,0,1},
            {7,0,0,0,2,0,0,0,6},
            {0,6,0,0,0,0,2,8,0},
            {6,0,0,4,1,9,0,0,5},
            {0,0,0,0,8,0,0,7,9}
          };
          AssertThat(sol.isValidSudoku(s), Equals(false));
        });
    
        // this one failed to pass on OJ, but ok on my machine.
        it("ok case", [&]{
          sudoku_t s {
            {0,8,7,6,5,4,3,2,1},
            {2,0,0,0,0,0,0,0,0},
            {3,0,0,0,0,0,0,0,0},
            {4,0,0,0,0,0,0,0,0},
            {5,0,0,0,0,0,0,0,0},
            {6,0,0,0,0,0,0,0,0},
            {7,0,0,0,0,0,0,0,0},
            {8,0,0,0,0,0,0,0,0},
            {9,0,0,0,0,0,0,0,0}
          };
          AssertThat(sol.isValidSudoku(s), Equals(true));
        });
    
      });
    });
    
    int main(int argc, char** argv)
    {
      return bandit::run(argc, argv);
    }

Log in to reply
 

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