Validating a 9 x 9 Sudoku Board

Sudoku is a popular puzzle game that challenges your logic and reasoning skills. One of the key aspects of Sudoku is ensuring that the board is valid. In this tutorial, we will explore how to determine if a 9 x 9 Sudoku board is valid based on specific rules. Whether you’re a beginner or just looking to brush up on your skills, this guide will walk you through the process step-by-step.

Prerequisites

Before we dive into the validation process, it’s helpful to have a basic understanding of the following concepts:

  • Sudoku Rules: Familiarize yourself with the basic rules of Sudoku, which state that each number from 1 to 9 must appear exactly once in each row, column, and 3×3 subgrid.
  • Data Structures: Understanding arrays or lists will be beneficial, as we will use them to represent the Sudoku board.

Step-by-Step Guide to Validate a Sudoku Board

Now that you have the prerequisites, let’s go through the steps to validate a 9 x 9 Sudoku board.

Step 1: Set Up the Board

First, you need to represent the Sudoku board. A typical Sudoku board is a 9×9 grid, where each cell can either contain a number (1-9) or be empty (represented by a dot or zero).

board = [
    [5, 3, '.', '.', 7, '.', '.', '.', '.'],
    [6, '.', '.', 1, 9, 5, '.', '.', '.'],
    ['.', 9, 8, '.', '.', '.', '.', 6, '.'],
    [8, '.', '.', '.', 6, '.', '.', '.', 3],
    [4, '.', '.', 8, '.', 3, '.', '.', 1],
    [7, '.', '.', '.', 2, '.', '.', '.', 6],
    ['.', 6, '.', '.', '.', '.', 2, 8, '.'],
    ['.', '.', '.', 4, 1, 9, '.', '.', 5],
    ['.', '.', '.', '.', 8, '.', '.', 7, 9]
]

Step 2: Validate Rows

To validate the rows, you need to check that each number appears only once in each row. You can use a set to keep track of the numbers you have seen.

def is_valid_row(row):
    seen = set()
    for num in row:
        if num != '.':
            if num in seen:
                return False
            seen.add(num)
    return True

Step 3: Validate Columns

Next, you will validate the columns in a similar manner. Loop through each column and check for duplicates.

def is_valid_column(board, col_index):
    seen = set()
    for row in board:
        num = row[col_index]
        if num != '.':
            if num in seen:
                return False
            seen.add(num)
    return True

Step 4: Validate 3×3 Subgrids

Finally, you need to validate the 3×3 subgrids. There are nine subgrids in a 9×9 Sudoku board, and you will check each one for duplicates.

def is_valid_subgrid(board, start_row, start_col):
    seen = set()
    for i in range(3):
        for j in range(3):
            num = board[start_row + i][start_col + j]
            if num != '.':
                if num in seen:
                    return False
                seen.add(num)
    return True

Step 5: Combine All Validations

Now that you have functions to validate rows, columns, and subgrids, you can combine them to check the entire board.

def is_valid_sudoku(board):
    for i in range(9):
        if not is_valid_row(board[i]):
            return False
        if not is_valid_column(board, i):
            return False
    for i in range(0, 9, 3):
        for j in range(0, 9, 3):
            if not is_valid_subgrid(board, i, j):
                return False
    return True

Conclusion

In this tutorial, we explored how to determine if a 9 x 9 Sudoku board is valid by validating its rows, columns, and 3×3 subgrids. By following the steps outlined above, you can implement your own Sudoku validator. This exercise not only enhances your programming skills but also deepens your understanding of the Sudoku game itself. Happy coding!

For further reading, check out the following links:

  • https://medium.com/deluxify/leetcode-36-valid-sukodu-f3f8d10aeae9?source=rss——algorithms-5″>Sudoku Basics
  • Continue reading on deluxify »”>Advanced Sudoku Techniques

Source: Original Article