xxxxxxxxxx
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
// Function to print the Sudoku board
void printBoard(const vector<vector<int>>& board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
// Function to check if the user's solution is correct
bool isValidSolution(const vector<vector<int>>& board) {
// Check all rows and columns
for (int i = 0; i < 9; i++) {
vector<bool> rowCheck(9, false);
vector<bool> colCheck(9, false);
for (int j = 0; j < 9; j++) {
// Check the row
if (board[i][j] != 0) {
if (rowCheck[board[i][j] - 1]) return false;
rowCheck[board[i][j] - 1] = true;
}
// Check the column
if (board[j][i] != 0) {
if (colCheck[board[j][i] - 1]) return false;
colCheck[board[j][i] - 1] = true;
}
}
}
// Check all 3x3 subgrids
for (int i = 0; i < 9; i += 3) {
for (int j = 0; j < 9; j += 3) {
vector<bool> gridCheck(9, false);
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
int val = board[i + r][j + c];
if (val != 0) {
if (gridCheck[val - 1]) return false;
gridCheck[val - 1] = true;
}
}
}
}
}
return true;
}
// Function to generate a random Sudoku puzzle (simplified version)
void generateSudoku(vector<vector<int>>& board) {
// Fill the board with an initial pattern (could be random)
srand(time(0));
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
// Randomly fill some cells (0 means empty)
if (rand() % 4 == 0) { // About 25% chance to leave a cell empty
board[i][j] = 0;
} else {
board[i][j] = rand() % 9 + 1; // Random number from 1 to 9
}
}
}
}
// Function to allow the user to manually solve the puzzle
void getUserInput(vector<vector<int>>& board) {
cout << "Enter your solution (fill 0 for empty cells):\n";
int row, col, value;
while (true) {
printBoard(board);
cout << "Enter row (1-9), column (1-9), and value (1-9), or -1 to submit: ";
cin >> row;
if (row == -1) break; // Submit the solution
cin >> col >> value;
if (row >= 1 && row <= 9 && col >= 1 && col <= 9 && value >= 1 && value <= 9) {
// Update the board
board[row - 1][col - 1] = value;
} else {
cout << "Invalid input. Try again.\n";
}
}
}
// Main function
int main() {
vector<vector<int>> board(9, vector<int>(9, 0));
generateSudoku(board);
cout << "Sudoku Puzzle: \n";
printBoard(board);
getUserInput(board);
cout << "\nYour solution: \n";
printBoard(board);
if (isValidSolution(board)) {
cout << "Congratulations! Your solution is correct.\n";
} else {
cout << "Sorry, your solution is incorrect. Try again.\n";
}
return 0;
}
xxxxxxxxxx
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
# a function to check rows, columns and blocks using O(logN) space
def not_valid(digits, m=0):
return any( 1 & (m >> int(d)) + (m := (m | (1 << int(d))))*0
for d in digits if d != ".")
# a generator to extract blocks
def blocks():
for i in range(3):
for j in range(3):
yield (n for row in board[i*3:(i+1)*3] for n in row[j*3:(j+1)*3])
if any(map(not_valid, board)) : return False # [1] test rows
if any(map(not_valid, zip(*board))) : return False # [2] test columns
if any(map(not_valid, blocks())) : return False # [3] test blocks
return True
xxxxxxxxxx
OFFSET -- specifies the number of rows to skip before selecting
-- In MySQL OFFSET keyword cannot be used alone without the LIMIT keyword.
"MySQL"
select * from Employee LIMIT 1 OFFSET 3;
-- selects 1 row after skipping 3 rows
-- In SQL Server, the OFFSET keyword cannot be used without an ORDER BY clause
"SQL Server"
select * from Employee order by Salary offset 1 rows
-- selects rows after skipping 1 row ordered by Salary column in ascending order