1 //occupyed_1检查行是否占用 2 //occupyed_2检查列是否占用 3 //occupyed_3检查块是否占用 4 bool isValidSudoku(vector<vector<char>>& board) 5 { 6 int occupyed_1[9][9], occupyed_2[9][9], occupyed_3[9][9]; 7 for (int i = 0; i < 9; ++i) { 8 for (int j = 0; j < 9; ++j) { 9 occupyed_1[i][j] = 0; 10 occupyed_2[i][j] = 0; 11 occupyed_3[i][j] = 0; 12 } 13 } 14 15 int rowSize = board.size(); 16 int colSize = board[0].size(); 17 for (int i = 0; i < rowSize; ++i) { 18 for (int j = 0; j < colSize; ++j) { 19 if (board[i][j] != ‘.‘) { 20 int num = board[i][j] - ‘0‘ - 1; 21 int k = i / 3 * 3 + j / 3; 22 23 if (occupyed_1[i][num] || occupyed_2[j][num] 24 || occupyed_3[k][num]) 25 return false; 26 occupyed_1[i][num] = 1; 27 occupyed_2[j][num] = 1; 28 occupyed_3[k][num] = 1; 29 } 30 } 31 } 32 return true; 33 }
时间: 2024-10-25 19:40:18